From 729dc5189cc8f40fa98b16fb8f14fe62fa2e4eef Mon Sep 17 00:00:00 2001 From: 沈斌 <bluelazysb@hotmail.com> Date: Mon, 07 Aug 2017 17:22:59 +0800 Subject: [PATCH] 家庭排名 --- queue.js | 42 +++++++++++++++++------------------------- 1 files changed, 17 insertions(+), 25 deletions(-) diff --git a/queue.js b/queue.js index b136fbb..3642351 100644 --- a/queue.js +++ b/queue.js @@ -1,33 +1,23 @@ /** * Created by bin.shen on 03/12/2016. */ - var amqp = require('amqplib/callback_api'); var moment = require('moment'); var uri = 'amqp://guest:guest@121.40.92.176'; -var ex = 'ex_data_dev1'; module.exports.publishMessage = function(data) { + this.pushToMQ('ex_data_dev1', { + data: data, + time: moment().format('YYYY-MM-DD HH:mm:ss') + }); +}; + +module.exports.pushToMQ = function(ex, data) { amqp.connect(uri, function(err, conn) { conn.createChannel(function(err, ch) { ch.assertExchange(ex, 'fanout', { durable: false }); - var message = '{"data": "' + data + '", "time":"' + moment().format('YYYY-MM-DD HH:mm:ss') + '"}'; - ch.publish(ex, '', new Buffer(message)); - }); - setTimeout(function() { - conn.close(); - //process.exit(0) - }, 500); - }); -}; - -module.exports.pushToScreen = function(data) { - amqp.connect(uri, function(err, conn) { - conn.createChannel(function(err, ch) { - var _ex = 'ex_data_screen'; - ch.assertExchange(_ex, 'fanout', { durable: false }); - ch.publish(_ex, '', new Buffer(JSON.stringify(data))); + ch.publish(ex, '', new Buffer(JSON.stringify(data))); }); setTimeout(function() { conn.close(); @@ -35,15 +25,17 @@ }); }; -module.exports.pushToAlarm = function(data) { +module.exports.listenToMQ = function(ex) { amqp.connect(uri, function(err, conn) { conn.createChannel(function(err, ch) { - var _ex = 'ex_data_alarm'; - ch.assertExchange(_ex, 'fanout', { durable: false }); - ch.publish(_ex, '', new Buffer(JSON.stringify(data))); + ch.assertExchange(ex, 'fanout', { durable: false }); + ch.assertQueue('', {exclusive: true}, function(err, q) { + ch.bindQueue(q.queue, ex, ''); + ch.consume(q.queue, function(msg) { + var message = JSON.parse(msg.content); + global.configs[message.mac] = message; + }, { noAck: true }); + }); }); - setTimeout(function() { - conn.close(); - }, 500); }); }; \ No newline at end of file -- Gitblit v1.8.0