DataWriter StoreAsync() throws exception

0

I got an issue as part of remote websocket server accessing ('ws://172.17.35.104:8080/WebsocketHttpListenerDemo') from UWP app.

DataWriter StoreAsync() throws exception . A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E)

please refer the below sample code (client):

            private async void Button_Click(object sender, RoutedEventArgs e)
            {
                Connect("ws://172.17.35.104:8080/WebsocketHttpListenerDemo").Wait();
            }

            public async Task Connect(string uri)
            {
                await Task.Delay(100); //wait for a sec, so server starts and ready to accept 
                // await Task.Delay(TimeSpan.FromSeconds(1));
                messageWebSocket = new Windows.Networking.Sockets.MessageWebSocket();
                messageWebSocket.Control.MessageType = Windows.Networking.Sockets.SocketMessageType.Utf8;
                messageWebSocket.MessageReceived += WebSocket_MessageReceived;
                messageWebSocket.Closed += WebSocket_Closed;
                try{
                    Task connectTask = messageWebSocket.ConnectAsync(new Uri(uri)).AsTask();
                    await connectTask.ContinueWith(_ => this.SendMessageUsingMessageWebSocketAsync("Hello, World!"));
                    await Task.Delay(1000);
                }
                catch (Exception ex)
                {}
                finally
                { }
            }
            private async Task SendMessageUsingMessageWebSocketAsync(string message)
            {
                try
                {
                    using (var dataWriter = new DataWriter(this.messageWebSocket.OutputStream))
                    {
                    dataWriter.WriteString(message);
                    await dataWriter.StoreAsync();
                    dataWriter.DetachStream();
                    }
                }
                catch (Exception ex)
                {}
            }

server side code:

WebsocketServer websocketServer = new WebsocketServer(); websocketServer.Start("http://172.17.35.104:8080/WebsocketHttpListenerDemo/");

            ------------
            public async void Start(string httpListenerPrefix)
            {
                HttpListener httpListener = new HttpListener(); 

                httpListener.Prefixes.Add(httpListenerPrefix);  

                httpListener.Start();   

                Console.WriteLine("Listening...");

                while (true){
                        HttpListenerContext httpListenerContext = await httpListener.GetContextAsync();

                        ProcessRequest(httpListenerContext);  
                        }
            }

            private async void ProcessRequest(HttpListenerContext httpListenerContext)
            {
                WebSocketContext webSocketContext = await httpListenerContext.AcceptWebSocketAsync(subProtocol: null);

                WebSocket webSocket = webSocketContext.WebSocket;

                byte[] receiveBuffer = new byte[1024];

                while (webSocket.State == WebSocketState.Open)
                {
                    WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), 
                                                        CancellationToken.None);    
                }  
            }   
websocket
uwp
asked on Stack Overflow Feb 2, 2018 by sumith • edited Feb 6, 2018 by sumith

1 Answer

0

Please refer the Official WebSocket sample and look into the Run the sample part in the sample README.md file to Configure your web server.

I tried to use the sample provided web server scripts and connect to the "ws://localhost/WebSocketSample/EchoWebSocket.ashx" server address to test your above code, it can work well. You can also try the Official WebSocket sample or the sample localhost server.

answered on Stack Overflow Feb 5, 2018 by Breeze Liu - MSFT

User contributions licensed under CC BY-SA 3.0