using System; using UnityEngine; using Sfs2X.Core; public abstract class BaseConnector { public GardenSmartFox GardenSmartFox; public int TcpPort = 9933; public string Host = "121.199.20.79"; //public string Host = "127.0.0.1"; public Action onConnectSucceed; public Action onConnectFailed; public Action onConnectionLost; public Action onConnectResult; public BaseConnector Init(GardenSmartFox smartFox) { GardenSmartFox = smartFox; GardenSmartFox.SmartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection); GardenSmartFox.SmartFox.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost); return this; } public abstract void Connect(); public abstract void Disconnect(); public void OnConnection(BaseEvent baseEvent) { if ((bool)baseEvent.Params["success"]) { Debug.Log("Connect succeed"); if (onConnectSucceed != null) onConnectSucceed.Invoke(baseEvent); if (onConnectResult != null) onConnectResult.Invoke(true, baseEvent); } else { Debug.Log("Connect failed"); if (onConnectFailed != null) onConnectSucceed.Invoke(baseEvent); if (onConnectResult != null) onConnectResult.Invoke(false, baseEvent); } } public void OnConnectionLost(BaseEvent baseEvent) { Debug.Log("ConnectionLost : " + (string)baseEvent.Params["reason"]); if (onConnectionLost != null) onConnectionLost.Invoke(baseEvent); } }