BaseConnector.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 = "GardenTest";
  11. public Action<BaseEvent> onConnectSucceed;
  12. public Action<BaseEvent> onConnectFailed;
  13. public Action<BaseEvent> onConnectionLost;
  14. public Action<bool, BaseEvent> onConnectResult;
  15. public BaseConnector Init(GardenSmartFox smartFox)
  16. {
  17. GardenSmartFox = smartFox;
  18. GardenSmartFox.SmartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
  19. GardenSmartFox.SmartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
  20. return this;
  21. }
  22. public abstract void Connect();
  23. public abstract void Disconnect();
  24. public void OnConnection(BaseEvent baseEvent)
  25. {
  26. if ((bool)baseEvent.Params["success"])
  27. {
  28. //Debug.Log("Connect succeed");
  29. if (onConnectSucceed != null)
  30. onConnectSucceed.Invoke(baseEvent);
  31. if (onConnectResult != null)
  32. onConnectResult.Invoke(true, baseEvent);
  33. }
  34. else
  35. {
  36. //Debug.Log("Connect failed");
  37. if (onConnectFailed != null)
  38. onConnectSucceed.Invoke(baseEvent);
  39. if (onConnectResult != null)
  40. onConnectResult.Invoke(false, baseEvent);
  41. }
  42. }
  43. public void OnConnectionLost(BaseEvent baseEvent)
  44. {
  45. //Debug.Log("ConnectionLost : " + (string)baseEvent.Params["reason"]);
  46. if (onConnectionLost != null)
  47. onConnectionLost.Invoke(baseEvent);
  48. }
  49. }