BaseConnector.cs 1.7 KB

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