//用户断开连接,(外部)手动断开连接 public virtual void DisConnect() { m_eStatus = SocketClientStatus.DisConnect; m_connectCallback = null; m_bIsDisConnect = false; m_fStartConnectTime = 0; }
public SocketClient() { m_bIsDisConnect = false; m_eStatus = SocketClientStatus.DisConnect; m_fStartConnectTime = 0; TimeOut = 10f; }
//主线程中 public virtual void OnUpdate() { if (m_eStatus == SocketClientStatus.Connecting) { //连接超时(直接断开连接) if (UnityEngine.Time.time - m_fStartConnectTime > TimeOut) { if (null != m_connectCallback) { m_connectCallback(false); m_connectCallback = null; } DisConnect(); } else { SocketClientStatus s = SocketConnectingStatus(); if (s == SocketClientStatus.Connected) { m_eStatus = SocketClientStatus.Connected; if (null != m_connectCallback) { m_connectCallback(true); m_connectCallback = null; } } else if (s == SocketClientStatus.DisConnect) { if (null != m_connectCallback) { m_connectCallback(false); m_connectCallback = null; } DisConnect(); } } } if (m_bIsDisConnect) { if (null != OnDisConnect) { OnDisConnect(); } DisConnect(); } }
//开始连接 public bool BeginConnect(string ip, int port, Action <bool> callback) { if (m_eStatus != SocketClientStatus.DisConnect) { CLog.LogError("socket client is not disConnect,can not beginConnect!"); return(false); } DisConnect(); m_eStatus = SocketClientStatus.Connecting; m_fStartConnectTime = UnityEngine.Time.time; this.m_connectCallback = callback; bool succ = BeginConnect(ip, port); if (!succ) { if (null != this.m_connectCallback) { this.m_connectCallback(false); this.m_connectCallback = null; } } return(succ); }