private void OnReceived(object sender, WebSocketReceivedEventArgs e) { if (e.Url == this.url) { this.Received?.Invoke(this, e); } }
private void OnReceived(object sender, WebSocketReceivedEventArgs e) { // Syntax: // Subscribe: !<NotiferName>/Key1/Key2 // Unsubscribe: ?<NotiferName>/Key1/Key2 // e.g. !MyNotifier/MyTopic/MySubTopic // Library: // let myFunction = (key, value) => {} // Notifier.Subscribe("MyNotifier/Key1/Key2", myFunction) // Notifier.UnSubscribe(myFunction) try { if (e.Message.StartsWith("!" + this.notifierName + "/")) { string key = e.Message.Substring(this.notifierName.Length + 2); if (this.subscriptions.ContainsKey(key) == false) { this.subscriptions.Add(key, new List <WebSocket>()); } if (this.subscriptions[key].Contains(e.WebSocket) == false) { this.subscriptions[key].Add(e.WebSocket); Logging.Info("Subscription added: " + e.Message); if (this.retainBuffer.ContainsKey(key)) { this.Notify(key, this.retainBuffer[key], true, e.WebSocket); } } } else if (e.Message.StartsWith("?" + this.notifierName + "/")) { string key = e.Message.Substring(this.notifierName.Length + 2); if (this.subscriptions.ContainsKey(key)) { if (this.subscriptions[key].Contains(e.WebSocket)) { this.subscriptions[key].Remove(e.WebSocket); Logging.Info("Subscription removed: " + e.Message); } } } } catch (Exception ex) { Logging.Error($"Error during receiving notification request: {e.Message}", ex); } }
private void OnReceived(object sender, WebSocketReceivedEventArgs e) { // Syntax: <ControllerName>/Key1/Key2=Value // e.g. MyController/MyTopic/MySubTopic=42 // Library: // let isHandledCallback = (key, value) => {} // Controller.send("MyNotifier/Key1/Key2", value) // Controller.send("MyNotifier/Key1/Key2", value, isHandledCallback) try { string key = e.Message.Split('=')[0].Trim(' '); if (key.StartsWith(this.controllerName + "/")) { string[] valueArray = e.Message.Split('='); string value = string.Empty; for (int i = 1; i < valueArray.Length; i++) { value += valueArray[i]; } Logging.Debug("Control received: " + e.Message); ControllerValueChangedEventArgs eventArgs = new ControllerValueChangedEventArgs(key.Substring(this.controllerName.Length + 1), value); try { this.ValueChanged?.Invoke(this, eventArgs); } catch (Exception ex) { Logging.Error($"Error while executing control request: Key: {key}, Value {value}"); } if (eventArgs.IsHandled == true) { WebSocketController.Instance.Send(e.Message); } } } catch (Exception ex) { Logging.Error($"Error during receiving control request: {e.Message}", ex); } }