Просмотр исходного кода

add maintenance and monitoring routes.

* `/world` to retrieve the current world state
* `/stats` for some basic statistics (# of users and pixls)
* `/reset` to reset the world state (not sure if forcing every client to
  clean the canvas is "right", maybe should not broadcast this?)
Lucas Stadler лет назад: 12
Родитель
Сommit
b99be1407c
1 измененных файлов с 24 добавлено и 1 удалено
  1. 24 1
      js/pixl/server.js

+ 24 - 1
js/pixl/server.js

@ -5,6 +5,29 @@ var express = require('express');
5 5
var app = express();
6 6
app.use(express.static(__dirname + "/public"));
7 7
8
app.get('/stats', function(req, res) {
9
	var stats = {
10
		users: wss.clients.length,
11
		pixls: Object.keys(world).length
12
	};
13
	res.setHeader('Content-Type', 'text/plain');
14
	res.send(JSON.stringify(stats, null, "  "));
15
});
16
17
app.get('/world', function(req, res) {
18
	res.setHeader('Content-Type', 'text/plain');
19
	res.send(JSON.stringify(world));
20
});
21
22
app.get('/reset', function(req, res) {
23
	world = {};
24
	wss.clients.forEach(function(client) {
25
		client.send("{}");
26
	});
27
	res.setHeader('Content-Type', 'text/plain');
28
	res.send(JSON.stringify({status: "ok"}));
29
});
30
8 31
var server = http.createServer(app);
9 32
server.listen(8001);
10 33
@ -28,4 +51,4 @@ wss.on('connection', function(socket) {
28 51
			}
29 52
		});
30 53
	});
31
});
54
});