Ejemplo 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);
        }
Ejemplo 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;
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public static SocketIOHandshake LoadFromJsonString(string value)
        {
            bool allOk = true;
            SocketIOHandshake returnItem = new SocketIOHandshake();

            if (!string.IsNullOrEmpty(value))
            {
                Dictionary <string, object> jsonDict = SimpleJson.SimpleJson.DeserializeObject <Dictionary <string, object> >(value);
                if (jsonDict == null)
                {
                    return(null);
                }
                //  {"sid":"DS7Rday6ZClMHIH8AAAB","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}


                if (jsonDict.ContainsKey("sid"))
                {
                    returnItem.SID = (string)jsonDict["sid"];
                }
                else
                {
                    allOk = false;
                }

                if (jsonDict.ContainsKey("pingInterval"))
                {
                    var pct = (int)((int)jsonDict["pingInterval"] * .75);                      // setup client time to occure 25% faster than needed
                    returnItem.HeartbeatTimeout = pct;
                }
                else
                {
                    allOk = false;
                }

                if (jsonDict.ContainsKey("pingTimeout"))
                {
                    returnItem.HeartbeatTimeout = (int)jsonDict["pingTimeout"];
                }
                else
                {
                    allOk = false;
                }
            }
            return(allOk ? returnItem : null);
        }
Ejemplo n.º 5
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



                {
                    var url = string.Format("{0}://{1}:{2}/socket.io/{3}", uri.Scheme, uri.Host, uri.Port, uri.Query);
                    client.Headers.Add("Upgrade", "websocket");
                    client.Headers.Add("Connection", "Upgrade");
                    client.Headers.Add("Sec-Websocket-Version", "13");
                    client.Headers.Add("Sec-WebSocket-Key", "Lp2qSYxx3lHnGHdwFyHKQA==");
                    value = client.DownloadString(url); // #5 tkiley: The uri.Query is available in socket.io's handshakeData object during authorization
                    //value = client.ResponseHeaders.Get("Sec-WebSocket-Accept");                                // 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);
        }
Ejemplo n.º 6
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;
		}
Ejemplo n.º 7
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 void requestHandshake(Uri uri, Action<SocketIOHandshake> callback)
        {
            string value = string.Empty;
            string errorText = string.Empty;
            SocketIOHandshake handshake = null;

            UnityHTTP.Request request = new UnityHTTP.Request("get", string.Format("{0}://{1}:{2}/socket.io/1/{3}", uri.Scheme, uri.Host, uri.Port, uri.Query));
            request.Send(req => {
                if (request.response != null) {
                    value = request.response.Text;
                }
                if (string.IsNullOrEmpty(value))
                    errorText = "Did not receive handshake string from server";
                if (string.IsNullOrEmpty(errorText))
                    handshake = SocketIOHandshake.LoadFromString(value);
                else
                {
                    handshake = new SocketIOHandshake();
                    handshake.ErrorMessage = errorText;
                }
                callback(handshake);
            });
        }
Ejemplo n.º 8
0
    private void HandshakeCallback(object sender, DownloadStringCompletedEventArgs eventArgs){
      SocketIOHandshake handshake;
      if (eventArgs.Error == null)
        handshake = SocketIOHandshake.LoadFromString(eventArgs.Result);
      else
      {
        handshake = new SocketIOHandshake();
        handshake.ErrorMessage = eventArgs.Error.Message;
      }
      this.HandShake = handshake;

      if (this.HandShake == null || string.IsNullOrEmpty(this.HandShake.SID) || this.HandShake.HadError)
      {
        this.LastErrorMessage = string.Format("Error initializing handshake with {0}", uri.ToString());
        this.OnErrorEvent(this, new ErrorEventArgs(this.LastErrorMessage, new Exception()));
      }
      else
      {
        string wsScheme = (uri.Scheme == Uri.UriSchemeHttps ? "wss" : "ws");
        string url = string.Format("{0}://{1}:{2}/socket.io/1/websocket/{3}", wsScheme, uri.Host, uri.Port, this.HandShake.SID);
        this.wsClient = new WebSocket(url);
        this.wsClient.OnOpen += this.wsClient_OpenEvent;
        this.wsClient.OnMessage += this.wsClient_MessageReceived;
        this.wsClient.OnError += this.wsClient_Error;
        this.wsClient.OnClose += wsClient_Closed;

        this.wsClient.Connect();
      }
    }