deniszykov / WebSocketListener

A lightweight and highly scalable asynchronous WebSocket listener
http://vtortola.github.io/WebSocketListener
81 stars 17 forks source link

websocket.isconnected = false after writeAsync #60

Closed renatosc closed 3 years ago

renatosc commented 3 years ago

I am porting my code from vtortola to deniszykov. My code works fine with vtortola (brower connects to WS, send request and get a streams of image frames) but when trying the same code after migrating the code my server only sends 1 frame since the webSocket.IsConnected becomes false and thus exit the loop where I handle the message (equivalent to the EchoAllIncomingMessagesAsync() method in the EchoServer sample).

Does this fork have any timeout of something like that that would explain why it gets disconnected after I do my messageWriterStream.WriteAsync ?

UPDATE: the websocket becomes disconnected after leaving the using block as shown below:

Using messageWriterStream As WebSocketMessageWriteStream = ws.CreateMessageWriter(WebSocketMessageType.Binary)    
         ' ws.IsConnected is True here
         Await messageWriterStream.WriteAsync(myData, 0, Int(myData.Length), cancellation)   
         ' ws.IsConnected is True here
End Using
' ws.IsConnected is False here  <<---
renatosc commented 3 years ago

I fixed replacing Await messageWriterStream.WriteAsync(myData, 0, Int(myData.Length), cancellation) with Await messageWriterStream.WriteAndCloseAsync(myData, 0, Int(myData.Length), cancellation)

deniszykov commented 3 years ago

Yep, writing messages requires some form of Close call. It could be messageWriterStream.CloseAsync() or messageWriterStream.WriteAndCloseAsync. This is shown in examples.

I recommend providing WebSocketListenerOptions.Logger during configuration of WSL. So it will be less difficult to diagnose such problems.