From c6e81d42e4356dca1e77e31d7c6773ee2b0839ed Mon Sep 17 00:00:00 2001 From: Vu Luong Date: Thu, 3 Aug 2023 23:07:53 +1000 Subject: [PATCH] add handler for try connect event (#33) --- support/EzySocketProxy.cs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/support/EzySocketProxy.cs b/support/EzySocketProxy.cs index acbf2d9..7d23628 100644 --- a/support/EzySocketProxy.cs +++ b/support/EzySocketProxy.cs @@ -40,6 +40,7 @@ public class EzySocketProxy private readonly IDictionary disconnectedHandlers; private readonly IDictionary reconnectingHandlers; private readonly IDictionary pingLostHandlers; + private readonly IDictionary tryConnectHandlers; public EzySocketProxy( String zoneName, @@ -60,6 +61,7 @@ EzyAppFactory appFactory this.disconnectedHandlers = new Dictionary(); this.reconnectingHandlers = new Dictionary(); this.pingLostHandlers = new Dictionary(); + this.tryConnectHandlers = new Dictionary(); } public EzySocketProxy setHost(String host) @@ -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)) @@ -303,6 +306,18 @@ EzySocketProxyEventHandler handler return handler; } + public Object onTryConnect( + EzySocketProxyEventHandler handler + ) + { + EventHandler dataHandler = evt => + { + handler.Invoke(this, (EzyTryConnectEvent)evt); + }; + tryConnectHandlers[handler] = dataHandler; + return handler; + } + public void unbind(Object handler) { loginSuccessHandlers.Remove(handler); @@ -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 @@ -508,6 +524,24 @@ protected override void process(EzyLostPingEvent evt) } } } + + public class TryConnectHandler : EzyAbstractEventHandler + { + 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); + } + } + } } }