-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (58 loc) · 1.31 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
//hapi server creation
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({ port: 8888 });
//server routing
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply.file('public/client.html');
}
});
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: 'public'
}
}
});
//painter vars
var rectarray = [];
function rect(xpos, ypos, width, height, color) {
this.x = xpos;
this.y = ypos;
this.w = width;
this.h = height;
this.col = color;
}
var recnum = 0;
//socketio
var io = require('socket.io')(server.listener);
//socket listeners
io.sockets.on('connection', function(socket) {
console.log("Client " + socket.id + " connected.");
socket.emit('array', rectarray);
socket.on('mouseupdate', function (data) {
rectarray[recnum] = new rect(data.x, data.y, 10, 10, data.col);
io.sockets.emit('rect', new rect(data.x, data.y, 10, 10, data.col));
recnum += 1;
});
socket.on('clear', function(data) {
rectarray = [];
recnum = 0;
io.sockets.emit('array', rectarray);
});
socket.on('auth', function (data) {
rectarray = [];
recnum = 0;
socket.emit('authresponse', true);
io.sockets.emit('array', rectarray);
});
});
//start hapi server
server.start(function () {
console.log('Server running at:', server.info.uri);
});