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

I am trying to use websocket-sharp for a project, and the server should be able to remove connections to websites.

Connections are represented by the WebSocketBehavior class that you inherit. There is no Stop or Close or Disconnect method in WebSocketBehavior . The closest I could find is

 WebSocketBehavior.Context.WebSocket.Close 

So, I tried to add a method to my override class

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

But this causes an error when enabling logging

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

How can a server disconnect / close a WebSocket connection?

+6
source share
1 answer

As indicated on the ticket in their repo

You just need to close the associated session:

  Sessions.CloseSession(ID); 

EDIT:

This is the same code as OP, but called through the correct abstractions , which should work theoretically, however, the fact that it is trying to access materials from the "low level" made me suspect that it is gaining access to other internal functions, bypassing the "public method", which can lead to deviant behavior (for example, a material called "public methods" no longer work, I suggest you only access from the right abstractions to avoid the problem, then, if the problem of IP eznet, you should make a ticket request for the correction of errors in the source repository. Such an error can not be corrected there, because you need to know all the other codes written by you.

After that, you should monitor both the client and server sides with the log file to find out what is happening, for example, a connection that has already been closed by the client (as indicated by Evk in the comments)

+1
source

All Articles