I have the following read pump for a gorilla websockets connection in Go.
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
}
}
}
This is the code we are using on the frontend:
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>"))
}
Everytime I load any page I get an 'websocket: close 1005'
error. I am about to go crazy. Any help would be great.