WebSocket4Net
WebSocket4Net copied to clipboard
Websocket4net doesn't terminate connection
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();
}
`
Could you write a test case to demonstrate this issue?
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();
}
}
Open method just start the open connection procedure. You need wait the OnOpen event to be fired and then make further actions.
@kerryjiang can you provide test case?
@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()
++++
@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?
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.
Any update on this one?