You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WebSocketSharp服务器运行约55秒后停止工作求助

WebSocketSharp服务器运行约55秒崩溃问题排查求助

问题描述

使用C#的WebSocketSharp框架搭建WebSocket服务器,Python客户端每秒发送测试数据,但服务器运行约55秒左右(有时54秒、有时56秒)就崩溃,报错信息如下:

Server started. Listening for incoming connections...
55 Received message: hello server
1/12/2023 7:38:25 AM|Fatal|WebSocket.<startReceiving>b__176_2|System.ObjectDisposedException: Cannot access a disposed object.
                           Object name: 'System.Net.Sockets.NetworkStream'.
                              at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
                              at WebSocketSharp.Ext.<>c__DisplayClass48_0.<ReadBytesAsync>b__0(IAsyncResult ar)

已尝试在代码中添加try/catch块,但无法定位错误发生的位置,现将服务器与客户端代码附上,请求协助排查问题。

C# 服务器代码

using System;
using WebSocketSharp;
using WebSocketSharp.Server;

public class Echo : WebSocketBehavior
{
    protected override void OnMessage(MessageEventArgs e)
    {
        try {
            Console.WriteLine("Received message: " + e.Data);
            Sessions.Broadcast(e.Data);
        }
        catch{
            Console.WriteLine("bad happened");
        }
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        var wssv = new WebSocketServer(8081);
        wssv.AddWebSocketService<Echo>("/Echo");
        wssv.Start();
        Console.WriteLine("Server started. Listening for incoming connections...");
        Console.ReadLine();
        wssv.Stop();
    }
}

Python 客户端代码

import time
import websocket


def send_words(ws,words):
    data = words
    ws.send(data)


ws = websocket.create_connection("ws://localhost:8081/Echo")


while True:
    time.sleep(1)
    data = "hello server"
    send_words(ws,data)

内容的提问来源于stack exchange,提问作者Ivan Viti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 20:16:05