WebSocket每30秒断开和连接一次+随机发送消息

kninwzqo  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(306)

Screenshot of terminal logs
在客户端,我使用y-websocket和Y.js,服务器端使用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"
        })
    }


将idleEffect更改为0会得到相同的结果。不应该是任何与useEffect相关的内容,因为它只呈现一次,客户端当前不做任何事情。
尝试将其恢复到版本1.3.8,这是应该修复它的版本。将空闲时间更改为120,这是最高值并不会改变任何东西。我希望建立连接并保持连接,直到客户端离开/关闭。所有帮助都非常感谢!

sulc1iza

sulc1iza1#

const [{ store, ws }] = useState(() => {
    const store = syncedStore({
      item: [] as {
        item: string;
      }[],
    });

    const ws = new WebsocketProvider(
      "ws://localhost:3000",
      "test-room",
      getYjsDoc(store)
    );

    return { store, ws };
  });```

By using useState (should still only run once) and putting it in a syncedStore we managed to keep the connection from disconnecting. using useState instead of useEffect solved problem.

字符串

相关问题