Пример #1
0
        public static CLHandshake LoadFromString(string value)
        {
            CLHandshake returnItem = new CLHandshake();

            if (!string.IsNullOrEmpty(value))
            {
                string[] items = value.Split(new char[] { ':' });
                if (items.Count() == 4)
                {
                    int hb = 0;
                    int ct = 0;
                    returnItem.SID = items[0];
                    if (int.TryParse(items[1], out hb))
                    {
                        int pct = (int)(hb * .75); // setup client time to occure 25% faster than needed
                        returnItem.HeartbeatTimeout = pct;
                    }
                    if (int.TryParse(items[2], out ct))
                    {
                        returnItem.ConnectionTimeout = ct;
                    }
                    returnItem.Transports.AddRange(items[3].Split(new char[] { ',' }));
                    return(returnItem);
                }
            }
            return(null);
        }
Пример #2
0
        private CLHandshake RequestHandshake(Uri uri)
        {
            string      value     = string.Empty;
            string      errorText = string.Empty;
            CLHandshake handshake = null;

            using (WebClient client = new WebClient())
            {
                value = client.DownloadString(string.Format("{0}://{1}:{2}/socket.io/1/{3}", uri.Scheme, uri.Host, uri.Port, uri.Query));
                if (string.IsNullOrEmpty(value))
                {
                    errorText = "Did not receive handshake string from server";
                }
            }
            if (string.IsNullOrEmpty(errorText))
            {
                handshake = CLHandshake.LoadFromString(value);
            }
            else
            {
                handshake = new CLHandshake();
                handshake.ErrorMessage = errorText;
            }
            return(handshake);
        }
Пример #3
0
        private void Connect()
        {
            if (!(this.m_ReadyState == WebSocketState.Connecting || this.m_ReadyState == WebSocketState.Open))
            {
                try
                {
                    m_Handshake = RequestHandshake(m_Uri);
                    if (m_Handshake == null || m_Handshake.SID.Trim() == string.Empty || m_Handshake.HadError)
                    {
                        Console.WriteLine("Failed to request socketio handshake ");
                    }
                    else
                    {
                        string wsScheme = (m_Uri.Scheme == Uri.UriSchemeHttps ? "wss" : "ws");
                        m_WebsocketClient = new WebSocket(
                            string.Format("{0}://{1}:{2}/socket.io/1/websocket/{3}", wsScheme, m_Uri.Host, m_Uri.Port, m_Handshake.SID),
                            string.Empty,
                            m_SocketVersion);
                        m_WebsocketClient.EnableAutoSendPing = false;

                        m_WebsocketClient.add_Opened(new EventHandler <EventArgs>(webSocketClient_Opened));
                        m_WebsocketClient.add_Closed(new EventHandler <EventArgs>(webSocketClient_Closed));
                        m_WebsocketClient.add_Error(new EventHandler <ErrorEventArgs>(webSocketClient_Error));
                        m_WebsocketClient.add_MessageReceived(new EventHandler <MessageReceivedEventArgs>(webSocketClient_MessageReceived));

                        /*
                         * m_WebsocketClient.Opened += this.webSocketClient_Opened;
                         * m_WebsocketClient.MessageReceived += this.webSocketClient_MessageReceived;
                         * m_WebsocketClient.Closed += webSocketClient_Closed;
                         * m_WebsocketClient.Error += this.webSocketClient_Error;
                         */
                        m_WebsocketClient.Open();
                        Console.WriteLine("Try to Open WebSocket Client ... {0}", DateTime.Now.ToString());
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Websocket connect error : " + ex.ToString());
                }
            }
        }