vtortola WebSocketListener无法写入websocket

时间:2017-09-14 19:26:25

标签: vb.net websocket

我正在使用来自vtortola的WebSocketListener,我遇到了一些套接字抛出错误然后自行关闭的问题。如果我一次在套接字上请求太多数据,我认为它正在发生。我正在通过1个插槽传输多个jpeg图像。所以我每秒可以获得50多个图像请求。有时它发生得比其他人快,有时根本不会。

错误: aex.message =“不能在websocket上写” aex.innerException =“写入超时。”

我在vb.net API项目中实现了这个功能,该项目通过1个单一套接字返回所有图像。对于大多数过去它的工作正常,除了这个随机错误然后关闭套接字。

错误会在try / catch中被捕获,然后自行关闭。如果我注释掉ws.close()和ws.Dispose(),套接字仍会自行关闭。

Private Shared Async Function HandleConnectionAsync(ws As vtortola.WebSockets.WebSocket, cancellation As CancellationToken) As Task(Of Task)
    ' A request is made
    Console.WriteLine("Connection " + ws.RemoteEndpoint.Port.ToString() + " opened. " + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"))

    Try
        While ws.IsConnected AndAlso Not cancellation.IsCancellationRequested
            msg = Await ws.ReadStringAsync(cancellation).ConfigureAwait(False)
            If msg IsNot Nothing Then
                Using messageWriterStream As WebSocketMessageWriteStream = ws.CreateMessageWriter(WebSocketMessageType.Binary)
                    image = Await GetImage()
                    If image IsNot Nothing Then
                        Await messageWriterStream.WriteAsync(image, 0, Int(image.Length), cancellation).ConfigureAwait(False)
                    End If
                End Using
            End If
        End While
    Catch aex As Exception
        ws.Close()
    Finally
        ws.Dispose()
        Console.WriteLine("Connection " + ws.RemoteEndpoint.Port.ToString() + " closed. " + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"))
    End Try
End Function

1 个答案:

答案 0 :(得分:0)

在项目中回答并修复了here