-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
54 lines (43 loc) · 1.6 KB
/
app.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
// Based on the tutorial:
// http://tutorialzine.com/2012/08/nodejs-drawing-game/
//Restrictions on HEROKU:
// Doesn't support installing dependencies with npm with node 0.8
// Doesn't support websocekts.
// Including libraries
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
nstatic = require('node-static'); // for serving files
// This will make all the files in the current folder
// accessible from the web
var fileServer = new nstatic.Server('./');
// This is the port for our web server.
// you will need to go to http://localhost:3000 to see it
var port = process.env.PORT || 80; // Cloud9 + Heroku || localhost
app.listen(port);
// If the URL of the socket server is opened in a browser
function handler (request, response) {
request.addListener('end', function () {
fileServer.serve(request, response);
});
}
// Delete this row if you want to see debug messages
io.set('log level', 1);
// Heroku doesn't support websockets so...
// Detect if heroku via config vars
// https://devcenter.heroku.com/articles/config-vars
// heroku config:add HEROKU=true --app node-drawing-game
if (process.env.HEROKU === 'true') {
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
}
// Listen for incoming connections from clients
io.sockets.on('connection', function (socket) {
// Start listening for mouse move events
socket.on('mousemove', function (data) {
// This line sends the event (broadcasts it)
// to everyone except the originating client.
socket.broadcast.emit('moving', data);
});
});