javascript SockJS 客户端自动重新连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18930786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:43:17  来源:igfitidea点击:

SockJS Client auto reconnect?

javascriptsockjs

提问by Alosyius

I'm trying to figure out a way for my SockJS clients to reconnect to the server if it should go down.

我试图找出一种方法,让我的 SockJS 客户端在服务器出现故障时重新连接到服务器。

I currently have this:

我目前有这个:

    new_conn = function() {    
        socket = new SockJS(protocol + serverDomain + '/echo', null, {
            'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 'iframe-eventsource', 'iframe-htmlfile', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling']
        });
    };

    socket.onopen = function () {
        clearInterval(recInterval);
    };  

    socket.onclose = function () {    
        recInterval = window.setInterval(function () {
            new_conn();
        }, 2000);
    }; 

The problem is that the setIntervalkeeps firing even after a successful reconnect. It seems that the socket.onopennever gets executed.

问题是setInterval即使成功重新连接后,它仍然会继续发射。似乎socket.onopen永远不会被执行。

Any ideas what I could be doing wrong?

任何想法我可能做错了什么?

回答by franzlorenzon

I think it could be related to variable scoping. Try this:

我认为这可能与变量范围有关。试试这个:

var recInterval = null;

new_conn = function() {    
    socket = new SockJS(protocol + serverDomain + '/echo', null, {
        'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 'iframe-eventsource', 'iframe-htmlfile', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling']
    });
};

socket.onopen = function () {
    clearInterval(recInterval);
};  

socket.onclose = function () {    
    recInterval = window.setInterval(function () {
        new_conn();
    }, 2000);
}; 

Anyway, it's strange, because you were declaring recIntervalon the windowobject, and it should have worked. If it doesn't work, you could also debug it with a browser, with debugger;statements or interactively by setting local breakpoints... (in onopen, for example).

无论如何,这很奇怪,因为您正在声明recIntervalwindow对象,并且它应该有效。如果它不起作用,您还可以使用浏览器,使用debugger;语句或通过设置本地断点以交互方式调试它......(onopen例如,in )。

By the way, I rewrited the whole code like this (I like refactoring:):

顺便说一句,我把整个代码改写成这样(我喜欢重构:):

var recInterval = null;
var socket = null;

var new_conn = function() {    
    socket = new SockJS(protocol + serverDomain + '/echo', null, {
        'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 
                                'iframe-eventsource', 'iframe-htmlfile', 
                                'xdr-polling', 'xhr-polling', 'iframe-xhr-polling',
                                'jsonp-polling']
    });

    socket.onopen = function () {
        clearInterval(recInterval);
    };  

    socket.onclose = function () {    
        recInterval = setInterval(function () {
            new_conn();
        }, 2000);
    };
};

回答by daPhantom

In case anyone still interested in this topic: The refactored code snippet from franzlorenzon causes a lot of reconnects since it is kind of recursive reconnecting itself somehow since each two seconds a new onclose event is spawned (regardless of the recInterval).

如果有人仍然对这个主题感兴趣:来自 franzlorenzon 的重构代码片段会导致大量重新连接,因为它是一种递归重新连接自身,因为每两秒会产生一个新的 onclose 事件(无论 recInterval 是多少)。

Moving the clear interval right after the socket creation does the trick. I also added a socket = null in the onclose event.

在创建套接字后立即移动清除间隔就可以了。我还在 onclose 事件中添加了一个 socket = null 。

var recInterval = null;
var socket = null;

var new_conn = function() {
  socket = new SockJS(protocol + serverDomain + '/echo', null, {
    'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming',
      'iframe-eventsource', 'iframe-htmlfile',
      'xdr-polling', 'xhr-polling', 'iframe-xhr-polling',
      'jsonp-polling'
    ]
  });

  clearInterval(recInterval);

  socket.onopen = function() {

  };

  socket.onclose = function() {
    socket = null;
    recInterval = setInterval(function() {
      new_conn();
    }, 2000);
  };
};