Beispiel #1
0
 protected void RaiseContinentLockEventOnMainThread(Events.World.ContinentLockEventArgs e)
 {
     this.SynchronizationContext.Post(x =>
     {
         OnContinentLock(e);
     }, null);
 }
Beispiel #2
0
 protected void OnContinentLock(Events.World.ContinentLockEventArgs e)
 {
     if (ContinentLocked != null)
     {
         ContinentLocked(this, e);
     }
 }
Beispiel #3
0
        //public string CreateCommand(int selectedWorld, serviceId)
        //{

        //}


        async void ListenToWebSocketStuff()
        {
            var client = new System.Net.Http.HttpClient
            {
                BaseAddress           = new Uri("http://1.2.3.4"),
                DefaultRequestHeaders = { Host = "example.com" }
            };

            using (var clientWebSocket = new ClientWebSocket())
            {
                this.ServiceId = "example";
                Uri serverUri = new Uri($"wss://push.planetside2.com/streaming?environment=ps2&service-id=s:trashpanda");

                //Uri serverUri = new Uri($"wss://push.planetside2.com/streaming?environment=ps2&service-id=s:{ServiceId}");


                await clientWebSocket.ConnectAsync(serverUri, CancellationToken.None);



                // create our command that we're going to tell the service
                string s = @"{\042service\042:\042event\042,\042action\042:\042subscribe\042,\042worlds\042:[\0421\042,\0429\042,\04210\042,\04211\042,\04213\042,\04217\042,\04218\042,\04219\042,\04225\042,\0421000\042,\0421001\042],\042eventNames\042:[\042FacilityControl\042,\042MetagameEvent\042]}";
                //s = "{'service':'event','action':'subscribe','worlds':['17'],'eventNames':['FacilityControl','MetagameEvent','ContinentLock',ContinentUnlock']}";
                s = "{'service':'event','action':'subscribe','worlds':['1','10','13','17','19','25','40'],'eventNames':['FacilityControl','MetagameEvent','ContinentLock',ContinentUnlock']}";
                //use json later to be able to parse the this.selectedWorld into the string command.

                //create a method for turning the data from filterbox into a JSON command as seen above ^

                // convert the command into an array of Bytes
                byte[] bytes = Encoding.UTF8.GetBytes(s);

                // convert to ArraySegment
                ArraySegment <byte> buffer            = new ArraySegment <byte>(bytes);
                CancellationToken   cancellationToken = CancellationToken.None;
                //send the command asynchroneously
                await clientWebSocket.SendAsync(buffer, WebSocketMessageType.Text, true, cancellationToken);

                List <string>          results = new List <string>();
                WebSocketReceiveResult result;
                string resultString = string.Empty;
                while (IsStarted)
                {
                    //define the WebSocket result
                    result = await clientWebSocket.ReceiveAsync(buffer, cancellationToken);

                    if (result.EndOfMessage) //we have the full message. now we...
                    {
                        //decode the buffer array to a UTF-8 string
                        resultString = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);

                        //empty the buffer so it's ready for a new message
                        buffer = new ArraySegment <byte>(new byte[1024]);

                        //deserialize the resultString into an object of type ReceivedMsg
                        Message message = Newtonsoft.Json.JsonConvert.DeserializeObject <Message>(resultString);

                        //filter out the help message
                        if (message.Service == "push")
                        {
                            Console.WriteLine("CONNECTION STATUS - - - - - " + message.ToString());
                            var args = new Events.MessageEventArgs()
                            {
                                connectionStatus = message.connectionStatus
                            };
                            RaiseConnectionStateOnMainThread(args);
                            Console.WriteLine("---------------------EVENTRAISED ON SERVICE END-----------------------------");
                            //this event just will NOT raise on the other page for some reason
                        }
                        if (message.Service != "push" &&
                            !(message.Service == "event" && message.Action == "help"))
                        {
                            Events.ReceivedMsg rMsg = Newtonsoft.Json.JsonConvert.DeserializeObject <Events.ReceivedMsg>(resultString);

                            if (rMsg.Service == "event" && rMsg.Type == "serviceMessage")
                            {
                                //okay, it's an event.  That's the first criteria
                                //Events.Payload.EventPayload payload = message.newPayload;


                                //if rMsg.newPayload.Event_name matches any entry in the eventsWeWant

                                var payload = rMsg.newPayload;
                                //if it's not specified for whatever reason, assume user wants FacilityControlArgs
                                if (eventsWeWant == null)
                                {
                                    eventsWeWant = new List <string>();
                                    this.eventsWeWant.Add("FacilityControl");
                                    this.eventsWeWant.Add("MetagameEvent");
                                }

                                int position = Array.IndexOf(eventsWeWant.ToArray(), payload.Event_name);
                                if (position > -1)
                                {
                                    //get the event payload
                                    Console.WriteLine("\nPAYLOAD RECEIVED " + rMsg.newPayload.ToString());
                                }

                                if (payload.Event_name == "FacilityControl")
                                {
                                    //there is a lot of invalid information that we need to filter out with if statements

                                    if ((payload.old_faction_id != 0) &&
                                        (payload.duration_held < payload.Timestamp))
                                    {
                                        Events.FacilityControlChangedEvent newFCevent = Newtonsoft.Json.JsonConvert.DeserializeObject <Events.FacilityControlChangedEvent>(innerPayloadJSON);
                                        //raise event
                                        var args = new FacilityControlChangedEventArgs()
                                        {
                                            Payload = payload
                                        };
                                        RaiseFacilityControlOnMainThread(args);
                                    }
                                }

                                //Generic event
                                if (payload.Event_name == "MetagameEvent")
                                {
                                    Events.MetagameEventEvent newMgEvent = Newtonsoft.Json.JsonConvert.DeserializeObject <Events.MetagameEventEvent>(innerPayloadJSON);
                                    //raise event
                                    var args = new Events.World.MetagameEventEventArgs()
                                    {
                                        Payload = payload
                                    };
                                    RaiseMetagameEventOnMainThread(args);
                                }

                                //ContinentLock
                                if (payload.Event_name == "MetagameEvent" && payload.metagame_event_state_name == "ended")
                                {
                                    Events.ContinentLockEvent newMgEvent = Newtonsoft.Json.JsonConvert.DeserializeObject <Events.ContinentLockEvent>(innerPayloadJSON);
                                    //raise event
                                    var args = new Events.World.ContinentLockEventArgs()
                                    {
                                        Payload = payload
                                    };
                                    RaiseContinentLockEventOnMainThread(args);
                                }

                                //ContinentUnlock
                                if (payload.Event_name == "MetagameEvent" && payload.metagame_event_state_name == "started")
                                {
                                    Events.ContinentUnlockEvent newMgEvent = Newtonsoft.Json.JsonConvert.DeserializeObject <Events.ContinentUnlockEvent>(innerPayloadJSON);
                                    //raise event
                                    var args = new Events.World.ContinentUnlockEventArgs()
                                    {
                                        Payload = payload
                                    };
                                    RaiseContinentUnlockEventOnMainThread(args);
                                }
                            }
                        }
                        //await SendTestCommand
                    }
                }
            }
        }