[英]React Websocket gets inactive after some time
I am using Azure Pub-Sub Service for Chatting module in a ReactApplication, I am creating this connection using Websocket.我在 ReactApplication 中使用 Azure Pub-Sub 服务进行聊天模块,我正在使用 Websocket 创建此连接。
let ws = new WebSocket(token.url);
ws.onmessage = (data) => {
//Messages Logic
}
when i am in other tabs, or in the sametab for longer time(more than 40-45 mins).当我在其他选项卡中,或在同一个选项卡中更长时间(超过 40-45 分钟)时。 I am not receiving messages, but when i refresh the page and websocket initialization code gets executed again and then i receive messages again.我没有收到消息,但是当我刷新页面并且 websocket 初始化代码再次执行时,我再次收到消息。 Any Suggestions?有什么建议么?
Use this technique :使用这种技术:
function connect() { var ws = new WebSocket('ws://localhost:8080'); ws.onopen = function() { // subscribe to some channels ws.send(JSON.stringify({ //.... some message the I must send when I connect .... })); }; ws.onclose = function(e) { console.log('Socket is closed. Reconnect will be attempted in 1 second.', e.reason); setTimeout(function() { connect(); }, 1000); };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.