Exemplo n.º 1
0
        /// <summary>
        /// <para>Client performs an initial HTTP POST to obtain a SessionId (sid) assigned to a client, followed
        ///  by the heartbeat timeout, connection closing timeout, and the list of supported transports.</para>
        /// <para>The tansport and sid are required as part of the ws: transport connection</para>
        /// </summary>
        /// <param name="uri">http://localhost:3000</param>
        /// <returns>Handshake object with sid value</returns>
        /// <example>DownloadString: 13052140081337757257:15:25:websocket,htmlfile,xhr-polling,jsonp-polling</example>
        protected SocketIOHandshake requestHandshake(Uri uri)
        {
            string            value     = string.Empty;
            string            errorText = string.Empty;
            SocketIOHandshake handshake = null;

            using (WebClient client = new WebClient())
            {
                try
                {
                    value = client.DownloadString(string.Format("{0}://{1}:{2}/socket.io/1/{3}", uri.Scheme, uri.Host, uri.Port, uri.Query)); // #5 tkiley: The uri.Query is available in socket.io's handshakeData object during authorization
                    // 13052140081337757257:15:25:websocket,htmlfile,xhr-polling,jsonp-polling
                    if (string.IsNullOrEmpty(value))
                    {
                        errorText = "Did not receive handshake string from server";
                    }
                }
                catch (Exception ex)
                {
                    errorText = string.Format("Error getting handsake from Socket.IO host instance: {0}", ex.Message);
                    //this.OnErrorEvent(this, new ErrorEventArgs(errMsg));
                }
            }
            if (string.IsNullOrEmpty(errorText))
            {
                handshake = SocketIOHandshake.LoadFromString(value);
            }
            else
            {
                handshake = new SocketIOHandshake();
                handshake.ErrorMessage = errorText;
            }

            return(handshake);
        }
Exemplo n.º 2
0
        public static SocketIOHandshake LoadFromString(string value)
        {
            SocketIOHandshake returnItem = new SocketIOHandshake();

            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))
                    {
                        var 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);
        }