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

Websocket每30秒断连重连及异常发消息问题求助

问题描述

客户端基于Y.js + y-websocket实现,服务端采用uWebsocket,目前遇到两个异常:

  • 客户端每30秒会自动断开连接并立即重连
  • 原本仅需发送初始消息,但运行一段时间后会莫名发送四条消息(怀疑与严格模式有关)
服务端代码
const app = uWs.App().ws('/*', {
    compression: 0,
    maxPayloadLength: 16 * 1024 * 1024,
    idleTimeout: 10,
    open: (socket) => {
        console.log('A user connected');
        c = new Date().getTime()/1000
    },
    message: (socket, message, isBinary) => {
        console.log(`Received message: ${message}`);
    },
    drain: (socket) => {
        console.log('Socket backpressure: ' + socket.getBufferedAmount());
    },
    close: (socket) => {
        console.log('A user disconnected');
        console.log("Time from connection: ", new Date().getTime()/1000 - c)
    }
}).listen(3000, (listenSocket) => {
    if (listenSocket) {
        console.log('Server listening on port 3000');
    }
});
客户端代码
const yArray = useRef()
const wsProvider = useRef()

useEffect(() => {
    const ydoc = new Y.Doc();
    yArray.current = ydoc.getArray("lines")
    wsProvider.current = new WebsocketProvider('ws://localhost:3000', 'whiteboard', ydoc)
    }, [])

if(wsProvider.current){
    wsProvider.current.on('status', event => {
    console.log(event.status) // logs "connected" or "disconnected"
        })
    }
已尝试的无效操作
  • 修改服务端idleTimeout为0或最大值120,问题未解决
  • 回滚y-websocket至声称修复过该问题的1.3.8版本,无效果
  • 确认问题与useEffect无关,组件仅渲染一次且客户端无其他操作

期望连接建立后保持稳定,直到客户端主动关闭,求可行的解决方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 09:13:34