Hello, I am wondering what the proper way to close a websocket connection is when using the websocket client. Everything I try ends in an abnormal 1006 error code instead of the normal closure codes of 1000 or 1001. I think the problem is that a close frame needs to be sent before closing the connection on the client side. I does not seem like conn.Close() does that and sending control messages before closing the connection does not seem to work either. Do I maybe need to handle the close frame on the server side?
I was wondering if someone could provide or point me towards an example of how to properly close a websocket connection on the client side. I have not been able to find an example of this yet.
Thanks in advance for any help, it has been very frustrating trying to get to the bottom of this.
More Info
On the server side I am using the ReadJSON function and have started to experiment with reading from the socket manually through NextReader(), as shown in examples. Unfortunately both seem to yield the same results.
On the client side I am simply dialing the websocket and then trying to close it (I have simplified the example just to highlight the problem)
client, _, err := websocket.DefaultDialer.Dial(WEBSOCKET_ADDRESS, nil)
if err != nil {
t.Fatal("dial:", err)
}
go func() {
for {
res := new(jsonstructs.WebsocketResponse)
err := client.ReadJSON(res)
if err != nil {
t.Log(err)
break
}
t.Log(res)
}
}()
// Do stuff with the websocket here
// ...
t.Log("Write Control")
// Is this needed?
client.WriteControl(websocket.CloseNormalClosure, []byte(""), time.Now().Add(time.Second*5))
t.Log("Close")
client.Close()