Closed prawnsalad closed 2 months ago
use WriteAsync to replace WriteClose
socket.WriteAsync(gws.OpcodeCloseConnection, nil, func(err error) {
_ = socket.NetConn().Close()
})
Thanks.. that does send the close. But it doesn't allow a custom close status and reason?
$ ws ws://localhost:5000/connect
< last text message
websocket: close 1005 (no status)
And secondly - where does it actually close the socket connection when sending OpcodeCloseConnection? I don't see that in the code.
The coding of code and reason makes little sense.
And secondly - where does it actually close the socket connection when sending OpcodeCloseConnection? I don't see that in the code.
I updated the code above
Ah.. the callback function was in a newer version than I had. I've updated and now that makes more sense, awesome.
WriteClose()
supports the code and reason which I see is encoded in Conn.emitError()
. Do we need to manually encode the status and reason to use it in WriteAsync()
?
For reference I now have this function which looks to do what I need, but I was expecting a gws function that handles the code and reason encoding for me. No problem if not though - I'm just looking for the correct way
func closeConnections(conns []*connectionlookup.Connection, code int16, reason []byte) {
closeCode := []byte{uint8(code >> 8), uint8(code << 8 >> 8)}
payload := append(closeCode, reason...)
for _, con := range conns {
con.Socket.WriteAsync(gws.OpcodeCloseConnection, payload, func(err error) {
con.Socket.NetConn().Close()
})
}
}
Consider adding a new WriteCloseAsync method
I'm trying to broadcast a message and then close several connections at once. Eg:
Broadcaster.Broadcast()
adds the message to a socketswriteQueue
, butConn.WriteClose()
>emitError
bypasses the queue and closes instantly, causing the final message from the Broadcaster to not be sent yet.What is the correct way to wait until the broadcaster has finished?