我是这样解决的:
我曾经将rabbitMq队列声明为durable=true,autoDelete=false,exclusive=false,并且在我的应用程序中有1个队列/用户和1个交换(类型=直接),routing_key name=queueName,我的应用程序也是使用与 android 应用程序或 iphone 应用程序等浏览器不同的其他客户端的队列作为推送后备,因此我使用为每个用户创建 1 个队列。
解决这个问题的方法是改变我的 rabbitMQ 队列和交换声明。现在我将交换/用户声明为 fanout 和 autoDelete=True,并且用户将有 N 个队列,其中持久性 =true、autoDelete=true、exclusive=true(No.queue = No.clients)并且所有队列都是绑定的到用户交换(多播)。
注意:我的应用程序是在 django 中编写的,我使用 node+socket+amqp 能够使用 web.scokets 与浏览器进行通信,所以我使用 node-restler 来查询我的应用程序 api 以获取用户队列信息。
这就是 rabbitMQ 方面,对于 node+amqp+socket 我这样做了:
服务器端:
- onConnect:声明用户交换为扇出、自动删除、持久。然后将队列声明为持久、自动删除和独占,然后 queue.bind 到用户交换,最后 queue.subscribe 和 socket.disconnect 将销毁队列,因此当客户端连接应用程序和这解决了刷新问题并允许用户在应用中拥有多个窗口选项卡:
服务器端:
/*
* unCaught exception handler
*/
process.on('uncaughtException', function (err) {
sys.p('Caught exception: ' + err);
global.connection.end();
});
/*
* Requiere libraries
*/
global.sys = require('sys');
global.amqp = require('amqp');
var rest = require('restler');
var io = require('socket.io').listen(8080);
/*
* Module global variables
*/
global.amqpReady = 0;
/*
* RabbitMQ connection
*/
global.connection = global.amqp.createConnection({
host: host,
login: adminuser,
password: adminpassword,
vhost: vhost
});
global.connection.addListener('ready',
function () {
sys.p("RabbitMQ connection stablished");
global.amqpReady = 1;
}
);
/*
* Web-Socket declaration
*/
io.sockets.on('connection', function (socket) {
socket.on('message', function (data) {
sys.p(data);
try{
var message = JSON.parse(data);
}catch(error){
socket.emit("message", JSON.stringify({"error": "invalid_params", "code": 400}));
var message = {};
}
var message = JSON.parse(data);
if(message.token != undefined) {
rest.get("http://dev.kinkajougames.com/api/push",
{headers:
{
"x-geochat-auth-token": message.token
}
}).on('complete',
function(data) {
a = data;
}).on('success',
function (data){
sys.p(data);
try{
sys.p("---- creating exchange");
socket.exchange = global.connection.exchange(data.data.bind, {type: 'fanout', durable: true, autoDelete: true});
sys.p("---- declarando queue");
socket.q = global.connection.queue(data.data.queue, {durable: true, autoDelete: true, exclusive: false},
function (){
sys.p("---- bind queue to exchange");
//socket.q.bind(socket.exchange, "*");
socket.q.bind(socket.exchange, "*");
sys.p("---- subscribing queue exchange");
socket.q.subscribe(function (message) {
socket.emit("message", message.data.toString());
});
}
);
}catch(err){
sys.p("Imposible to connection to rabbitMQ-server");
}
}).on('error', function (data){
a = {
data: data,
};
}).on('400', function() {
socket.emit("message", JSON.stringify({"error": "connection_error", "code": 400}));
}).on('401', function() {
socket.emit("message", JSON.stringify({"error": "invalid_token", "code": 401}));
});
}
else {
socket.emit("message", JSON.stringify({"error": "invalid_token", "code": 401}));
}
});
socket.on('disconnect', function () {
socket.q.destroy();
sys.p("closing socket");
});
});
客户端:
- 带有选项“强制新连接”=true 和“卸载时同步断开连接”=false 的套接字实例。
- 客户端使用onbeforeunload和onunload windows对象事件发送socket.disconnect
- socket.connect 事件中的客户端将用户令牌发送到节点。
-
处理来自套接字的消息
var socket;
function webSocket(){
//var socket = new io.Socket();
socket = io.connect("ws.dev.kinkajougames.com", {'force new connection':true, 'sync disconnect on unload': false});
//socket.connect();
onSocketConnect = function(){
alert('Connected');
socket.send(JSON.stringify({
token: Get_Cookie('liveScoopToken')
}));
};
socket.on('connect', onSocketConnect);
socket.on('message', function(data){
message = JSON.parse(data);
if (message.action == "chat") {
if (idList[message.data.sender] != undefined) {
chatboxManager.dispatch(message.data.sender, {
first_name: message.data.sender
}, message.data.message);
}
else {
var username = message.data.sender;
Data.Collections.Chats.add({
id: username,
title: username,
user: username,
desc: "Chat",
first_name: username,
last_name: ""
});
idList[message.data.sender] = message.data.sender;
chatboxManager.addBox(message.data.sender, {
title: username,
user: username,
desc: "Chat",
first_name: username,
last_name: "",
boxClosed: function(id){
alert("closing");
}
});
chatboxManager.dispatch(message.data.sender, {
first_name: message.data.sender
}, message.data.message);
}
}
});
}
webSocket();
window.onbeforeunload = function() {
return "You have made unsaved changes. Would you still like to leave this page?";
}
window.onunload = function (){
socket.disconnect();
}
就是这样,所以不要再循环消息了。