【问题标题】:WebSocketSharp cannot catch exceptionsWebSocketSharp 无法捕获异常
【发布时间】:2021-10-03 20:24:24
【问题描述】:

我无法捕捉和处理 websocketsharp 抛出的异常

当我用客户端连接到服务器时,我只是按 ALT+F4 或 X 键关闭客户端,服务器收到连接被强制关闭的异常。

尽管这些异常不会使程序崩溃,但这会导致控制台被这些异常发送垃圾邮件,重要的消息和日志将被推送,并且无法读取任何内容(有很多客户端)。

这是个例外:

7/28/2021 6:41:56 AM|Fatal|<>c__DisplayClass71_0.<receiveRequest>b__0:0|WebSocketSharp.WebSocketException: An exception has occurred while reading an HTTP request/response.
                            ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..
                            ---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
                              --- End of inner exception stack trace ---
                              at System.Net.Sockets.NetworkStream.Read(Span`1 buffer)
                              at System.Net.Sockets.NetworkStream.ReadByte()
                              at WebSocketSharp.HttpBase.readHeaders(Stream stream, Int32 maxLength)
                              at WebSocketSharp.HttpBase.Read[T](Stream stream, Func`2 parser, Int32 millisecondsTimeout)
                              --- End of inner exception stack trace ---
                              at WebSocketSharp.HttpBase.Read[T](Stream stream, Func`2 parser, Int32 millisecondsTimeout)
                              at WebSocketSharp.HttpRequest.Read(Stream stream, Int32 millisecondsTimeout)
                              at WebSocketSharp.Net.WebSockets.TcpListenerWebSocketContext..ctor(TcpClient tcpClient, String protocol, Boolean secure, ServerSslConfiguration sslConfig, Logger logger)
                              at WebSocketSharp.Ext.GetWebSocketContext(TcpClient tcpClient, String protocol, Boolean secure, ServerSslConfiguration sslConfig, Logger logger)
                              at WebSocketSharp.Server.WebSocketServer.<>c__DisplayClass71_0.<receiveRequest>b__0(Object state)

我尝试过使用

    public class Echo : WebSocketBehavior
    {
        protected override void OnError(ErrorEventArgs e)
        {
            //handle
        }

我尝试将 websocket 服务器包装在 try catch 中

            try
            {
                string path = "ws://localhost:111";
                WebSocketServer wssv = new WebSocketServer(path);
                wssv.Start();
                while (Console.ReadKey(true).Key != ConsoleKey.Enter) { }
                wssv.Stop();
            } catch { }

没有任何效果,无论我尝试什么,异常都会抛出并显示在控制台上。 那么问题来了,如何捕捉这些异常呢?

【问题讨论】:

    标签: c# sockets exception websocket


    【解决方案1】:

    我能够解决完全禁用/隐藏日志输出的问题。

    public static void Disable(this Logger logger)
    {
        var field = logger.GetType().GetField("_output", BindingFlags.NonPublic | BindingFlags.Instance);
        field?.SetValue(logger, new Action<LogData, string>((d, s) => { }));
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-29
      • 1970-01-01
      • 2021-10-09
      • 2021-09-17
      • 2018-02-04
      • 2018-02-04
      • 2011-04-19
      • 2017-08-10
      相关资源
      最近更新 更多