BaseConnector.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using UnityEngine;
  3. using Sfs2X.Core;
  4. public abstract class BaseConnector
  5. {
  6. public GardenSmartFox GardenSmartFox;
  7. public static string Zone = "Garden";
  8. public Action<BaseEvent> onConnectSucceed;
  9. public Action<BaseEvent> onConnectFailed;
  10. public Action<BaseEvent> onConnectionLost;
  11. public Action<bool, BaseEvent> onConnectResult;
  12. public BaseConnector Init(GardenSmartFox smartFox)
  13. {
  14. GardenSmartFox = smartFox;
  15. GardenSmartFox.SmartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
  16. GardenSmartFox.SmartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
  17. return this;
  18. }
  19. public abstract void Connect();
  20. public abstract void Disconnect();
  21. public void OnConnection(BaseEvent baseEvent)
  22. {
  23. if ((bool)baseEvent.Params[SFSLabel.DefaultResult])
  24. {
  25. //Debug.Log("Connect succeed");
  26. if (onConnectSucceed != null)
  27. onConnectSucceed.Invoke(baseEvent);
  28. if (onConnectResult != null)
  29. onConnectResult.Invoke(true, baseEvent);
  30. }
  31. else
  32. {
  33. //Debug.Log("Connect failed");
  34. if (onConnectFailed != null)
  35. onConnectSucceed.Invoke(baseEvent);
  36. if (onConnectResult != null)
  37. onConnectResult.Invoke(false, baseEvent);
  38. }
  39. }
  40. public void OnConnectionLost(BaseEvent baseEvent)
  41. {
  42. //Debug.Log("ConnectionLost : " + (string)baseEvent.Params["reason"]);
  43. if (onConnectionLost != null)
  44. onConnectionLost.Invoke(baseEvent);
  45. }
  46. }