package pubsub import ( "encoding/json" "time" ) func go_reader(c *Connection) { go func(c *Connection) { for { select { case <-c.done: return default: if c.active { _, msg, err := c.Connection.ReadMessage() if err != nil { c.onError(err) c.active = false c.onDisconnect() // Wait 1 second or return immediately select { case <-time.After(time.Second): case <-c.done: return } } else { var resp struct { Type string `json:"type"` } if err := json.Unmarshal(msg, &resp); err != nil { c.onError(err) } else { if resp.Type == "PONG" { ct := time.Now() c.onPong(c.ping_start, ct) c.ping_start = ct c.ping_sended = false } else if resp.Type == "RECONNECT" { // TODO: ... } else { c.onMessage(msg) } } } } else { // Wait 1 second or return immediately select { case <-time.After(time.Second): case <-c.done: return } } } } }(c) }