diff --git a/engineio/README.md b/engineio/README.md index dbc33c5d..05c5bb23 100644 --- a/engineio/README.md +++ b/engineio/README.md @@ -72,4 +72,4 @@ func main() { ## License -The 3-clause BSD License - see LICENSE for more details +The 3-clause BSD License - see [LICENSE](https://opensource.org/licenses/BSD-3-Clause) for more details diff --git a/engineio/client.go b/engineio/client.go index 5ee549dc..5f56455c 100644 --- a/engineio/client.go +++ b/engineio/client.go @@ -1,6 +1,7 @@ package engineio import ( + "fmt" "io" "net" "net/http" @@ -66,10 +67,14 @@ func (c *client) NextReader() (session.FrameType, io.ReadCloser, error) { switch pt { case packet.PONG: - c.conn.SetReadDeadline(time.Now().Add(c.params.PingInterval + c.params.PingTimeout)) + if err = c.conn.SetReadDeadline(time.Now().Add(c.params.PingInterval + c.params.PingTimeout)); err != nil { + return 0, nil, err + } + case packet.CLOSE: c.Close() return 0, nil, io.EOF + case packet.MESSAGE: return session.FrameType(ft), r, nil } @@ -116,6 +121,9 @@ func (c *client) serve() { if err := w.Close(); err != nil { return } - c.conn.SetWriteDeadline(time.Now().Add(c.params.PingInterval + c.params.PingTimeout)) + + if err = c.conn.SetWriteDeadline(time.Now().Add(c.params.PingInterval + c.params.PingTimeout)); err != nil { + fmt.Printf("set writer's deadline error,msg:%s\n", err.Error()) + } } } diff --git a/engineio/transport/polling/server.go b/engineio/transport/polling/server.go index cc083794..f1af02c1 100644 --- a/engineio/transport/polling/server.go +++ b/engineio/transport/polling/server.go @@ -2,6 +2,7 @@ package polling import ( "bytes" + "fmt" "html/template" "net" "net/http" @@ -64,7 +65,7 @@ func (c *serverConn) SetHeaders(w http.ResponseWriter, r *http.Request) { w.Header().Set("X-XSS-Protection", "0") } - //just in case the default behaviour gets changed and it has to handle an origin check + // just in case the default behaviour gets changed and it has to handle an origin check checkOrigin := Default.CheckOrigin if c.transport.CheckOrigin != nil { checkOrigin = c.transport.CheckOrigin @@ -136,6 +137,9 @@ func (c *serverConn) ServeHTTP(w http.ResponseWriter, r *http.Request) { } _, err = w.Write([]byte("ok")) + if err != nil { + fmt.Printf("ack post err=%s\n", err.Error()) + } default: http.Error(w, "invalid method", http.StatusBadRequest) diff --git a/namespace_conn.go b/namespace_conn.go index fcc9543c..c7ada639 100644 --- a/namespace_conn.go +++ b/namespace_conn.go @@ -132,5 +132,4 @@ func (nc *namespaceConn) dispatch(header parser.Header) { return } } - return } diff --git a/redis_broadcast.go b/redis_broadcast.go index 29602220..c0a4c94c 100644 --- a/redis_broadcast.go +++ b/redis_broadcast.go @@ -129,8 +129,13 @@ func newRedisBroadcast(nsp string, adapter *RedisAdapterOptions) (*redisBroadcas bc.resChannel = bc.prefix + "-response#" + bc.nsp bc.requests = make(map[string]interface{}) - bc.sub.PSubscribe(bc.prefix + "#" + bc.nsp + "#*") - bc.sub.Subscribe(bc.reqChannel, bc.resChannel) + if err = bc.sub.PSubscribe(bc.prefix + "#" + bc.nsp + "#*"); err != nil { + return nil, err + } + + if err = bc.sub.Subscribe(bc.reqChannel, bc.resChannel); err != nil { + return nil, err + } go func() { for { @@ -144,7 +149,10 @@ func newRedisBroadcast(nsp string, adapter *RedisAdapterOptions) (*redisBroadcas break } - bc.onMessage(m.Channel, m.Data) + err = bc.onMessage(m.Channel, m.Data) + if err != nil { + return + } case redis.Subscription: if m.Count == 0 { return @@ -172,9 +180,13 @@ func (bc *redisBroadcast) AllRooms() []string { req.done = make(chan bool, 1) bc.requests[req.RequestID] = &req - bc.pub.Conn.Do("PUBLISH", bc.reqChannel, reqJSON) + _, err := bc.pub.Conn.Do("PUBLISH", bc.reqChannel, reqJSON) + if err != nil { + return []string{} // if error occurred,return empty + } <-req.done + rooms := make([]string, 0, len(req.rooms)) for room := range req.rooms { rooms = append(rooms, room) @@ -338,6 +350,9 @@ func (bc *redisBroadcast) onMessage(channel string, msg []byte) error { } event, ok := opts[1].(string) + if !ok { + return errors.New("invalid event") + } if room != "" { bc.send(room, event, args...) @@ -355,19 +370,20 @@ func (bc *redisBroadcast) getNumSub(channel string) (int, error) { return 0, err } - var numSub64 int64 - numSub64 = rs.([]interface{})[1].(int64) - return int(numSub64), nil + numSub64, ok := rs.([]interface{})[1].(int) + if !ok { + return 0, errors.New("redis reply cast to int error") + } + return numSub64, nil } // Handle request from redis channel. func (bc *redisBroadcast) onRequest(msg []byte) { var req map[string]string - err := json.Unmarshal(msg, &req) - if err != nil { + + if err := json.Unmarshal(msg, &req); err != nil { return } - // log.Println("on request:", req) var res interface{} switch req["RequestType"] { @@ -417,7 +433,6 @@ func (bc *redisBroadcast) onResponse(msg []byte) { return } - // log.Println("on resp:", res) switch res["RequestType"] { case roomLenReqType: roomLenReq := req.(*roomLenRequest)