Пример #1
0
    IEnumerator OnWebGet2()
    {
        UnityWebRequest www = UnityWebRequest.Get(LoginInfo.Instance().mylogindata.URL +
                                                  "room-end?"
                                                  + "user_id=" + LoginInfo.Instance().mylogindata.user_id
                                                  + "&game_id=" + LoginInfo.Instance().mylogindata.choosegame);

        yield return(www.Send());

        if (www.error == null)
        {
            JsonData jd;
            try
            {
                jd = JsonMapper.ToObject(www.downloadHandler.text);
                if (jd["code"].ToString() == "200")
                {
                    NewTcpNet.IsKick = true;
                    if (NewTcpNet.instance != null)
                    {
                        NewTcpNet.GetInstance().SocketQuit();
                    }
                    DisconnectPanel.GetInstance().Show();
                    DisconnectPanel.GetInstance().Modification("", "长时间未操作,你已被移除房间");
                }
            }
            catch
            {
            }
        }
    }
Пример #2
0
    /// <summary>
    /// 开启服务
    /// </summary>
    /// <param name="data"></param>
    public NewTcpNet()
    {
        isPlayOver = false;
        //loom = Loom.Current;
        //获得文本框中的IP地址对象
        IPAddress address = IPAddress.Parse(ip);
        //创建包含IP和端口的网络节点对象
        IPEndPoint endpoint = new IPEndPoint(address, prot);

        //创建客户端套接字,负责连接服务器
        socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        _loom      = new GameObject().AddComponent <Loom>();
        _loom.name = "Loom";


        try
        {
            //客户端连接到服务器
            socketClient.Connect(endpoint);
            IsKick = false;
            IsQuit = false;
        }
        catch (SocketException ex)
        {
            Debug.Log("客户端连接服务器发生异常:" + ex.Message);
            DisconnectPanel.GetInstance().Show();
            DisconnectPanel.GetInstance().Modification("", "连接服务器失败!!");
        }
        catch (Exception ex)
        {
            Debug.Log("客户端连接服务器发生异常:" + ex.Message);
            DisconnectPanel.GetInstance().Show();
            DisconnectPanel.GetInstance().Modification("", "连接服务器失败!!");
        }
        Loom.RunAsync(
            () =>
        {
            threadClient = new Thread(ReceiveMsg /*ReceiveMessage*/); //将ReceiveMsg传入
            threadClient.IsBackground = true;                         //设置threadClient变量IsBackground为true
            threadClient.Start();                                     //调用Start方法
        }
            );


        OnLogin login = new OnLogin("Login", LoginInfo.Instance().mylogindata.user_id, LoginInfo.Instance().mylogindata.room_id.ToString(), LoginInfo.Instance().mylogindata.choosegame.ToString());
        string  str   = JsonMapper.ToJson(login);

        SendMessage(str);
        //调用SendMessage发送消息到服务器
    }
Пример #3
0
    //接收服务器返回的消息
    void ReceiveMsg()
    {
        while (true)
        {
            //定义一个接收消息用的字节数组缓冲区(2M大小)
            byte[] arrMsgRev = new byte[8192];
            //将接收到的数据存入arrMsgRev,并返回真正接收到数据的长度
            int length = -1;
            try
            {
                length = socketClient.Receive(arrMsgRev);
            }
            catch (SocketException ex)
            {
                //System.Console.WriteLine("客户端接收消息时发生异常:" + ex.Message);

//				socketClient.Shutdown (SocketShutdown.Both);
//				socketClient.Close ();
                if (!IsKick && !IsQuit)
                {
                    Loom.QueueOnMainThread
                    (
                        () => {
                        //不是被踢 执行显示
                        Debug.Log("接收异常" + ex.ToString());
                        DisconnectPanel.GetInstance().Show();
                        DisconnectPanel.GetInstance().Modification("", "与服务器断开连接");
                    }
                    );
                }
                break;
            }
            catch (Exception ex)
            {
//                System.Console.WriteLine("客户端接收消息时发生异常:" + ex.Message);

                if (!IsKick && !IsQuit)
                {
                    Loom.QueueOnMainThread
                    (
                        () => {
                        //不是被踢 执行显示
                        Debug.Log("接收异常" + ex.ToString());
                        DisconnectPanel.GetInstance().Show();
                        DisconnectPanel.GetInstance().Modification("", "与服务器断开连接");
                    }
                    );
                }

                break;
            }

            //此时是将数组的所有元素(每个字节)都转成字符串,而真正接收到只有服务端发来的几个字符
            string strMsgReceive = Encoding.UTF8.GetString(arrMsgRev, 0, length);
            //Debug.Log("接收的原消息格式:"+strMsgReceive);
            //Debug.LogError(strMsgReceive);
            //DanTiao.instance.MessageManage(strMsgReceive);
            string[] newStr = strMsgReceive.Split(new string[] { "xx==" }, StringSplitOptions.RemoveEmptyEntries);
            //Debug.LogError(newStr.Length);

            for (int i = 0; i < newStr.Length; i++)
            {
                Debug.Log("接收到的数据为:" + newStr[i]);



                try
                {
                    JsonData jd = JsonMapper.ToObject(newStr[i]);
                    LoginData.IsLogin  = true;
                    LoginData.OverTime = 0;
                }
                catch (Exception)
                {
                    LoginData.IsLogin  = true;
                    LoginData.OverTime = 2;
                }

                Message(newStr[i]);
            }
        }
    }