Skip to content

Commit

Permalink
add handler for try connect event (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
vu-luong authored Aug 3, 2023
1 parent c4a91f0 commit c6e81d4
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions support/EzySocketProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class EzySocketProxy
private readonly IDictionary<Object, EventHandler> disconnectedHandlers;
private readonly IDictionary<Object, EventHandler> reconnectingHandlers;
private readonly IDictionary<Object, EventHandler> pingLostHandlers;
private readonly IDictionary<Object, EventHandler> tryConnectHandlers;

public EzySocketProxy(
String zoneName,
Expand All @@ -60,6 +61,7 @@ EzyAppFactory appFactory
this.disconnectedHandlers = new Dictionary<Object, EventHandler>();
this.reconnectingHandlers = new Dictionary<Object, EventHandler>();
this.pingLostHandlers = new Dictionary<Object, EventHandler>();
this.tryConnectHandlers = new Dictionary<Object, EventHandler>();
}

public EzySocketProxy setHost(String host)
Expand Down Expand Up @@ -195,6 +197,7 @@ private void init()
client.setup()
.addEventHandler(EzyEventType.DISCONNECTION, new DisconnectionHandler(this))
.addEventHandler(EzyEventType.LOST_PING, new PingLostHandler(this))
.addEventHandler(EzyEventType.TRY_CONNECT, new TryConnectHandler(this))
.addDataHandler(EzyCommand.HANDSHAKE, new HandshakeEventHandler(this))
.addDataHandler(EzyCommand.LOGIN, new LoginSuccessHandler(this))
.addDataHandler(EzyCommand.LOGIN_ERROR, new LoginErrorHandler(this))
Expand Down Expand Up @@ -303,6 +306,18 @@ EzySocketProxyEventHandler<EzyLostPingEvent> handler
return handler;
}

public Object onTryConnect(
EzySocketProxyEventHandler<EzyTryConnectEvent> handler
)
{
EventHandler dataHandler = evt =>
{
handler.Invoke(this, (EzyTryConnectEvent)evt);
};
tryConnectHandlers[handler] = dataHandler;
return handler;
}

public void unbind(Object handler)
{
loginSuccessHandlers.Remove(handler);
Expand All @@ -312,6 +327,7 @@ public void unbind(Object handler)
disconnectedHandlers.Remove(handler);
reconnectingHandlers.Remove(handler);
pingLostHandlers.Remove(handler);
tryConnectHandlers.Remove(handler);
}

public class HandshakeEventHandler : EzyHandshakeHandler
Expand Down Expand Up @@ -508,6 +524,24 @@ protected override void process(EzyLostPingEvent evt)
}
}
}

public class TryConnectHandler : EzyAbstractEventHandler<EzyTryConnectEvent>
{
private readonly EzySocketProxy parent;

public TryConnectHandler(EzySocketProxy parent)
{
this.parent = parent;
}

protected override void process(EzyTryConnectEvent evt)
{
foreach (EventHandler handler in parent.tryConnectHandlers.Values)
{
handler.Invoke(evt);
}
}
}
}
}

Expand Down

0 comments on commit c6e81d4

Please sign in to comment.