This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (51 loc) · 1.89 KB
/
index.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
var Express = require('express'),
Routes = require('./routes'),
HTTP = require('http'),
R = require('rethinkdb'),
Path = require('path');
var App = Express(),
Server = HTTP.createServer(App),
IO = require('socket.io')(Server);
// configure the App itself
App.set('port', process.env.PORT || 3000);
App.set('views', __dirname + '/views');
App.set('view engine', 'jade');
//App.use(require('serve-favicon'));
App.use(require('morgan')('combined'));
App.use(require('body-parser').urlencoded({extended: false}));
// App.use(require('method-override'));
App.use(require('cookie-parser')('a cookie secret'));
//App.use(require('express-session')('a session secret'));
// App.use(require('less-middleware')({src: __dirname + '/public'}));
App.use(Express.static(Path.join(__dirname, 'public')));
App.use(require('errorhandler')());
App.get('/', Routes.index);
var currentUsers = 0,
currentCanvas = '';
IO.on('connection', function(user) {
console.log("User Connected:" + user.id);
user.on('started drawing', function(point) {
point.userId = user.id;
user.broadcast.emit('started drawing', point);
});
user.on('drawing move', function(point) {
point.userId = user.id;
user.broadcast.emit('drawing move', point);
});
user.on('ended drawing', function(canvas) {
user.broadcast.emit('ended drawing', {userId: user.id});
currentCanvas = canvas.image;
});
user.on("RESET", function() {
console.log('User ' + user.id + 'requested a full reset');
user.broadcast.emit("RESET");
});
IO.emit('userCount', {users: ++currentUsers});
user.on('disconnect', function() {
IO.emit('userCount', {users: --currentUsers});
});
user.emit('canvas load', {uri: currentCanvas});
});
Server.listen(App.get('port'), function() {
console.log('Listening on http://localhost:' + App.get('port'))
});