我在Go中有以下读取泵用于大猩猩websockets连接。
func (c *connection) readPump() {
//function to close the websocket
defer func() {
socketsHub.unregister <- c
c.ws.Close()
}()
c.ws.SetReadLimit(maxMessageSize)
c.ws.SetReadDeadline(time.Now().Add(pongWait))
c.ws.SetPongHandler(func(string) error {
c.ws.SetReadDeadline(time.Now().Add(pongWait))
return nil
})
for {
//create a new socketMessage
message := socketMessage{}
//get the json message from the socket
err := c.ws.ReadJSON(message)
if err != nil {
log.Println(err)
break
}
//add the connection to the message
message.Connection = c
shouldBroadcast := message.Process()
//if the new message should be broadcasted to any user (includeing the
//one who sent it)
if shouldBroadcast {
socketsHub.broadcast <- message
}
}
}
这是我们在前端使用的代码:
if (window["WebSocket"]) {
conn = new WebSocket("wss://localhost/api/ws")
conn.onclose = function(evt) {
console.log($("<div><b>Connection closed.</b></div>"))
}
} else {
console.log($("<div><b>Your browser does not support WebSockets.</b></div>"))
}
每次加载任何页面时都会出现'websocket: close 1005'
错误。我快要疯了。任何帮助都会很棒。