示例#1
0
        private void WebSocket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
        {
            string serialized = "";

            try
            {
                using (DataReader dataReader = args.GetDataReader())
                {
                    dataReader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                    serialized = dataReader.ReadString(dataReader.UnconsumedBufferLength);
                    Debug.WriteLine("Message received from MessageWebSocket: " + serialized);
                }
            }
            catch (Exception ex)
            {
                Windows.Web.WebErrorStatus webErrorStatus = WebSocketError.GetStatus(ex.GetBaseException().HResult);
                // Add additional code here to handle exceptions.
            }

            DispatchMessage(serialized);
        }
示例#2
0
        public async void EstablishConnectionAsync()
        {
            while (context.MyWebSocket != null)
            {
                await Task.Delay(1000);
            }
            context.MyWebSocket = new MessageWebSocket();

            context.MyWebSocket.Control.MessageType = SocketMessageType.Utf8;
            context.MyWebSocket.MessageReceived    += WebSocket_MessageReceived;
            context.MyWebSocket.Closed += WebSocket_Closed;

            try
            {
                connectTask = context.MyWebSocket.ConnectAsync(new Uri("ws://localhost:8123")).AsTask();
            }
            catch (Exception ex)
            {
                Windows.Web.WebErrorStatus webErrorStatus = WebSocketError.GetStatus(ex.GetBaseException().HResult);
            }
        }
示例#3
0
        private async void ReceiveMessageUsingStreamWebSocket()
        {
            try
            {
                using (var dataReader = new DataReader(this.streamWebSocket.InputStream))
                {
                    dataReader.InputStreamOptions = InputStreamOptions.Partial;
                    await dataReader.LoadAsync(256);

                    byte[] message = new byte[dataReader.UnconsumedBufferLength];
                    dataReader.ReadBytes(message);
                    Debug.WriteLine("Data received from StreamWebSocket: " + message.Length + " bytes");
                }
                this.streamWebSocket.Dispose();
            }
            catch (Exception ex)
            {
                Windows.Web.WebErrorStatus webErrorStatus = Windows.Networking.Sockets.WebSocketError.GetStatus(ex.GetBaseException().HResult);
                // Add code here to handle exceptions.
            }
        }
示例#4
0
        //使用了 WebSocket.org 回显服务器 — 将服务回显到向其发送任一消息的发送方。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.messageWebSocket = new Windows.Networking.Sockets.MessageWebSocket();

            // In this example, we send/receive a string, so we need to set the MessageType to Utf8.
            this.messageWebSocket.Control.MessageType = Windows.Networking.Sockets.SocketMessageType.Utf8;

            this.messageWebSocket.MessageReceived += WebSocket_MessageReceived;
            this.messageWebSocket.Closed          += WebSocket_Closed;

            try
            {
                Task connectTask = this.messageWebSocket.ConnectAsync(new Uri("wss://echo.websocket.org")).AsTask();
                connectTask.ContinueWith(_ => this.SendMessageUsingMessageWebSocketAsync("Hello, World!"));
            }
            catch (Exception ex)
            {
                Windows.Web.WebErrorStatus webErrorStatus = Windows.Networking.Sockets.WebSocketError.GetStatus(ex.GetBaseException().HResult);
                // Add additional code here to handle exceptions.
            }
        }
示例#5
0
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.streamWebSocket = new Windows.Networking.Sockets.StreamWebSocket();

            this.streamWebSocket.Closed += WebSocket_Closed;

            try
            {
                Task connectTask = this.streamWebSocket.ConnectAsync(new Uri("wss://echo.websocket.org")).AsTask();

                connectTask.ContinueWith(_ =>
                {
                    Task.Run(() => this.ReceiveMessageUsingStreamWebSocket());
                    Task.Run(() => this.SendMessageUsingStreamWebSocket(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 }));
                });
            }
            catch (Exception ex)
            {
                Windows.Web.WebErrorStatus webErrorStatus = Windows.Networking.Sockets.WebSocketError.GetStatus(ex.GetBaseException().HResult);
                // Add code here to handle exceptions.
            }
        }