堆栈使用Socket.io,redis和RabbitMQ作为消息传递平台。 问题
连接套接字后,将创建到Rabbit mq的队列,在断开连接事件中存在使用者取消逻辑。问题在于互联网设置有问题,然后在执行队列创建的回调之后首先触发了断开连接,因此创建了孤立队列。
io.on('connection', async function(socket){
socket.on('initializationAcknowledged', function(){
// Creating a queue on rabbit MQ in auto delete setting
// That is the queue will exists till a consumer is present
// We cancel the consumer in on disconnect event
rabbitMQ.assertQueue('', {autoDelete: true, expires: 6000}, function(err, q) {
if (err) {
console.log('Error while asserting the consumer queue.');
};
// Bind the queue
consumer.bindQueue(q.queue, 'some exchange', socket.userID);
// Creating the consumer to consume on the created queue
consumer.consume(q.queue, function(msg) {
}, {noAck: true}, function(err, obj) {
if (err) {
console.log('There was an errow while assigning a consumer ');
}
// Setting the consumer tag on the socket to cancel consumer.
socket.consumerTag = obj.consumerTag;
});
});
})
// This is to make sure we have initialized and a stable internet connection
socket.emit('initialization')
socket.on('disconnect', function(){
// It fires the disconnet event before and finds the consumerTag as undefined hence does not cancel.
if (socket.consumerTag != undefined) {
consumer.cancel(socket.consumerTag, function(err, ok) {
if (err) {
console.log('Error while cancelling the consumer:', err);
}
if (ok) {
console.log('Consumer cancelled successfully', ok);
}
});
}
});
})
const socket = io.connect("some url")
socket.on('initialization', function(data){
// This is to make sure we have a stable internet connection
socket.emit('initializationAcknowledged');
}
答案 0 :(得分:0)
为什么不使用dns.lookup(url,callback)检查Internet连接? 如果回调返回true,则仅使用断开连接方法。