-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrabbit-web-bridge.js
76 lines (67 loc) · 1.76 KB
/
rabbit-web-bridge.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Web Server.
*/
var server = require('http').createServer(handler), io = require('socket.io')
.listen(server), fs = require('fs');
server.listen(12345);
function handler(req, res) {
fs.readFile(__dirname + '/index.html', function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
// res.writeHead(200, {
// 'Access-Control-Allow-Origin':'http://jslate.com/'
// });
res.writeHead(200);
res.end(data);
});
}
/*
* Web Client operations/
*/
io.sockets.on('connection', function(socket) {
socket.set('subs', []);
socket.on('subscribe', function(data, fn) {
var subscription = stompClient.subscribe('/exchange/events/' + data.island + "." + data.actor, function(message) {
if (message.body) {
console.log('Received ' + message.body);
socket.emit('update', message.body);
}
});
console.log('New client subscribed: ' + subscription.id);
fn(subscription.id);
socket.get('subs', function(err, subs) {
subs[subscription.id] = subscription;
});
});
socket.on('unsubscribe', function(data) {
socket.get('subs', function(err, subs) {
console.log("Disconnecting " + data.id);
if (subs.hasOwnProperty(data.id)) {
subs[data.id].unsubscribe();
delete subs[data.id];
}
});
});
socket.on('disconnect', function() {
socket.get('subs', function(err, subs) {
for (var subid in subs) {
console.log("Disconnecting " + subid);
if (subs.hasOwnProperty(subid)) {
subs[subid].unsubscribe();
}
}
});
});
});
/*
* STOMP.
*/
var Stomp = require('stompjs');
var stompClient = Stomp.overTCP('localhost', 61613);
stompClient.connect('guest', 'guest', function() {
console.log("STOMP connection: OK.");
}, function(error) {
console.log("STOMP connection: " + error);
});