Exemple #1
0
    /// <summary>
    /// 连接到服务器
    /// </summary>
    /// <param name="ip">服务器IP</param>
    /// <returns></returns>
    public void Connect()
    {
        AddressFamily family = AddressFamily.InterNetwork;

        //ipv4转ipv6 - 苹果ios
                #if !UNITY_EDITOR && UNITY_IPHONE
        APIS.socketUrl = IOSIpv6.GetIPv6Str(APIS.socketUrl, APIS.socketPort.ToString(), out family);
                #endif

        try
        {
            tcpclient = new TcpClient(family);
            //防止延迟,即时发送!
            tcpclient.NoDelay = true;
            tcpclient.BeginConnect(IPAddress.Parse(APIS.socketUrl), APIS.socketPort, new AsyncCallback(ConnectCallback), tcpclient);
            MyDebug.Log(" APIS.socketUrl:" + APIS.socketUrl);
        }
        catch (Exception ex)
        {
            //设置标志,连接服务端失败!
            showMessageTip("服务器断开连接,请重新运行程序或稍后再试");
            MyDebug.Log("----------------Connect------------------------Exception----------------");
            //	ReConnectScript.getInstance().ReConnectToServer();
            Debug.Log(ex.ToString());
            isConnected = false;
        }
    }
Exemple #2
0
    /// <summary>
    /// 连接到服务器
    /// </summary>
    /// <param name="ip">服务器IP</param>
    /// <returns></returns>
    public void Connect()
    {
        AddressFamily family = AddressFamily.InterNetwork;

        //ipv4转ipv6 - 苹果ios
                #if !UNITY_EDITOR && UNITY_IPHONE
        APIS.chatSocketUrl = IOSIpv6.GetIPv6Str(APIS.chatSocketUrl, APIS.chatPort.ToString(), out family);
                #endif

        try
        {
            tcpclient = new TcpClient(family);
            //防止延迟,即时发送!
            tcpclient.NoDelay = true;
            tcpclient.BeginConnect(APIS.chatSocketUrl, APIS.chatPort, new AsyncCallback(ConnectCallback), tcpclient);
            MyDebug.Log("--chatSocketUrl-------开始连接-----" + APIS.chatSocketUrl + "------" + 10112);
        }
        catch (Exception ex)
        {
            //设置标志,连接服务端失败!
            Connect();
            Debug.Log(ex.ToString());
            MyDebug.Log("--chatSocketUrl-------连接异常-----");
        }
    }