This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
478 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace SocketIOSharp.Client | ||
{ | ||
partial class SocketIOClient | ||
{ | ||
private static class Event | ||
{ | ||
public static readonly string CONNECT_ERROR = "connect_error"; | ||
|
||
public static readonly string RECONNECT = "reconnect"; | ||
public static readonly string RECONNECT_ATTEMPT = "reconnect_attempt"; | ||
public static readonly string RECONNECTING = "reconnecting"; | ||
|
||
public static readonly string RECONNECT_ERROR = "reconnect_error"; | ||
public static readonly string RECONNECT_FAILED = "reconnect_failed"; | ||
|
||
public static readonly string PING = "ping"; | ||
public static readonly string PONG = "pong"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,120 @@ | ||
using EngineIOSharp.Client; | ||
using EngineIOSharp.Common.Enum; | ||
using EngineIOSharp.Common.Packet; | ||
using SocketIOSharp.Common; | ||
using SocketIOSharp.Common.Abstract.Connection; | ||
using System; | ||
using System.Threading; | ||
|
||
namespace SocketIOSharp.Client | ||
{ | ||
public partial class SocketIOClient : SocketIOConnection | ||
public partial class SocketIOClient : SocketIOConnection<SocketIOClient> | ||
{ | ||
private readonly EngineIOClient Client; | ||
private static readonly Random Random = new Random(); | ||
private DateTime LastPing = DateTime.UtcNow; | ||
|
||
private EngineIOClient Client = null; | ||
private ulong ReconnectionAttempts = 0; | ||
|
||
public SocketIOClientOption Option { get; private set; } | ||
public bool AutoReconnect { get; set; } | ||
|
||
public override EngineIOReadyState ReadyState => Client.ReadyState; | ||
public override EngineIOReadyState ReadyState => Client?.ReadyState ?? EngineIOReadyState.CLOSED; | ||
|
||
public SocketIOClient(SocketIOClientOption Option) | ||
{ | ||
AutoReconnect = Option.AutoReconnect; | ||
UseAckTimeout = Option.UseAckTimeout; | ||
|
||
Client = new EngineIOClient(this.Option = Option); | ||
Client.OnOpen(() => | ||
{ | ||
AckManager.SetTimeout(Client.Handshake.PingTimeout); | ||
Closed = false; | ||
}); | ||
this.Option = Option; | ||
} | ||
|
||
Client.OnMessage(OnPacket); | ||
Client.OnClose(() => | ||
public SocketIOClient Connect() | ||
{ | ||
if (Client == null) | ||
{ | ||
OnDisconnect(); | ||
ReconnectionAttempts = 0; | ||
|
||
if (AutoReconnect) | ||
void Connect() | ||
{ | ||
Connect(); | ||
Client = new EngineIOClient(Option); | ||
Client.OnOpen(() => | ||
{ | ||
AckManager.SetTimeout(Client.Handshake.PingTimeout); | ||
AckManager.StartTimer(); | ||
|
||
if (ReconnectionAttempts > 0) | ||
{ | ||
CallEventHandler(Event.RECONNECT, ReconnectionAttempts); | ||
ReconnectionAttempts = 0; | ||
} | ||
}); | ||
|
||
Client.OnMessage(OnPacket); | ||
Client.OnClose((Exception) => | ||
{ | ||
OnDisconnect(Exception); | ||
|
||
if (ReconnectionAttempts == 0) | ||
{ | ||
CallEventHandler(Event.CONNECT_ERROR, new SocketIOException("Connect error", Exception).ToString()); | ||
} | ||
else | ||
{ | ||
CallEventHandler(Event.RECONNECT_ERROR, new SocketIOException("Reconnect error", Exception).ToString()); | ||
} | ||
|
||
if (Option.Reconnection && ReconnectionAttempts < Option.ReconnectionAttempts) | ||
{ | ||
ReconnectionAttempts++; | ||
|
||
ThreadPool.QueueUserWorkItem((_) => | ||
{ | ||
int Factor = (int)(Option.ReconnectionDelay * Option.RandomizationFactor); | ||
int Delay = Random.Next(Option.ReconnectionDelay - Factor, Option.ReconnectionDelay + Factor + 1); | ||
|
||
Thread.Sleep(Delay); | ||
Option.ReconnectionDelay = Math.Min(Option.ReconnectionDelayMax, Option.ReconnectionDelay * 2); | ||
|
||
CallEventHandler(Event.RECONNECT_ATTEMPT); | ||
Connect(); | ||
|
||
CallEventHandler(Event.RECONNECTING, ReconnectionAttempts); | ||
}); | ||
} | ||
else if (ReconnectionAttempts >= Option.ReconnectionAttempts) | ||
{ | ||
CallEventHandler(Event.RECONNECT_FAILED); | ||
} | ||
}); | ||
|
||
Client.On(EngineIOClient.Event.PACKET_CREATE, (Argument) => | ||
{ | ||
if ((Argument as EngineIOPacket).Type == EngineIOPacketType.PING) | ||
{ | ||
CallEventHandler(Event.PING); | ||
LastPing = DateTime.UtcNow; | ||
} | ||
}); | ||
|
||
Client.On(EngineIOClient.Event.PACKET, (Argument) => | ||
{ | ||
if ((Argument as EngineIOPacket).Type == EngineIOPacketType.PONG) | ||
{ | ||
CallEventHandler(Event.PONG, DateTime.UtcNow.Subtract(LastPing).TotalMilliseconds); | ||
} | ||
}); | ||
|
||
Client.Connect(); | ||
} | ||
}); | ||
} | ||
|
||
public void Connect() | ||
{ | ||
Client.Connect(); | ||
Connect(); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public override void Close() | ||
public override SocketIOClient Close() | ||
{ | ||
AutoReconnect = false; | ||
Client?.Close(); | ||
|
||
Client.Close(); | ||
AckManager.Dispose(); | ||
Reconstructor.Dispose(); | ||
return this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.