Beispiel #1
0
        public override void Connect(Uri uri)
        {
            IPAddress addr = IPAddress.Parse(uri.Host);

            if (Application.platform != RuntimePlatform.WebGLPlayer)
            {
                if (IPTool.IOSCheck(addr, out addr))
                {
                    string ipv6UriString = string.Format("ws://[{0}]:{1}/WebSocket", addr.ToString(), uri.Port);
                    uri = new Uri(ipv6UriString);
                }
            }

            mWebSocket = new WebSocket(uri);

            mWebSocket.StartPingThread = true;
            mWebSocket.OnOpen         += webSocket =>
            {
                UnityEngine.Debug.Log("WebSocket isOpen= " + webSocket.IsOpen);
                if (webSocket.IsOpen)
                {
                    onCompleteConnect(null);
                }
            };
            mWebSocket.OnBinary += (webSocket, message) =>
            {
                fireCompleteReadFromServerStream(message);
                //UnityEngine.Debug.Log("Binary Message received from server. Length: " + message.Length);
            };
            mWebSocket.OnError += (ws, ex) =>
            {
                string errorMsg = string.Empty;
                fireCompleteDisconnect();
                mWebSocket.Close();
                UnityEngine.Debug.Log("An error occured: " + (ex != null ? ex.Message : "Unknown: " + errorMsg));
                System.Console.WriteLine("MYLOG = Error:" + ex.Message);
            };
            mWebSocket.OnClosed += (webSocket, code, message) =>
            {
                fireCompleteDisconnect();
                UnityEngine.Debug.Log("WebSocket Closed!");
                System.Console.WriteLine("MYLOG = Client Close");
            };
            mWebSocket.Open();
        }
Beispiel #2
0
        public override void Connect(Uri uri)
        {
            SetFireDisconnectTrigger();
            IPAddress addr = IPAddress.Parse(uri.Host);

            if (Application.platform != RuntimePlatform.WebGLPlayer)
            {
                if (IPTool.IOSCheck(addr, out addr))
                {
                    string ipv6UriString = string.Format("ws://[{0}]:{1}/WebSocket", addr.ToString(), uri.Port);
                    uri = new Uri(ipv6UriString);
                }
            }

            mWebSocket = new WebSocket(uri);

            mWebSocket.StartPingThread = true;
            mWebSocket.OnOpen         += webSocket =>
            {
                LogProxy.WriteLine("WebSocket isOpen= " + webSocket.IsOpen);
                if (webSocket.IsOpen)
                {
                    onCompleteConnect(null);
                }
            };
            mWebSocket.OnBinary += (webSocket, message) =>
            {
                fireCompleteReadFromServerStream(message);
            };
            mWebSocket.OnError += (WebSocket webSocket, Exception ex) =>
            {
                LogProxy.WriteLine("OnError");
                webSocket.Close();
                CheckAndFireDisconnect();
                //Debug.LogError(ex.Message);
            };
            mWebSocket.OnClosed += (webSocket, code, message) =>
            {
                LogProxy.WriteLine("OnClose");
                CheckAndFireDisconnect();
            };
            mWebSocket.Open();
        }
Beispiel #3
0
        public override void Connect(IPAddress addr, int port)
        {
            LogProxy.WriteLine("<color=darkblue>Udp.Connect=>addr=" + addr + ",port=" + port + "</color>");

            if ((maintainConnecting == null) || !maintainConnecting.Enabled)
            {
                udpClient = new UdpClient(AddressFamily.InterNetworkV6);
                Socket newSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)
                {
                    DualMode = true
                };
                udpClient.Client = newSocket;

                if (!IPTool.IOSCheck(addr, out addr))
                {
                    addr = addr.MapToIPv6();
                }
                serverIPEndPoint = new IPEndPoint(addr.MapToIPv6(), port);

                udpClient.Send(ReqConnect, ReqConnect.Length, serverIPEndPoint);
                udpClient.BeginReceive(onCompleteConnect, udpClient);
            }
        }