kerryjiang / WebSocket4Net

A popular .NET WebSocket Client
752 stars 272 forks source link

Websocket4net doesn't terminate connection #104

Open Radzhab opened 6 years ago

Radzhab commented 6 years ago

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();
 }
`
kerryjiang commented 6 years ago

Could you write a test case to demonstrate this issue?

kristijan97 commented 6 years ago

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:

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();
        }
    }
kerryjiang commented 6 years ago

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

Radzhab commented 6 years ago

@kerryjiang can you provide test case?

Maximiz commented 6 years ago

@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()

ahvahsky2008 commented 5 years ago

++++

ahvahsky2008 commented 5 years ago

@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?

breyed commented 5 years ago

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.

faulandcc commented 4 years ago

Any update on this one?