-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
executable file
·99 lines (68 loc) · 2.07 KB
/
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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
var express = require('express');
var queues = require('./queues');
var WebSocketServer = require("ws").Server;
var http = require('http');
var app = express();
var port = process.env.PORT || 3000;
// parse json posts
var bodyParser = require('body-parser');
app.use( bodyParser.json() );
app.use(express.static(__dirname + '/public'));
app.disable('etag');
app.post('/requeue', function(req, res) {
queues.requeue(req.body.from, req.body.deliveryTag)
.then(function() {
res.status(204).end();
})
.catch(function(err) {
console.log(err);
res.status(500);
res.json({error: err.toString()});
});
});
app.post('/delete', function(req, res) {
queues.delete(req.body.from, req.body.deliveryTag)
.then(function() {
res.status(204).end();
})
.catch(function(err) {
console.log(err);
res.status(500);
res.json({error: err.toString()});
});
});
app.get('/queues/:queue/items', function(req, res) {
queues.peek(req.params.queue)
.then(res.json.bind(res))
.catch(function(err) {
console.log(err);
res.status(500);
res.json({error: err.toString()});
})
.done();
});
app.get('*', function(request, response){
response.sendFile(__dirname + '/public/index.html');
});
// http server
var server = http.createServer(app);
server.listen(port, function() {
console.log("Listening on port %d", port);
});
// Websocket server
var wss = new WebSocketServer({server: server});
console.log("websocket server created");
wss.on("connection", function(ws) {
console.log('url was: ' + ws.upgradeReq.url);
path = ws.upgradeReq.url;
exchange = '';
if (path && (path.substring(0,1) == '/'))
exchange = path.substring(1);
console.log(exchange);
queues.startListening(exchange, function(msg) { ws.send(JSON.stringify(msg)); });
console.log("websocket connection open");
ws.on("close", function() {
console.log("websocket connection close");
});
});