Skip to content

Instantly share code, notes, and snippets.

@minimal
Created May 5, 2010 16:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minimal/391079 to your computer and use it in GitHub Desktop.
Save minimal/391079 to your computer and use it in GitHub Desktop.
/* JSON-Websocket-AMQP bridge */
var amqp_ws = function() {
ws = null;
function recieve_amqp(args) {
console.log(args.msg);
};
var rpc_methods = {
msg: recieve_amqp
};
function handle_ws_msg(msg_event) {
console.log(msg_event);
var msg = JSON.parse(msg_event.data);
console.log(msg);
rpc_methods[msg['_action']](msg['args']);
};
function openWebsocket(host) {
amqp_ws.ws = new WebSocket(host);
amqp_ws.ws.onmessage = handle_ws_msg;
amqp_ws.ws.onclose = function(msg){console.log("Closed")};
amqp_ws.ws.onopen = function(msg){console.log("Open ws")};
//amqp_ws.ws.onclose =
}
function startQueue(name, exchange, routing_key) {
/* send a message to start a queue */
var msg = {
_action: "start-queue",
args: {name: name,
exchange: exchange,
"routing-key": routing_key
}
};
amqp_ws.ws.send(JSON.stringify(msg));
}
function stopQueues() {
/* stop all queues */
var msg = {
_action: "stop-queue",
args: {name: null
}
};
amqp_ws.ws.send(JSON.stringify(msg));
}
return {
ws: ws,
handle: handle_ws_msg,
openSocket: openWebsocket,
startQueue: startQueue,
stopQueues: stopQueues
};
}();
/* Usage */
function usage() {
amqp_ws.openSocket('ws://localhost:8090');
amqp_ws.startQueue("mongoose", "amq.topic", "#");
amqp_ws.stopQueues();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment