Ejemplo n.º 1
0
        public static void Main()
        {
            _client = new XSocketClient("127.0.0.1", 4502, "JsonProtocol", "Welcome to JsonProtocol");

            _client.OnOpen += (sender, args) =>
            {
                Debug.Print("Connected");
                // Tell the server that I am a Netduino
                _client.SetEnum("Hardware", "Netduino", "Sensor");

                //Start sending fake sensor data
                sensorThread = new Thread(Sensor);
                sensorThread.Start();
            };
            _client.OnClose += (sender, args) =>
            {
                Debug.Print("Closed");
                sensorThread.Suspend();
            };
            //Listen for messages
            _client.OnMessage += _client_OnMessage;

            //Open connection
            _client.Open();

            //Prevent exit since that would stop the program
            Thread.Sleep(Timeout.Infinite);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates and connects the hub connection and hub proxy. This method
        /// is called asynchronously from SignInButton_Click.
        /// </summary>
        private async void ConnectAsync()
        {
            Connection = new XSocketClient(ServerURI, Origin, "chat");
            Connection.OnDisconnected += Connection_Disconnected;
            chatController             = Connection.Controller("chat");

            //Handle incoming event from server: use Invoke to write to console from XSocket's thread
            chatController.On <string>("AddMessage", (message) =>
                                       this.Dispatcher.Invoke(() =>
                                                              RichTextBoxConsole.AppendText(String.Format("{0}\r", message))
                                                              )
                                       );

            try
            {
                await Connection.Open();

                //Set username
                await chatController.SetProperty("username", UserName);
            }
            catch
            {
                StatusText.Content = "Unable to connect to server: Start server before connecting clients.";
                //No connection: Don't enable Send button or show chat UI
                return;
            }

            //Show chat UI; hide login UI
            SignInPanel.Visibility = Visibility.Collapsed;
            ChatPanel.Visibility   = Visibility.Visible;
            ButtonSend.IsEnabled   = true;
            TextBoxMessage.Focus();
            RichTextBoxConsole.AppendText("Connected to server at " + ServerURI + "\r");
        }
Ejemplo n.º 3
0
        public static void Main()
        {
            _client = new XSocketClient("127.0.0.1", 4502, "JsonProtocol", "Welcome to JsonProtocol");

            _client.OnOpen += (sender, args) =>
            {
                Debug.Print("Connected");
                // Tell the server that I am a Netduino
                _client.SetEnum("Hardware","Netduino","Sensor");
                
                //Start sending fake sensor data
                sensorThread = new Thread(Sensor);
                sensorThread.Start();
            };
            _client.OnClose += (sender, args) =>
            {
                Debug.Print("Closed");
                sensorThread.Suspend();
            };
            //Listen for messages
            _client.OnMessage += _client_OnMessage;

            //Open connection
            _client.Open();            

            //Prevent exit since that would stop the program
            Thread.Sleep(Timeout.Infinite);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates and connects the connection. Only uses one controller (chat). This method
        /// is called from SignInButton_Click.
        /// </summary>
        private async void Connect()
        {
            Connection = new XSocketClient(ServerURI,Origin,"chat");
            Connection.OnDisconnected += Connection_Disconnected;
            chatController = Connection.Controller("chat");            
            
            //Handle incoming event from server: use Invoke to write to console from XSocket's thread
            chatController.On<string>("addMessage", message => this.Invoke((Action) (() =>
                RichTextBoxConsole.AppendText(String.Format("{0}" + Environment.NewLine, message))
            )));
            
            try
            {
                Connection.Open();
                comboBoxLocation.SelectedIndex = 0;
                //Set username
                chatController.SetProperty("username", UserName);
            }
            catch
            {
                StatusText.Text = "Unable to connect to server: Start server before connecting clients.";
                //No connection: Don't enable Send button or show chat UI
                return;
            }

            //Activate UI
            SignInPanel.Visible = false;
            ChatPanel.Visible = true;
            ButtonSend.Enabled = true;
            TextBoxMessage.Focus();
            RichTextBoxConsole.AppendText("Connected to server at " + ServerURI + Environment.NewLine);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates and connects the hub connection and hub proxy. This method
        /// is called asynchronously from SignInButton_Click.
        /// </summary>
        private async void ConnectAsync()
        {
            Connection = new XSocketClient(ServerURI, Origin, "chat");
            Connection.OnDisconnected += Connection_Disconnected;
            chatController = Connection.Controller("chat");

            //Handle incoming event from server: use Invoke to write to console from XSocket's thread
            chatController.On<string>("AddMessage", (message) =>
                this.Dispatcher.Invoke(() =>
                    RichTextBoxConsole.AppendText(String.Format("{0}\r", message))
                )
            );

            try
            {
                Connection.Open();

                //Set username
                chatController.SetProperty("username", UserName);
            }
            catch
            {
                StatusText.Content = "Unable to connect to server: Start server before connecting clients.";
                //No connection: Don't enable Send button or show chat UI
                return;
            }

            //Show chat UI; hide login UI
            SignInPanel.Visibility = Visibility.Collapsed;
            ChatPanel.Visibility = Visibility.Visible;
            ButtonSend.IsEnabled = true;
            TextBoxMessage.Focus();
            RichTextBoxConsole.AppendText("Connected to server at " + ServerURI + "\r");
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates and connects the connection. Only uses one controller (chat). This method
        /// is called from SignInButton_Click.
        /// </summary>
        private async void Connect()
        {
            Connection = new XSocketClient(ServerURI, Origin, "chat");
            Connection.OnDisconnected += Connection_Disconnected;
            chatController             = Connection.Controller("chat");

            //Handle incoming event from server: use Invoke to write to console from XSocket's thread
            chatController.On <string>("addMessage", message => this.Invoke((Action)(() =>
                                                                                     RichTextBoxConsole.AppendText(String.Format("{0}" + Environment.NewLine, message))
                                                                                     )));

            try
            {
                Connection.Open();
                comboBoxLocation.SelectedIndex = 0;
                //Set username
                chatController.SetProperty("username", UserName);
            }
            catch
            {
                StatusText.Text = "Unable to connect to server: Start server before connecting clients.";
                //No connection: Don't enable Send button or show chat UI
                return;
            }

            //Activate UI
            SignInPanel.Visible = false;
            ChatPanel.Visible   = true;
            ButtonSend.Enabled  = true;
            TextBoxMessage.Focus();
            RichTextBoxConsole.AppendText("Connected to server at " + ServerURI + Environment.NewLine);
        }
Ejemplo n.º 7
0
 public Communication(IXSocketClient client, Uri uri)
 {
     Client     = client;
     this._uri  = uri;
     this._host = uri.Host;
     this._port = uri.Port.ToString();
     readState  = new ReadState();
 }
Ejemplo n.º 8
0
 public Controller(IXSocketClient client, string controller)
 {
     this.Subscriptions = new RepositoryInstance<string, ISubscription>();
     this.Listeners = new RepositoryInstance<string, IListener>();
     this.AddDefaultSubscriptions();
     this.ClientInfo = new ClientInfo{PersistentId = client.PersistentId, Controller = controller};
     this.XSocketClient = client;
 }
Ejemplo n.º 9
0
 public Communication(IXSocketClient client, Uri uri)
 {
     Client = client;
     this._uri = uri;
     this._host = uri.Host;
     this._port = uri.Port.ToString();
     readState = new ReadState();
     SocketConnected += OnSocketConnected;
 }
Ejemplo n.º 10
0
 public Controller(IXSocketClient client, string controller)
 {
     this.queuedFrames  = new List <byte>();
     this.Subscriptions = new RepositoryInstance <string, ISubscription>();
     this.Listeners     = new RepositoryInstance <string, IListener>();
     this.AddDefaultSubscriptions();
     this.ClientInfo = new ClientInfo {
         PersistentId = client.PersistentId, Controller = controller
     };
     this.XSocketClient = client;
 }
Ejemplo n.º 11
0
        public Rfc6455Handshake(string url, string origin, IXSocketClient client)
        {
            _client        = client;
            this.Handshake = new NameValueCollection();
            this.Handshake.Add("GET", "GET {0} HTTP/1.1");
            this.Handshake.Add("HOST", "Host: {0}");
            this.Handshake.Add("ORIGIN", "Origin: {0}");
            this.Handshake.Add("UPGRADE", "Upgrade: websocket");
            this.Handshake.Add("CONNECTION", "Connection: Upgrade,Keep-Alive");

            //Add cookies if any...
            if (client.Cookies.Count > 0)
            {
                var csb = new StringBuilder();
                var ii  = 0;
                foreach (Cookie cookie in client.Cookies)
                {
                    if (ii + 1 == client.Cookies.Count)
                    {
                        csb.Append(string.Format("{0}={1}", cookie.Name, cookie.Value));
                    }
                    else
                    {
                        csb.Append(string.Format("{0}={1}; ", cookie.Name, cookie.Value));
                    }
                    ii++;
                }

                //for (var i = 0; i < client.Cookies.Count; i++)
                //{
                //    var cookie = client.Cookies[i];
                //    if (i + 1 == client.Cookies.Count)
                //        csb.Append(string.Format("{0}={1}", cookie.Name, cookie.Value));
                //    else
                //        csb.Append(string.Format("{0}={1}; ", cookie.Name, cookie.Value));
                //}
                this.Handshake.Add("COOKIE", string.Format("Cookie: {0}", csb.ToString()));
            }

            this.Handshake.Add("SEC-WEBSOCKET-KEY", "Sec-WebSocket-Key: {0}");
            this.Handshake.Add("SEC-WEBSOCKET-VERSION", "Sec-WebSocket-Version: 13");
            this.Handshake.Add("SEC-WEBSOCKET-PROTOCOL", "Sec-WebSocket-Protocol: XSocketsNET");

            var uri = new Uri(url);

            this._origin = origin;
            this._host   = string.Format("{0}:{1}", uri.Host, uri.Port);

            this._path = uri.PathAndQuery;

            this.Key = GenerateKey();
        }
Ejemplo n.º 12
0
        public static void Main()
        {
            //Change this ip to the IP of your machine (where XSockets is running)
            _conn = new XSocketClient("192.168.1.2", 4502);

            _conn.OnOpen    += ConnOnOpen;
            _conn.OnClose   += ConnOnClose;
            _conn.OnMessage += ConnOnMessage;

            _conn.Open();

            Thread.Sleep(Timeout.Infinite);
        }
Ejemplo n.º 13
0
 public static IMessage ToMessage(this IXSocketClient c, byte[] b)
 {
     try
     {
         var json = UTF8Encoding.UTF8.GetString(b);
         var t    = c.Serializer.Deserialize(json) as Hashtable;
         return(new Message(t["D"] as string, t["T"] as string, t["C"] as string));
     }
     catch
     {
         return(null);
     }
 }
Ejemplo n.º 14
0
        public static void Main()
        {
            //Change this ip to the IP of your machine (where XSockets is running)
            _conn = new XSocketClient("192.168.1.2", 4502);

            _conn.OnOpen += ConnOnOpen;
            _conn.OnClose += ConnOnClose;
            _conn.OnMessage += ConnOnMessage;

            _conn.Open();

            Thread.Sleep(Timeout.Infinite);
        }
Ejemplo n.º 15
0
        public Rfc6455Handshake(string url, string origin, IXSocketClient client)
        {
            _client = client;
            this.Handshake = new NameValueCollection();
            this.Handshake.Add("GET", "GET {0} HTTP/1.1");
            this.Handshake.Add("HOST", "Host: {0}");
            this.Handshake.Add("ORIGIN", "Origin: {0}");
            this.Handshake.Add("UPGRADE", "Upgrade: websocket");
            this.Handshake.Add("CONNECTION", "Connection: Upgrade,Keep-Alive");

            //Add cookies if any...
            if (client.Cookies.Count > 0)
            {
                var csb = new StringBuilder();
                var ii = 0;
                foreach (Cookie cookie in client.Cookies)
                {                    
                    if (ii + 1 == client.Cookies.Count)
                        csb.Append(string.Format("{0}={1}", cookie.Name, cookie.Value));
                    else
                        csb.Append(string.Format("{0}={1}; ", cookie.Name, cookie.Value));
                    ii++;
                }

                //for (var i = 0; i < client.Cookies.Count; i++)
                //{
                //    var cookie = client.Cookies[i];
                //    if (i + 1 == client.Cookies.Count)
                //        csb.Append(string.Format("{0}={1}", cookie.Name, cookie.Value));
                //    else
                //        csb.Append(string.Format("{0}={1}; ", cookie.Name, cookie.Value));
                //}
                this.Handshake.Add("COOKIE", string.Format("Cookie: {0}", csb.ToString()));
            }

            this.Handshake.Add("SEC-WEBSOCKET-KEY", "Sec-WebSocket-Key: {0}");
            this.Handshake.Add("SEC-WEBSOCKET-VERSION", "Sec-WebSocket-Version: 13");
            this.Handshake.Add("SEC-WEBSOCKET-PROTOCOL", "Sec-WebSocket-Protocol: XSocketsNET");

            var uri = new Uri(url);
            this._origin = origin;
            this._host = string.Format("{0}:{1}", uri.Host, uri.Port);

            this._path = uri.PathAndQuery;

            this.Key = GenerateKey();
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Deserialize JSON to a strongly typed object
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="client"></param>
 /// <param name="json"></param>
 /// <returns></returns>
 public static T Deserialize <T>(this IXSocketClient client, string json)
 {
     return(client.Serializer.DeserializeFromString <T>(json));
 }
Ejemplo n.º 17
0
 public static object Parse(this IXSocketClient c, string json, Type t)
 {
     return((c.Serializer.Deserialize(json) as Hashtable).Parse(t));
 }
Ejemplo n.º 18
0
 /// <summary>
 /// If possible use the extension-method ToTextArgs for the controller instead
 /// </summary>
 /// <param name="client"></param>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static string Serialize(this IXSocketClient client, object obj)
 {
     return(client.Serializer.SerializeToString(obj));
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Deserialize JSON to a strongly typed object.
 /// </summary>
 /// <param name="client"></param>
 /// <param name="targetType"></param>
 /// <param name="json"></param>
 /// <returns></returns>
 internal static object GetObject(this IXSocketClient client, Type targetType, string json)
 {
     return(client.Serializer.DeserializeFromString(json, targetType));
 }
Ejemplo n.º 20
0
 /// <summary>
 /// If possible use the extension-method ToTextArgs for the controller instead
 /// </summary>
 /// <param name="client"></param>
 /// <param name="obj"></param>
 /// <param name="eventname"></param>
 /// <returns></returns>
 public static ITextArgs AsTextArgs(this IXSocketClient client, object obj, string eventname)
 {
     return(new TextArgs {
         @event = eventname.ToLower(), data = client.Serializer.SerializeToString(obj)
     });
 }