/// <summary> /// 连接服务器 /// </summary> /// <param name="ip"></param> /// <param name="port"></param> /// <param name="callBack">回调函数,成功/失败都回调</param> public static void Connect(string ip, int port, Action <int> callBack) { if ("".Equals(ip) || ip == null) { if (GLog.IsLogInfoEnabled) { GLog.LogInfo("ClientSocket Connect step 1"); } return; } NetReceiver.Init(); SocketManager.Instance.Connect(ip, port, callBack); }
/// <summary> /// 注册服务器推送回调 /// byte[] /// </summary> /// <param name="protocallType"></param> /// <param name="handler"></param> public static void AddCallBackByte(uint protocallType, Action <byte[]> handler) { NetReceiver.AddHandlerByte(protocallType, handler); }
/// <summary> /// 注册服务器推送回调 /// </summary> /// <param name="protocallType"></param> /// <param name="handler"></param> public static void AddCallBack(uint protocallType, Action <string> handler) { NetReceiver.AddHandler(protocallType, handler); }
/// <summary> /// 断开连接 /// </summary> public static void DisConnect() { SocketManager.Instance.Close(); NetReceiver.Destroy(); }
/// <summary> /// 删除对应的回调 /// </summary> /// <param name="protocallType"></param> public static void RemoveCallBack(uint protocallType) { NetReceiver.RemoveHandler(protocallType); }
private void DispatchRecvQueue(ConcurrentQueue <TmpSocketData> recvQueue) { if (recvQueue == null) { return; } bool isShowLog = false; #if UNITY_EDITOR isShowLog = true; #endif try { while (recvQueue.Count > 0) { TmpSocketData data; if (!recvQueue.TryDequeue(out data)) { continue; } if (data.protocallType < 10000) { Action <string> handler = NetReceiver.GetHandler(data.protocallType); string content = Encoding.UTF8.GetString(data.data); if (handler != null) { if (isShowLog && GLog.IsLogInfoEnabled) { GLog.LogInfo("protocallType = " + data.protocallType + ", content = " + content); } handler(content); continue; } if (isShowLog && GLog.IsLogErrorEnabled) { GLog.LogError("The (" + data.protocallType + ") do not have handler!"); } continue; } else { Action <byte[]> handler = NetReceiver.GetHandlerByte(data.protocallType); if (handler != null) { if (isShowLog && GLog.IsLogInfoEnabled) { GLog.LogInfo("protocallType = " + data.protocallType); } handler(data.data); continue; } if (isShowLog && GLog.IsLogErrorEnabled) { GLog.LogError("The (" + data.protocallType + ") do not have handler!"); } } } } catch (Exception e) { GLog.LogException("DispatchRecvQueue ==> " + e.Message); } }