websocket-sharp icon indicating copy to clipboard operation
websocket-sharp copied to clipboard

How to close the connection in a WebSocketBehavior ?

Open fungiboletus opened this issue 11 years ago • 5 comments

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.

fungiboletus avatar Feb 06 '15 09:02 fungiboletus

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.

Maxhy avatar Feb 06 '15 09:02 Maxhy

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.

fungiboletus avatar Feb 06 '15 10:02 fungiboletus

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?

sta avatar Feb 07 '15 08:02 sta

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.

fungiboletus avatar Feb 12 '15 08:02 fungiboletus

It makes sense to close if you want to allow websocket connections from whitelisted IP addresses only @sta

HashPong avatar Dec 09 '22 12:12 HashPong