public void Setup()
      {
         factory = A.Fake<IConnectionFactory>();
         fakeHttp = A.Fake<ISimpleHttpGetRequest>();
         fakeWebSocket = A.Fake<IWebSocket>();
         heartBeatSignaler = A.Fake<IHeartBeatSignaler>();

         A.CallTo(() => factory.CreateHttpRequest(A<string>._)).Returns(fakeHttp);
         A.CallTo(() => fakeHttp.Execute()).ReturnsLazily(() => handshakeResponse);
         A.CallTo(() => factory.CreateWebSocket(A<string>._)).Returns(fakeWebSocket);
         A.CallTo(() => fakeWebSocket.Connected).ReturnsLazily(() => isConnected);

         A.CallTo(() => fakeWebSocket.Open()).Invokes(() =>
         {
            isConnected = true;
            fakeWebSocket.Opened += Raise.With(fakeWebSocket, EventArgs.Empty).Now;
         });

         A.CallTo(() => fakeWebSocket.Close()).Invokes(() =>
         {
            isConnected = false;
            fakeWebSocket.Closed += Raise.With(fakeWebSocket, EventArgs.Empty).Now;
         });

         io = new SocketIOClient(factory, heartBeatSignaler);

         socket = io.Connect("http://localhost:3000");
      }
        public void Setup()
        {
            factory           = A.Fake <IConnectionFactory>();
            fakeHttp          = A.Fake <ISimpleHttpGetRequest>();
            fakeWebSocket     = A.Fake <IWebSocket>();
            heartBeatSignaler = A.Fake <IHeartBeatSignaler>();

            A.CallTo(() => factory.CreateHttpRequest(A <string> ._)).Returns(fakeHttp);
            A.CallTo(() => fakeHttp.Execute()).ReturnsLazily(() => handshakeResponse);
            A.CallTo(() => factory.CreateWebSocket(A <string> ._)).Returns(fakeWebSocket);
            A.CallTo(() => fakeWebSocket.Connected).ReturnsLazily(() => isConnected);

            A.CallTo(() => fakeWebSocket.Open()).Invokes(() =>
            {
                isConnected           = true;
                fakeWebSocket.Opened += Raise.With(fakeWebSocket, EventArgs.Empty).Now;
            });

            A.CallTo(() => fakeWebSocket.Close()).Invokes(() =>
            {
                isConnected           = false;
                fakeWebSocket.Closed += Raise.With(fakeWebSocket, EventArgs.Empty).Now;
            });

            io = new SocketIOClient(factory, heartBeatSignaler);

            socket = io.Connect("http://localhost:3000");
        }
        private HandshakeResult DoHandshake(Uri uri, string resource)
        {
            string responseText = null;

            try
            {
                var query = uri.Query + (string.IsNullOrEmpty(uri.Query) ? "?" : "&") +
                            "t=" + Math.Round((DateTimeOffset.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds, 0);

                var handshakeUrl = string.Format("{0}://{1}:{2}/{4}/1/{3}", uri.Scheme, uri.Host, uri.Port, query, resource);

                responseText = m_connectionFactory.CreateHttpRequest(handshakeUrl).Execute();

                var resultParts = responseText.Split(new[] { ':' });

                Id = resultParts[0];
                HeartbeatTimeout = Int32.Parse(resultParts[1]) * 1000;
            }
            catch (WebException we)
            {
                if (we.Status == WebExceptionStatus.ProtocolError)
                {
                    var resp = we.Response as HttpWebResponse;

                    if (resp != null && resp.StatusCode == HttpStatusCode.Forbidden)
                    {
                        return(HandshakeResult.Forbidden);
                    }
                }

                return(HandshakeResult.Error);
            }
            catch (Exception)
            {
                EmitLocally("error", responseText);
                return(HandshakeResult.Error);
            }

            return(HandshakeResult.Success);
        }