Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket4net doesn't terminate connection #104

Open
Radzhab opened this issue Sep 25, 2017 · 9 comments
Open

Websocket4net doesn't terminate connection #104

Radzhab opened this issue Sep 25, 2017 · 9 comments

Comments

@Radzhab
Copy link

Radzhab commented Sep 25, 2017

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
Copy link
Owner

Could you write a test case to demonstrate this issue?

@offbit-it
Copy link

offbit-it commented Oct 13, 2017

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();
        }
    }

@kerryjiang
Copy link
Owner

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

@Radzhab
Copy link
Author

Radzhab commented Oct 26, 2017

@kerryjiang can you provide test case?

@Maximiz
Copy link

Maximiz commented Jun 16, 2018

@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
Copy link

++++

@ahvahsky2008
Copy link

@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
Copy link

breyed commented May 9, 2019

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
Copy link

Any update on this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants