-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
37 lines (29 loc) · 932 Bytes
/
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
var express = require('express');
var config = require('./env');
console.log(config);
app = express(),
http = require('http'),
socketIo = require('socket.io');
// start webserver on port 8080
var server = http.createServer(app);
var io = socketIo.listen(server);
server.listen(config.port);
// add directory with our static files
app.use(express.static(__dirname + '/public'));
console.log("Server running on 127.0.0.1:8080");
// array of all lines drawn
var line_history = [];
// event-handler for new incoming connections
io.on('connection', function (socket) {
// first send the history to the new client
for (var i in line_history) {
socket.emit('draw_line', { line: line_history[i] } );
}
// add handler for message type "draw_line".
socket.on('draw_line', function (data) {
// add received line to history
line_history.push(data.line);
// send line to all clients
io.emit('draw_line', { line: data.line });
});
});