在Node.js应用程序中自动重新连接Stomp.js [英] Automatic reconnect with Stomp.js in Node.js application

查看:726
本文介绍了在Node.js应用程序中自动重新连接Stomp.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个用Node.js和Express编写的应用程序,我正在尝试使用 Stomp.js客户端连接到ActiveMQ服务器。



我可以让应用程序连接到ActiveMQ只是使用Stomp,但我无法使系统在连接失败时自动重新连接。似乎失败函数只有在连接最初成功但后来被切断的情况下被调用,虽然当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:在10秒钟内重建);
};

function stompConnect(){
console.log('STOMP:Attempting connection');
stompClient.connect('login','password',stompSuccessCallback,stompFailureCallback);

}

stompConnect();有没有人知道这里发生了什么?

解决方案

Stomp.client所持有的WebSocket只能打开一次。如果出现网络故障,请重新连接相同的StompClient将不起作用,因为网络套接字将保持关闭。



这可以通过stomp.js改进,但在平均时间,您可以通过在检测到故障时重新创建Stomp.client来解决此问题。如下所示:

  var stompClient; 

var stompFailureCallback = function(error){
console.log('STOMP:'+ error);
setTimeout(stompConnect,10000);
console.log('STOMP:在10秒钟内重建);
};

function stompConnect(){
console.log('STOMP:Attempting connection');
//重新创建踩脚板来使用新的WebSocket
stompClient = Stomp.overTCP('localhost',61612);
stompClient.connect('login','password',stompSuccessCallback,stompFailureCallback);
}


I'm working with an application that is written in Node.js and Express, and I'm trying to use the Stomp.js client to connect to an ActiveMQ server.

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.

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?

解决方案

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.

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:

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);
}

这篇关于在Node.js应用程序中自动重新连接Stomp.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆