javascript 在 Node.js 应用程序中自动重新连接 Stomp.js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22361917/
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
Automatic reconnect with Stomp.js in Node.js application
提问by Michael Oryl
I'm working with an application that is written in Node.js and Express, and I'm trying to use the Stomp.js clientto connect to an ActiveMQ server.
我正在使用一个用 Node.js 和 Express 编写的应用程序,我正在尝试使用Stomp.js 客户端连接到 ActiveMQ 服务器。
I can get the application to connect to ActiveMQ just fine using Stomp, but I am unable to get the system to automatically reconnect upon connection failure. It seems like the failure function is only called if the connection is initially successful and then later severed, though if ActiveMQ is already down when the Node app starts, I do see the error message that proves the failure function was called.
我可以让应用程序使用 Stomp 很好地连接到 ActiveMQ,但是我无法让系统在连接失败时自动重新连接。似乎只有在连接最初成功然后断开连接时才会调用失败函数,但如果在 Node 应用程序启动时 ActiveMQ 已经关闭,我确实会看到证明调用了失败函数的错误消息。
var Stomp = require('stompjs');
var stompClient = Stomp.overTCP('localhost', 61612);
var stompStatus = false;
var stompSuccessCallback = function (frame) {
stompStatus = true;
console.log('STOMP: Connection successful');
};
var stompFailureCallback = function (error) {
stompStatus = false;
console.log('STOMP: ' + error);
setTimeout(stompConnect, 10000);
console.log('STOMP: Reconecting in 10 seconds');
};
function stompConnect() {
console.log('STOMP: Attempting connection');
stompClient.connect('login', 'password', stompSuccessCallback, stompFailureCallback);
}
stompConnect();
Does anybody have any idea what's going on here?
有人知道这里发生了什么吗?
回答by jmesnil
The WebSocket that is held by the Stomp.client can only be opened once. If there is a network failure, reconnecting with the same StompClient will not work as the web socket will remain closed.
Stomp.client 持有的 WebSocket 只能打开一次。如果出现网络故障,重新连接同一个 StompClient 将不起作用,因为 Web 套接字将保持关闭状态。
This can definitely be improved by stomp.js but in the mean time, you can workaround this by recreating a Stomp.client when a failure is detected. Something like:
这绝对可以通过 stomp.js 改进,但与此同时,您可以通过在检测到故障时重新创建 Stomp.client 来解决此问题。就像是:
var stompClient;
var stompFailureCallback = function (error) {
console.log('STOMP: ' + error);
setTimeout(stompConnect, 10000);
console.log('STOMP: Reconecting in 10 seconds');
};
function stompConnect() {
console.log('STOMP: Attempting connection');
// recreate the stompClient to use a new WebSocket
stompClient = Stomp.overTCP('localhost', 61612);
stompClient.connect('login', 'password', stompSuccessCallback, stompFailureCallback);
}
回答by Deepak Kumar
The original sompjs is no longer maintained. Please use https://github.com/stomp-js/stomp-websocketThis version has support for automatic re-connection. On each successful connection, the connect callback is called where you can do your subscribes.
不再维护原始的 sompjs。请使用https://github.com/stomp-js/stomp-websocket该版本支持自动重连。每次成功连接时,都会调用 connect 回调,您可以在其中进行订阅。
If you are using Angular 2, 4, or 5. You should look at https://github.com/stomp-js/ng2-stompjsThis package not only support automatic re-connection, but it will also re-subscribe all queues and send any pending messages.
如果你使用的是 Angular 2、4 或 5。你应该看看https://github.com/stomp-js/ng2-stompjs这个包不仅支持自动重新连接,而且还会重新订阅所有队列并发送任何待处理的消息。