示例#1
0
 internal void SubscribeEvent(PowerLineEvent eventObject)
 {
     lock (this.eventsLock)
     {
         this.events.Add(eventObject.Name, eventObject);
     }
 }
示例#2
0
 internal bool UnsubscribeEvent(PowerLineEvent eventObject)
 {
     lock (this.eventsLock)
     {
         return(this.events.Remove(eventObject.Name));
     }
 }
示例#3
0
 public PowerLineEvent AddEvent(PowerLineEvent powerLineEvent)
 {
     lock (this.websocketEventLock)
     {
         this.websocketEvents.Add(powerLineEvent.Name, powerLineEvent);
     }
     return(powerLineEvent);
 }
示例#4
0
        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);
        }
示例#5
0
 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;
                }
            }
        }
示例#7
0
 internal void innerUnsbscribeEvent(PowerLineWebsocketClient client, PowerLineEvent powerLineEvent)
 {
     client.UnsubscribeEvent(powerLineEvent);
     powerLineEvent.UnsubscribeClient(client);
 }
示例#8
0
        public PowerLineEvent CreateEvent(string eventName)
        {
            PowerLineEvent eventObject = new PowerLineEvent(eventName);

            return(this.AddEvent(eventObject));
        }