Skip to content

Commit b1c1c1d

Browse files
dil-spaynemattqs
authored andcommitted
Allows disconnection when socket opening\opened but not connected (#102)
* Fix disconnection while opening connection * Fix reconnection after closing connection
1 parent b20537f commit b1c1c1d

2 files changed

Lines changed: 32 additions & 16 deletions

File tree

Src/SocketIoClientDotNet.net45/Client/Manager.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public enum ReadyStateEnum
4242
private long _reconnectionDelay;
4343
private long _reconnectionDelayMax;
4444
private long _timeout;
45-
private int Connected;
4645
private int Attempts;
4746
private Uri Uri;
4847
private List<Parser.Packet> PacketBuffer;
4948
private Queue<On.IHandle> Subs;
5049
private Quobject.EngineIoClientDotNet.Client.Socket.Options Opts;
5150
private bool AutoConnect;
51+
private HashSet<Socket> OpeningSockets;
5252
/*package*/
5353

5454
public Quobject.EngineIoClientDotNet.Client.Socket EngineSocket;
@@ -95,10 +95,10 @@ public Manager(Uri uri, Options opts)
9595
this.Timeout(opts.Timeout < 0 ? 20000 : opts.Timeout);
9696
this.ReadyState = ReadyStateEnum.CLOSED;
9797
this.Uri = uri;
98-
this.Connected = 0;
9998
this.Attempts = 0;
10099
this.Encoding = false;
101100
this.PacketBuffer = new List<Parser.Packet>();
101+
this.OpeningSockets = new HashSet<Socket>();
102102
this.Encoder = new Parser.Parser.Encoder();
103103
this.Decoder = new Parser.Parser.Decoder();
104104
this.AutoConnect = opts.AutoConnect;
@@ -201,6 +201,8 @@ private Manager Open(IOpenCallback fn)
201201
Quobject.EngineIoClientDotNet.Client.Socket socket = EngineSocket;
202202

203203
ReadyState = ReadyStateEnum.OPENING;
204+
OpeningSockets.Add(Socket(Uri.PathAndQuery));
205+
SkipReconnect = false;
204206

205207
var openSub = SocketIoClientDotNet.Client.On.Create(socket, Engine.EVENT_OPEN, new ListenerImpl(() =>
206208
{
@@ -333,17 +335,14 @@ public Socket Socket(string nsp)
333335

334336
var socket = new Socket(this,nsp);
335337
Nsps = Nsps.Add(nsp, socket);
336-
socket.On(Client.Socket.EVENT_CONNECT, new ListenerImpl(() =>
337-
{
338-
Connected++;
339-
}));
338+
340339
return socket;
341340
}
342341

343342
internal void Destroy(Socket socket)
344343
{
345-
--Connected;
346-
if (Connected == 0)
344+
OpeningSockets.Remove(socket);
345+
if (OpeningSockets.Count == 0)
347346
{
348347
Close();
349348
}
@@ -406,7 +405,19 @@ private void Cleanup()
406405
public void Close()
407406
{
408407
this.SkipReconnect = true;
409-
this.EngineSocket.Close();
408+
this.Reconnecting = false;
409+
410+
if (ReadyState != ReadyStateEnum.OPEN)
411+
{
412+
Cleanup();
413+
}
414+
415+
ReadyState = ReadyStateEnum.CLOSED;
416+
417+
if (EngineSocket != null)
418+
{
419+
this.EngineSocket.Close();
420+
}
410421
}
411422

412423

@@ -428,7 +439,7 @@ private void Reconnect()
428439
{
429440
var log = LogManager.GetLogger(Global.CallerName());
430441

431-
if (Reconnecting)
442+
if (Reconnecting || SkipReconnect)
432443
{
433444
return;
434445
}

Src/SocketIoClientDotNet.net45/Client/Socket.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,21 @@ private void Destroy()
362362

363363
public Socket Close()
364364
{
365-
if (!Connected)
365+
var log = LogManager.GetLogger(Global.CallerName());
366+
367+
if (Connected)
366368
{
367-
return this;
369+
log.Info(string.Format("performing disconnect ({0})", Nsp));
370+
Packet(new Packet(Parser.Parser.DISCONNECT));
368371
}
369-
var log = LogManager.GetLogger(Global.CallerName());
370372

371-
log.Info(string.Format("performing disconnect ({0})", Nsp));
372-
Packet(new Packet(Parser.Parser.DISCONNECT));
373373
Destroy();
374-
OnClose("io client disconnect");
374+
375+
if (Connected)
376+
{
377+
OnClose("io client disconnect");
378+
}
379+
375380
return this;
376381
}
377382

0 commit comments

Comments
 (0)