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); }
/// <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"); }
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); }
/// <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); }
/// <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"); }
/// <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); }
public Communication(IXSocketClient client, Uri uri) { Client = client; this._uri = uri; this._host = uri.Host; this._port = uri.Port.ToString(); readState = new ReadState(); }
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; }
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; }
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; }
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(); }
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); }
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); } }
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); }
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(); }
/// <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)); }
public static object Parse(this IXSocketClient c, string json, Type t) { return((c.Serializer.Deserialize(json) as Hashtable).Parse(t)); }
/// <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)); }
/// <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)); }
/// <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) }); }