Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using websocket-sharp for .NET how can the server close a WebSocketBehavior?

I'm trying to use websocket-sharp for a project and the server needs to be able to drop websocket connections.

The connections are represented by a WebSocketBehavior class which you inherit from. There's no Stop or Close or Disconnect method on WebSocketBehavior. The closest I could find is

WebSocketBehavior.Context.WebSocket.Close

So I tried adding a method to my overriding class

public class MySocket : WebSocketBehavior
{
    public void Close()
    {
        base.Context.WebSocket.Close();
    }
}

But that causes an error when logging is turned on

2/5/2016 7:08:25 PM|Error|WebSocket.Close|This operation isn't available in: closed

How can the server disconnect/close a WebSocket connection?

like image 632
gman Avatar asked Feb 06 '16 05:02

gman


1 Answers

As stated from a ticket in their repo

You just need to close the related session:

 Sessions.CloseSession(ID);

EDIT:

It is the same code as the OP but called through correct abstractions, that should work in theory, however the fact that he tries to access stuff from "low level", made me to suspect that he access other internal functionality all bypassing "public method" wich could results in wrong behaviour (like stuff called by "public methods" no longer working, I suggest you access everything only from proper abstractions in order to avoid problems, after that if the problem persist you should make a ticket asking for a bugfix in the original repository. Such a bug cannot by fixed there becuse require to be aware of all other the code written by you.

After that you should track both client and server side with a log file to discover what happens, in example connection already closed by client (as stated by Evk in comments)

like image 83
CoffeDeveloper Avatar answered Oct 01 '22 05:10

CoffeDeveloper