我有基本脚本来获取amqp消息并将它们转发到socket.io 它可以工作数小时,有时持续数天,但最终它会在继续发出心跳的同时静默地停止转发amqp消息。
日志显示当它到达破坏状态时,它不会尝试在amqp消息到达时发出socket.io消息,所以我认为问题在于amqp connection / subscription而不是socket.io
我在脚本运行时测试了手动关闭并重新启动rabbitmq服务器,它似乎没有问题重新连接并继续按预期运行。
var port = 8888,
http = require("http"),
amqp = require("amqp"),
socketio = require("socket.io"),
express = require("express"),
rabbitMq = amqp.createConnection({ host: 'server', reconnect: true }),
app = express(),
server = http.createServer(app),
io = socketio.listen(server);
rabbitMq.on("ready", function () {
console.log("ready");
var queue = rabbitMq.queue("ft-secondary-display", function () {
queue.bind(rabbitMq.exchange('ft', {type: 'topic'}), "data");
console.log("bound");
queue.subscribe(function (message, headers, basicInfo) {
var data = {'key': basicInfo, 'data': message.data.toString()};
io.sockets.emit("message-name", data);
});
});
});
setInterval(hb, 10000);
function hb(){
io.sockets.emit("message-name", "heartbeat");
}