简体   繁体   English

配置websocket始终开启

[英]Configure websocket always to be open

I use this code to connect to NodeJS web socket:我使用此代码连接到 NodeJS web 套接字:

useEffect(() => {      
        let futureResponseFeedAddress = "ws://localhost:/endpoint";
        const futureResponseClient = new W3CWebSocket(futureResponseFeedAddress);
        props.onUpdateOrderbookWebsocket(futureResponseClient);
        futureResponseClient.onopen = () => {
            console.log("WebSocket Client Connected on " + futureResponseFeedAddress);
        };
        futureResponseClient.onmessage = (message) => {
            ..............
        };
        futureResponseClient.onclose = closeEvent => {
            console.log("response messages websocket closed.");
        }

        return function cleanup() {
            futureResponseClient.close();
        };
    }, []); 

But after around 1-2 minutes the messages are not received.但是大约 1-2 分钟后没有收到消息。 Looks like I get timeout.看起来我得到了超时。 Do you know how I can configure the web socket to be always open?你知道我如何将 web 插座配置为始终打开吗?

My suggestion is you should use socket.io for doing any real-time communication.我的建议是您应该使用 socket.io 进行任何实时通信。 It uses http long polling,upgrades the connection to websocket if needed and it has also good browser support.它使用 http 长轮询,如果需要,升级到 websocket 的连接,它还具有良好的浏览器支持。 It is much realiable too.这也很现实。 But there are some drawbacks, your server need to use socket.io server api.但是有一些缺点,你的服务器需要使用 socket.io 服务器 api。

Socket.io-client Socket.io 客户端

Socket.io-server Socket.io-服务器

There is no need to re-invent the wheel.没有必要重新发明轮子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM