BaseConnector.cs 1.8 KB

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