Skip to content

Commit

Permalink
设置readWait
Browse files Browse the repository at this point in the history
  • Loading branch information
Kotodian committed Aug 28, 2021
1 parent f0ca57d commit 1b1b8e7
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions ac/websocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second
readWait = 100 * time.Second

// Maximum message size allowed from peer.
maxMessageSize = 4096
Expand Down Expand Up @@ -247,12 +248,12 @@ func (c *Client) ReadPump() {
_ = c.Close(err)
}()
c.conn.SetReadLimit(maxMessageSize)
err = c.conn.SetReadDeadline(time.Now().Add(90 * time.Second))
err = c.conn.SetReadDeadline(time.Now().Add(readWait * time.Second))
if err != nil {
return
}
c.conn.SetPingHandler(func(appData string) error {
_ = c.conn.SetReadDeadline(time.Now().Add(90 * time.Second))
_ = c.conn.SetReadDeadline(time.Now().Add(readWait * time.Second))
c.log.Info("ping message received", zap.String("sn", c.chargeStation.SN()))
err = c.conn.WriteControl(websocket.PongMessage, []byte(appData), time.Now().Add(writeWait))
if err == websocket.ErrCloseSent {
Expand All @@ -270,23 +271,18 @@ func (c *Client) ReadPump() {

})
for {
select {
case <-c.close:
return
default:
}
ctx := context.WithValue(context.TODO(), "client", c)
if c.conn == nil {
return
}
err = c.conn.SetReadDeadline(time.Now().Add(90 * time.Second))
err = c.conn.SetReadDeadline(time.Now().Add(readWait * time.Second))
if err != nil {
break
}
var msg []byte
_, msg, err = c.conn.ReadMessage()
if err != nil {
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
if websocket.IsUnexpectedCloseError(err, websocket.CloseAbnormalClosure, websocket.CloseAbnormalClosure) {
c.log.Sugar().Errorf("error: %v", err)
}
break
Expand Down

0 comments on commit 1b1b8e7

Please sign in to comment.