internal void SubscribeEvent(PowerLineEvent eventObject) { lock (this.eventsLock) { this.events.Add(eventObject.Name, eventObject); } }
internal bool UnsubscribeEvent(PowerLineEvent eventObject) { lock (this.eventsLock) { return(this.events.Remove(eventObject.Name)); } }
public PowerLineEvent AddEvent(PowerLineEvent powerLineEvent) { lock (this.websocketEventLock) { this.websocketEvents.Add(powerLineEvent.Name, powerLineEvent); } return(powerLineEvent); }
internal async Task RaiseEventAsync(PowerLineEvent name, JToken message) { JObject obj = new JObject(); obj.Add("code", 600); obj.Add("name", name.Name); obj.Add("payload", message); string rawJobject = obj.ToString(Newtonsoft.Json.Formatting.None); byte[] raw = System.Text.Encoding.UTF8.GetBytes(rawJobject); await this.SendRawData(raw); }
internal bool innerSubscribeEvent(PowerLineWebsocketClient client, PowerLineEvent powerLineEvent) { if (powerLineEvent.SubscribeWithCheck(client)) { client.SubscribeEvent(powerLineEvent); return(true); } else { return(false); } }
public override async Task HandleRequest(PowerLineContext context) { if (!context.IsWebSocket) { context.SetResponse(400); context.SetResponseHttpString("Only websockets support this kind of endpoints"); return; } else { JObject obj = await context.ReadResponsePayloadAsJson(); string eventName = obj.GetValue <string>(new string[] { "payload", "name" }); lock (context.WebsocketClient.eventsLock) { if (context.WebsocketClient.events.ContainsKey(eventName)) { context.SetResponse((int)PowerLineEventHandlerResponseType.EventAlreadySubscribed); context.SetResponseHttpString("You are already subscribed to this event"); return; } } PowerLineEvent currentEvent = Server.GetEvent(eventName); if (currentEvent == null) { context.SetResponse((int)PowerLineEventHandlerResponseType.EventNotFound); context.SetResponseHttpString("Given event was not found"); return; } if (Server.innerSubscribeEvent(context.WebsocketClient, currentEvent)) { context.SetResponse((int)PowerLineEventHandlerResponseType.EventSubscribed); context.SetResponseHttpString("You are subscribed to the event"); return; } else { context.SetResponse((int)PowerLineEventHandlerResponseType.EventFaildSubscription); context.SetResponseHttpString("Something went wrong while susbscirbing to event"); return; } } }
internal void innerUnsbscribeEvent(PowerLineWebsocketClient client, PowerLineEvent powerLineEvent) { client.UnsubscribeEvent(powerLineEvent); powerLineEvent.UnsubscribeClient(client); }
public PowerLineEvent CreateEvent(string eventName) { PowerLineEvent eventObject = new PowerLineEvent(eventName); return(this.AddEvent(eventObject)); }