WebSocket4Net icon indicating copy to clipboard operation
WebSocket4Net copied to clipboard

Websocket4net doesn't terminate connection

Open Radzhab opened this issue 7 years ago • 9 comments

I connect to server and wanna disconnect from it.

When i rise method websocket.Close() its not terminate connection. I see status "Closed". What means it? How force terminate connection?

public sealed class Singleton {
 private static volatile Singleton instance;
 private static object sync = new Object();

 private Singleton() {}

 public static Singleton GetInstance {
  get {
   if (instance == null) {
    lock(sync) {
     if (instance == null)
      instance = new Singleton();
    }
   }

   return instance;
  }
 }


 public bool flag;
 public string cookies;
 public string json;
 WebSocket websocket;


 public void Run(string socketUrl) {
  
  List < KeyValuePair < string, string >> cookieList = new List < KeyValuePair < string, string >> ();
  string[] pairs = cookies.Split(';');

  int pos;
  string key, value;

  foreach(var p in pairs) {
   pos = p.IndexOf('=');
   if (pos > 0) {
    key = p.Substring(0, pos).Trim();
    pos += 1;
    if (pos < p.Length)
     value = p.Substring(pos).Trim();
    else
     value = string.Empty;

    cookieList.Add(new KeyValuePair < string, string > (key, Uri.UnescapeDataString(value)));
   }
  }

  websocket = new WebSocket(socketUrl, "", cookieList);

  websocket.Open();

  websocket.Opened += (a, b) => {

  };
  websocket.Closed += (a, b) => {

  };
  websocket.MessageReceived += (a, b) => {

   json = b.Message;

   project.SendErrorToLog(json, true);
   if (json.Contains("add_items")) //
   {

    // this method not working
    websocket.Close();
   }

  };
 }
 public void Close() {
  websocket.Close();
 }
`

Radzhab avatar Sep 25 '17 21:09 Radzhab

Could you write a test case to demonstrate this issue?

kerryjiang avatar Oct 02 '17 06:10 kerryjiang

Same problem were... Many people are affect by this issue. Can you fix it please? Error message: "The socket is connected, you needn't connect again!"

Websocket instance properties at the moment of exception:

  • Handshaked: true
  • State: Connecting

Test case:

    class WebSocketHandler
    {
        private WebSocket webSocket;

        public WebSocketHandler(string ws)
        {
            webSocket = new WebSocket(ws);
        }

        public void Initialize()
        {
            webSocket.Closed += webSocket_Closed;
            webSocket.Open();
            webSocket.Close();
        }

        private void webSocket_Closed(object sender, EventArgs e)
        {
            webSocket.Open();
        }
    }

kristijan97 avatar Oct 13 '17 13:10 kristijan97

Open method just start the open connection procedure. You need wait the OnOpen event to be fired and then make further actions.

kerryjiang avatar Oct 21 '17 06:10 kerryjiang

@kerryjiang can you provide test case?

Radzhab avatar Oct 26 '17 08:10 Radzhab

@kerryjiang can you provide test sample? I have a similar error "The socket is connecting, cannot connect again"

If WSocket IsNot Nothing AndAlso Not WSocket.State = WebSocketState.Connecting Then WSocket.Open()

Maximiz avatar Jun 16 '18 12:06 Maximiz

++++

ahvahsky2008 avatar Feb 14 '19 13:02 ahvahsky2008

@kerryjiang can you provide test sample? I have a similar error "The socket is connecting, cannot connect again"

If WSocket IsNot Nothing AndAlso Not WSocket.State = WebSocketState.Connecting Then WSocket.Open()

do you solve issue?

ahvahsky2008 avatar Feb 14 '19 13:02 ahvahsky2008

The typical case is to call Close, which will perform a close handshake and then close the WebSocket. Forcibly closing a WebSocket without a close handshake should be rare, but if you need to for some reason, call Dispose on the WebSocket.

breyed avatar May 09 '19 14:05 breyed

Any update on this one?

faulandcc avatar Nov 13 '19 14:11 faulandcc