-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (31 loc) · 903 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
let open = require('amqplib').connect('amqp://guest:guest@rabbit:5672');
let app = require('express')();
let http = require('http').Server(app);
let socketio = require('socket.io');
let io = socketio(http);
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
let queue = 'websocket';
// Consumer
open.then(function(conn) {
http.listen(8988, function(){
console.log('listening on *:8988');
});
return conn.createChannel();
}).then(function(channel) {
return channel.assertQueue(queue).then(function(ok) {
return channel.consume(queue, function(msg) {
if (msg !== null) {
io.emit('message', msg.content.toString());
console.log(msg.content.toString());
channel.ack(msg);
}
});
});
}).catch((error) => {
console.warn(error);
});