How to close the connection in a WebSocketBehavior ?
Hi,
Do you have any input about how to close a connection server side? I want to close the connection when the access is unauthorized.
In your WebSocketBehavior implementation class, call this.Sessions.CloseSession(sessionId) where sessionId is the session to close.
EDIT: this.ID to get the current session identifier.
Thank you it works fine. I added a sessions.CloseSession(ID); in my OnOpen callback.
I leave the issue open because it may be interesting to have a Close() method.
Hello there,
There is another way to close, by calling the WebSocketBehavior.Context.WebSocket.Close method.
I don't guess that it's so often to call the close in from the derived WebSocketBehavior class, isn't it so?
I don't know, but I don't think it's very common. A close method can make it closer to the client WebSocket class, but you maybe don't want to have a bloated WebSocketBehavior.
It makes sense to close if you want to allow websocket connections from whitelisted IP addresses only @sta