//Sending methods /// <summary> /// Send message /// </summary> /// <param name="payload">ITextArgs</param> /// <param name="callback"> </param> public void Send(ITextArgs payload, Action callback) { if (!this.IsConnected) throw new Exception("You cant send messages when not conencted to the server"); var frame = GetDataFrame(payload); Socket.Send(frame.ToBytes(), callback.Invoke, err => FireOnClose()); }
private static void OnSayConfirmed(ITextArgs textArgs) { Console.WriteLine("Confirmed Say"); //We send a message on the bound event “say”, this will instantly trigger the callback “OnSay” below. //Not very realistic, but it proves the functionality of confirm callbacks. //socket.Send(new { message = "Hello World" }.AsTextArgs("say")); }
//public XSocketClient(string url, string origin, Pkcs12Store certificate, bool connect = false, bool isPrimitive = false):this(url,origin,connect,isPrimitive) //{ // //this._certificate = certificate; //} private void Error(ITextArgs textArgs) { if (this.OnError != null) { this.OnError.Invoke(this, textArgs as TextArgs); } }
private void OnTick(ITextArgs args) { Debug.WriteLine("Tick"); var stock = JsonConvert.DeserializeObject <Stock>(args.data); RunOnContext(() => AddOrUpdateStock(stock)); }
/// <summary> /// A message was received /// </summary> /// <param name="args"></param> protected virtual void FireOnMessage(ITextArgs args) { try { //Is this a primitive instance (pub/sub not used) if (this.IsPrimitive) { if (this.OnMessage != null) { this.OnMessage.Invoke(this, args as TextArgs); } return; } //Get binding var binding = this.GetBindings().FirstOrDefault(b => b.Event.Equals(args.@event) && b.IsBound); //If binding is missing and we allow unbound events, fire onmessage if (binding == null) { if (FireOnMessageForUnboundEvents) { if (this.OnMessage != null) { this.OnMessage.Invoke(this, args as TextArgs); } } return; } //The binding is primitive = fire onmessage if (binding.IsPrimitive) { //fire primitive event onmessage if (this.OnMessage != null) { this.OnMessage.Invoke(this, args as TextArgs); } } //Pub/Sub is used, fire the callback with the argument else { this.FireBoundMethod(binding, args); } } catch (Exception) // Will dispatch to OnError on exception { if (this.OnError != null) { this.OnError.Invoke(this, args as TextArgs); } } }
private async void FireBoundMethod(ISubscription binding, ITextArgs args) { binding.Callback(args); binding.Counter++; if (binding.SubscriptionType == SubscriptionType.One) { await this.UnBind(binding.Event); } else if (binding.SubscriptionType == SubscriptionType.Many && binding.Counter == binding.Limit) { await this.UnBind(binding.Event); } }
private void OnAllStocks(ITextArgs args) { Debug.WriteLine("OnAllStocks"); var stocks = JsonConvert.DeserializeObject <IList <Stock> >(args.data); RunOnContext(() => { foreach (var stock in stocks) { AddOrUpdateStock(stock); } }); }
public async Task Send(ITextArgs payload) { if (!this.IsConnected) { throw new Exception("You cant send messages when not conencted to the server"); } var frame = GetDataFrame(payload); try { await Socket.SendAsync(frame.ToBytes()).ConfigureAwait(false); } catch (Exception) { FireOnClose(); } }
private Rfc6455DataFrame GetDataFrame(ITextArgs textArgs) { return(GetDataFrame(FrameType.Text, Encoding.UTF8.GetBytes(this.Serialize(textArgs)))); }
private void Opened(ITextArgs textArgs) { this.ClientInfo = this.Serializer.DeserializeFromString<ClientInfo>(textArgs.data); FireOnOpen(); }
public void Demo1(ITextArgs args) { Debug.WriteLine("DefaultBinding"); Debug.WriteLine("Event:{0}, Data:{1}", args.@event, args.data); Debug.WriteLine(""); }
/// <summary> /// Send prepared ITextArgs /// </summary> /// <param name="textArgs"></param> public void Send(ITextArgs textArgs) { Websockets[_conn]._textQueue.Add(textArgs); }
private void FireBoundMethod(ISubscription binding, ITextArgs args) { binding.Callback(args); binding.Counter++; if (binding.SubscriptionType == SubscriptionType.One) this.UnBind(binding.Event); else if (binding.SubscriptionType == SubscriptionType.Many && binding.Counter == binding.Limit) { this.UnBind(binding.Event); } }
public void Trigger(ITextArgs payload, Action callback) { this.Send(payload, callback); }
public Task Trigger(ITextArgs payload) { return(this.Send(payload)); }
/// <summary> /// Give this controller a "Generic" behavior /// Any object sent (that do not have an action method) in will be sent out to the clients subscribing to the topic. /// </summary> /// <param name="textArgs"></param> public override void OnMessage(ITextArgs textArgs) { this.SendToAll(textArgs); }
private void Opened(ITextArgs textArgs) { this.ClientInfo = this.Serializer.DeserializeFromString <ClientInfo>(textArgs.data); FireOnOpen(); }
//Incomming textmessage public override void OnMessage(IXSocketController controller, ITextArgs e) { Console.WriteLine("IN " + e.data); //Let the message continue into the server base.OnMessage(controller, e); }
//Outgoing textmessage public override ITextArgs OnSend(IXSocketProtocol protocol, ITextArgs e) { Console.WriteLine("OUT " + e.data); return(base.OnSend(protocol, e)); }
public override void OnMessage(ITextArgs textArgs) { factory.StartNew(() => HandleMessage(textArgs), cts.Token); }
public void Trigger(ITextArgs payload) { this.Send(payload); }
public void HandleMessage(ITextArgs textArgs) { try { var jdata = JObject.Parse(textArgs.data); var id = jdata["msg"]; if (id == null) { return; } var command = id.Value <string>(); switch (command) { case OnInstalledMod.Msg: { var msg = jdata.ToObject <OnInstalledMod>(); log.Debug(SteamID + " -> installed " + msg.Mod.name + "."); Mods.Add(msg.Mod); Browser.AsyncSendTo(x => x.user != null && x.user.steam.steamid == SteamID, BrowserController.InstallResponse("The mod has been installed.", true), rf => { }); break; } case OnDeletedMod.Msg: { var msg = jdata.ToObject <OnDeletedMod>(); log.Debug(SteamID + " -> removed " + msg.Mod.name + "."); var localMod = Mods.FirstOrDefault(m => Equals(msg.Mod, m)); if (localMod != null) { Mods.Remove(localMod); } break; } case Init.Msg: { var msg = jdata.ToObject <Init>(); InitData = msg; if (msg.Version != Version.ClientVersion) { this.SendJson(JObject.FromObject(new Shutdown()).ToString(Formatting.None), "commands"); return; } foreach (var mod in msg.Mods.Where(mod => mod != null && mod.name != null && mod.version != null)) { Mods.Add(mod); } //Insert the client into the DB RegisterClient(); break; } case RequestMod.Msg: { var msg = jdata.ToObject <RequestMod>(); var mod = D2MPMaster.Mods.Mods.ByName(msg.Mod.name); if (mod != null && mod.playable) { if (!Mods.Any(m => m.name == mod.name && m.version == mod.version)) { this.InstallMod(this.UID, mod); } } break; } } } catch (Exception ex) { log.Error("Error parsing client message.", ex); } }
private static void OnSay(ITextArgs textArgs) { Console.WriteLine("Say: " + textArgs.data); }
private Rfc6455DataFrame GetDataFrame(ITextArgs textArgs) { return GetDataFrame(FrameType.Text, Encoding.UTF8.GetBytes(this.Serialize(textArgs))); }
/// <summary> /// A message was received /// </summary> /// <param name="args"></param> protected virtual void FireOnMessage(ITextArgs args) { try { //Is this a primitive instance (pub/sub not used) if (this.IsPrimitive) { if (this.OnMessage != null) this.OnMessage.Invoke(this, args as TextArgs); return; } //Get binding var binding = this.GetBindings().FirstOrDefault(b => b.Event.Equals(args.@event) && b.IsBound); //If binding is missing and we allow unbound events, fire onmessage if (binding == null) { if (FireOnMessageForUnboundEvents) { if (this.OnMessage != null) this.OnMessage.Invoke(this, args as TextArgs); } return; } //The binding is primitive = fire onmessage if (binding.IsPrimitive) { //fire primitive event onmessage if (this.OnMessage != null) this.OnMessage.Invoke(this, args as TextArgs); } //Pub/Sub is used, fire the callback with the argument else { this.FireBoundMethod(binding, args); } } catch (Exception) // Will dispatch to OnError on exception { if (this.OnError != null) this.OnError.Invoke(this, args as TextArgs); } }
private void Error(ITextArgs textArgs) { if (this.OnError != null) this.OnError.Invoke(this, textArgs as TextArgs); }
public void ProcessMessage(ITextArgs textArgs) { try { var jdata = JObject.Parse(textArgs.data); var id = jdata["msg"]; if (id == null) { return; } var command = id.Value <string>(); lock (ConcurrentLock) { try { switch (command) { case Init.Msg: { if (Inited) { return; } var msg = jdata.ToObject <Init>(); if (msg.password != Init.Password) { //Wrong password Send("authFail"); return; } if (msg.version != Init.Version) { Send("outOfDate|" + Program.S3.GenerateBundleURL("s" + Init.Version + ".zip")); return; } //Build server addon operation var add = (from addon in ServerAddons.Addons let exist = msg.addons.FirstOrDefault( m => m.name == addon.name && m.version == addon.version) where exist == null select addon.name + ">" + addon.version + ">" + Program.S3.GenerateBundleURL(addon.bundle).Replace('|', ' ')).ToList(); var del = (from addon in msg.addons let exist = ServerAddons.Addons.FirstOrDefault(m => m.name == addon.name) where exist == null select addon.name).ToList(); if ((add.Count + del.Count) == 0) { InitData = msg; Address = InitData.publicIP; Inited = true; } else { Send("addonOps|" + string.Join(",", add) + "|" + string.Join(",", del)); } break; } case OnServerLaunched.Msg: { var msg = jdata.ToObject <OnServerLaunched>(); if (Instances.ContainsKey(msg.id)) { var instance = Instances[msg.id]; instance.port = msg.port; LobbyManager.OnServerReady(instance); } break; } case OnServerShutdown.Msg: { var msg = jdata.ToObject <OnServerShutdown>(); if (Instances.ContainsKey(msg.id)) { GameInstance instance; Instances.TryRemove(msg.id, out instance); LobbyManager.OnServerShutdown(instance); } break; } } } catch (Exception ex) { log.Error("Problem processing server message: ", ex); } } } catch (Exception ex) { log.Error("Parsing server message.", ex); } }