コード例 #1
0
 /// <summary>关闭Tcp</summary>
 public void Close()
 {
     if (client != null && client.session != null)
     {
         client.session.Clear();
         client.Close();
         client = null;
     }
     if (server != null && server.session != null)
     {
         server.session.Clear();
         server.Close();
         server = null;
     }
     TimerManager.Instance.EndTimer(TIME_ID);
 }
コード例 #2
0
    /// <summary>服务端网络初始化</summary>
    public void InitServerNet(string ip, int port, Action <bool> cb = null)
    {
        TIME_ID = TimerManager.Instance.StartTimer(Update);
        server  = new SocketTcp <SessionTcp>();
        #region 网络日志
        //日志是否开启、日志的回调函数(内容,级别) 覆盖unity日志系统 查看网络错误
        server.SetLog(true, (string msg, int lv) =>
        {
            if (this == null)
            {
                return;
            }
            switch (lv)
            {
            case 0:    //普通
                msg = "Log:" + msg;
                Debug.Log(msg);
                break;

            case 1:    //警告
                msg = "Warn:" + msg;
                Debug.LogWarning(msg);
                break;

            case 2:    //错误
                msg = "Error:" + msg;
                Debug.LogError(msg);
                break;

            case 3:    //信息
                msg = "Info:" + msg;
                Debug.Log(msg);
                break;
            }
        });
        #endregion
        server.StartAsServer(ip, port, cb);//开启服务端
    }
コード例 #3
0
    /// <summary>网络服务初始化</summary>
    public void InitClientNet(string ip, int port, Action <bool> cb = null)
    {
        client = new SocketTcp <SessionTcp>();
        #region 网络日志
        //日志是否开启、日志的回调函数(内容,级别) 覆盖unity日志系统 查看网络错误
        client.SetLog(true, (string msg, int lv) =>
        {
            if (this == null)
            {
                return;
            }
            switch (lv)
            {
            case 0:    //普通
                msg = "Log:" + msg;
                Debug.Log(msg);
                break;

            case 1:    //警告
                msg = "Warn:" + msg;
                Debug.LogWarning(msg);
                break;

            case 2:    //错误
                msg = "Error:" + msg;
                Debug.LogError(msg);
                break;

            case 3:    //信息
                msg = "Info:" + msg;
                Debug.Log(msg);
                break;
            }
        });
        #endregion
        client.StartAsClient_IP(ip, port, cb);//开启客户端
    }