private void OnMessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
        {
            try
            {
                using (DataReader reader = args.GetDataReader())
                {
                    reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;

                    byte[] read = new byte[reader.UnconsumedBufferLength];
                    reader.ReadBytes(read);
                    try
                    {
                        WebSocketMessage message = WebSocketMessage.ParseFrom(read);

                        Debug.WriteLine("Message Type: " + message.Type);

                        if (message.Type == WebSocketMessage.Types.Type.REQUEST)
                        {
                            incomingRequests.AddFirst(message.Request);
                            MessageReceived(this, message.Request);
                        }
                    }
                    catch (InvalidProtocolBufferException e)
                    {
                        Debug.WriteLine(e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
Exemplo n.º 2
0
        private void OnMessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
        {
            try
            {
                using (DataReader reader = args.GetDataReader())
                {
                    reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;

                    byte[] read = new byte[reader.UnconsumedBufferLength];
                    reader.ReadBytes(read);
                    try
                    {
                        WebSocketMessage message = WebSocketMessage.ParseFrom(read);

                        Debug.WriteLine("Message Type: " + message.Type);

                        if (message.Type == WebSocketMessage.Types.Type.REQUEST)
                        {
                            incomingRequests.AddFirst(message.Request);
                            MessageReceived(this, message.Request);
                        }
                        else if (message.Type == WebSocketMessage.Types.Type.RESPONSE)
                        {
                            if (outgoingRequests.ContainsKey((long)message.Response.Id))
                            {
                                outgoingRequests[(long)message.Response.Id] = Tuple.Create((int)message.Response.Status,
                                                                                           Encoding.UTF8.GetString(message.Response.Body.ToByteArray()));
                            }
                        }
                    }
                    catch (InvalidProtocolBufferException e)
                    {
                        Debug.WriteLine(e.Message);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }