Пример #1
0
        public void ClientSendDataConcurrent()
        {
            _forever = true;
            if (_client.Connected)
            {
                var client2 = new WebSocketClient("ws://127.0.0.1:54321/path")
                {
                    OnReceive = OnClientReceive
                };
                client2.Connect();

                if (client2.Connected)
                {
                    _client.Send("Test");
                    client2.Send("Test");
                }
                else
                {
                    _clientDataPass = false;
                }
                Thread.Sleep(5000);
            }
            else
            {
                _clientDataPass = false;
            }
            Assert.IsTrue(_clientDataPass);
        }
        public void ClientSendDataConcurrent()
        {
            _forever = true;
            if (_client.Connected)
            {
                var client2 = new WebSocketClient("ws://127.0.0.1:54321/path") { OnReceive = OnClientReceive };
                client2.Connect();

                if (client2.Connected)
                {
                    _client.Send("Test");
                    client2.Send("Test");
                }
                else
                {
                    _clientDataPass = false;
                }
                Thread.Sleep(5000);
            }
            else
            {
                _clientDataPass = false;
            }
            Assert.IsTrue(_clientDataPass);
        }
 public void SetUp()
 {
     _server = new WebSocketServer(54321, IPAddress.Loopback) {OnReceive = OnServerReceive};
     _server.Start();
     _client = new WebSocketClient("ws://127.0.0.1:54321/path") { Origin = "localhost", OnReceive = OnClientReceive };
     _client.Connect();
 }
Пример #4
0
 public void ClientCanCheckIfServerIsNotAvailable()
 {
     _hasDisconnected = false;
     var client2 = new WebSocketClient("ws://127.0.0.1:50000/path") { OnFailedConnection = OnClientDisconnect, ConnectTimeout = new System.TimeSpan(0,0,0,1) };
     client2.Connect();
     Thread.Sleep(3000);
     Assert.IsTrue(_hasDisconnected);
 }
 public void connect()
 {
     Console.WriteLine ("Will connect to GraphWalker server...");
     ws = new WebSocketClient ("ws://localhost:8887/") {
         OnReceive = OnReceive,
         OnConnected = OnConnected
     };
     ws.Connect ();
 }
        public void ClientShouldNotConnectWithInvalidProtocol()
        {
            _server = new WebSocketServer(54321, IPAddress.Loopback);
            _server.Start();
            _client = new WebSocketClient("ws://127.0.0.1:54321/path")
                          {Origin = "localhost", SubProtocols = new[] {"test", "test2"}};

            _client.Connect();
        }
Пример #7
0
        public void ClientShoulConnectWithValidProtocol()
        {
            _server = new WebSocketServer(54321, IPAddress.Loopback) { SubProtocols = new[] { "test" }};
            _server.Start();
            _client = new WebSocketClient("ws://127.0.0.1:54321/path") { Origin = "localhost", SubProtocols = new[] { "test", "test2" } };

            _client.Connect();

            Assert.IsTrue(_client.IsAuthenticated);
            Assert.AreEqual("test", _client.CurrentProtocol);
        }
Пример #8
0
        public void ClientCanCheckIfServerIsNotAvailable()
        {
            _hasDisconnected = false;
            var client2 = new WebSocketClient("ws://127.0.0.1:50000/path")
            {
                OnFailedConnection = OnClientDisconnect, ConnectTimeout = new System.TimeSpan(0, 0, 0, 1)
            };

            client2.Connect();
            Thread.Sleep(3000);
            Assert.IsTrue(_hasDisconnected);
        }
Пример #9
0
 public void SetUp()
 {
     _server = new WebSocketServer(54321, IPAddress.Loopback)
     {
         OnReceive = OnServerReceive
     };
     _server.Start();
     _client = new WebSocketClient("ws://127.0.0.1:54321/path")
     {
         Origin = "localhost", OnReceive = OnClientReceive
     };
     _client.Connect();
 }
        public void ClientShouldNotConnectWithInvalidProtocol()
        {
            _server = new WebSocketServer(false, 54321, IPAddress.Loopback);
            _server.Start();
            _client = new WebSocketClient("ws://127.0.0.1:54321/path")
            {
                Origin = "localhost", SubProtocols = new[] { "test", "test2" }
            };

            _client.Connect();

            Thread.Sleep(500);

            Assert.IsFalse(_client.IsAuthenticated);
        }
Пример #11
0
        public void ClientSendDataConcurrent()
        {
            _forever        = true;
            _clientDataPass = true;
            Alchemy.Handlers.Handler.FastDirectSendingMode = false;
            _requestId      = 1000;
            _responseId     = 1000;
            _requestId2     = 2000;
            _responseId2    = 2000;
            _expectedLength = 8;
            var client2 = new WebSocketClient("ws://127.0.0.1:54321/path")
            {
                OnReceive = OnClientReceive2
            };

            if (_client.Connected)
            {
                client2.Connect();

                if (client2.Connected)
                {
                    _client.Send("Test1000");
                    client2.Send("Halo2000");
                }
                else
                {
                    _clientDataPass = false;
                }
                Thread.Sleep(5000);
            }
            else
            {
                _clientDataPass = false;
            }
            Assert.IsTrue(_clientDataPass);
            Assert.IsTrue(_requestId == _responseId || _requestId - 1 == _responseId);
            Assert.IsTrue(_requestId2 == _responseId2 || _requestId2 - 1 == _responseId2);
            Assert.IsTrue(_responseId > 1010);
            Assert.IsTrue(_responseId2 > 2010);

            _client.Disconnect();
            client2.Disconnect();
            Assert.IsFalse(_client.Connected);
            Assert.IsFalse(client2.Connected);

            _client.Connect();
            Assert.IsTrue(_client.Connected);
        }
        public void ClientShouldConnectWithSecondaryValidProtocol()
        {
            _server = new WebSocketServer(false, 54321, IPAddress.Loopback)
            {
                SubProtocols = new[] { "test2" }
            };
            _server.Start();
            _client = new WebSocketClient("ws://127.0.0.1:54321/path")
            {
                Origin = "localhost", SubProtocols = new[] { "test", "test2" }
            };

            _client.Connect();

            Assert.IsTrue(_client.IsAuthenticated);
            Assert.AreEqual("test2", _client.CurrentProtocol);
        }
Пример #13
0
        public Task<ConnectionResult> Connect(string ip, int port, int custid, string username, string shortname, string password)
        {
            _name = username;
            _shortname = shortname;
            _password = password;
            _custid = custid;
            _subSessionId = Connection.Instance.SubSessionId.GetValueOrDefault();

            _state = new SyncState(_subSessionId);

            _address = string.Format("{0}:{1}", ip, port);
            string path = string.Format("ws://{0}/channel", _address);

            var tcs = new TaskCompletionSource<ConnectionResult>();

            try
            {
                // Create new websocket
                _client = new WebSocketClient(path);
                _client.ConnectTimeout = new TimeSpan(0, 10, 0);

                // Handle connection events
                _client.OnConnected += OnConnected;
                _client.OnDisconnect += context => OnServerDisconnected();

                // Handle received event
                _client.OnReceive += context => OnReceive(context, tcs);
                
                // Connect and wait for connected event
                _client.Connect();

            }
            catch (Exception ex)
            {
                this.ResetStatus();

                tcs.SetResult(new ConnectionResult
                    {
                        Success = false,
                        Message = "Unknown error during connection: " + ex.Message
                    });
            }

            return tcs.Task;
        }
Пример #14
0
        protected override void OnStart(string[] args)
        {
            try
            {
                string wsServer = ConfigurationManager.AppSettings["ws-server"];
                string stanox = ConfigurationManager.AppSettings["stanox"];
                if (string.IsNullOrEmpty(wsServer) || string.IsNullOrEmpty(stanox))
                    throw new ArgumentNullException("", "both ws-server and stanox must be set in app.config");

                _statistics = Delays.Load();
                if (_statistics.Created.Date < DateTime.Now.Date)
                {
                    const string start = "Early Departures for {0:dd-MM-yy}:";
                    var statsByMinute = _statistics.EarlyDepartures.GroupBy(v => v);
                    ICollection<string> values = new List<string>(statsByMinute.Count());
                    const string format = "{0} mins early:{1}";
                    foreach (var minutes in statsByMinute)
                    {
                        values.Add(string.Format(format, minutes.Key, minutes.Count()));
                    }

                    SendTweet(string.Concat(string.Format(start, _statistics.Created), string.Join(",", values.ToArray())));
                    _statistics = Delays.NewInstance();
                    _statistics.Save();
                }

                double lat, lng;
                if (double.TryParse(ConfigurationManager.AppSettings["Lat"], out lat) && double.TryParse(ConfigurationManager.AppSettings["Lng"], out lng))
                {
                    Latitude = lat;
                    Longitude = lng;
                }

                Trace.TraceInformation("Connecting to server on {0}", wsServer);
                _wsClient = new WebSocketClient(wsServer)
                {
                    OnReceive = OnReceive,
                    OnDisconnect = OnDisconnect
                };
                _wsClient.Connect();
                Trace.TraceInformation("Subscribing to stanox {0}", stanox);
                _wsClient.Send(string.Format("substanox:{0}", stanox));
            }
            catch (Exception e)
            {
                Trace.TraceError("Error starting service: {0}", e);
                throw;
            }
        }