public void TearDown()
 {
     _client.Disconnect();
     _server.Stop();
     _client = null;
     _server = null;
 }
Example #2
0
        public AlchemyClientCS()
        {
            string _address = string.Empty;
            if (string.IsNullOrEmpty(Server) || string.IsNullOrEmpty(Port))
            {
                _address = "ws://127.0.0.1:81/";
            }
            else
            {
                _address = string.Format("ws://{0}:{1}/", Server, Port);
            }

            //client = new WebSocketClient(_address);
            //client.OnConnect = new OnEventDelegate(OnConnect);
            //client.OnConnected = new OnEventDelegate(OnConnected);
            //client.OnSend = new OnEventDelegate(OnSend);
            //client.OnReceive = new OnEventDelegate(OnReceive);
            //client.OnDisconnect = new OnEventDelegate(OnDisconnect);

            client = new WebSocketClient(_address)
            {
                OnConnect = OnConnect,
                OnConnected = OnConnected,
                OnSend = OnSend,
                OnReceive = OnReceive,
                OnDisconnect = OnDisconnect
            };

            //client.Connect();
        }
 public void Dispose()
 {
     _client.Disconnect();
     _server.Stop();
     _client = null;
     _server = null;
 }
        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();
 }
 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();
        }
        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);
        }
Example #10
0
 ///<summary>
 /// Class Constructor that handles the Gazetracker client and the WebSocket connections.
 /// </summary>
 /// <param name="address">The server address: ws://address:port/</param>
 public WSClient(string address)
 {
     createXml();
     wsClient = new WebSocketClient(address)
     {
         OnConnected = OnConnected,
         OnDisconnect = OnDisconnect,
         OnFailedConnection = OnFailedConnection,
         OnReceive = OnReceive
     };
     gtClient = new GazeTrackerClient.Client();
 }
        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);
        }
Example #12
0
        ///<summary>
        /// Class Constructor that handles the Gazetracker client and the WebSocket connections.
        /// </summary>
        public WSClient()
        {
            createXml();
            wsClient = new WebSocketClient("ws://ciman.math.unipd.it:8000/")
            {
                OnConnected = OnConnected,
                OnDisconnect = OnDisconnect,
                OnFailedConnection = OnFailedConnection,
                OnReceive = OnReceive
            };
            gtClient = new GazeTrackerClient.Client();

            gtClient.ErrorOccured += OnErrorOccured;

            gtClient.Calibration.OnPointChange += new GazeTrackerClient.Calibration.PointChangeHandler(OnCalibrationPointChange);
            gtClient.Calibration.OnEnd += new GazeTrackerClient.Calibration.EndHandler(OnCalibrationEnd);
            gtClient.GazeData.OnGazeData += new GazeTrackerClient.GazeData.GazeDataHandler(OnGazeData);
        }
Example #13
0
        public void Dispose()
        {
            if (client != null)
            {
                this.Disconnect();
                client = null;
            }

            if (_timer != null)
            {
                _timer.Stop();
                _timer.Enabled = false;
                _timer.Close();
                _timer.Dispose();
                _timer = null;
            }
        }
Example #14
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;
        }
Example #15
0
 public void Disconnect()
 {
     try
     {
         if (_client != null) _client.Disconnect();
     }
     finally
     {
         _client = null;
     }
     
     this.ResetStatus();
     this.OnDisconnected();
 }
Example #16
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;
            }
        }