This repository was archived by the owner on May 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (39 loc) · 1.51 KB
/
Copy pathserver.js
File metadata and controls
44 lines (39 loc) · 1.51 KB
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
//////////////////////////////////////////////////////
// Import node modules
//////////////////////////////////////////////////////
var express = require('express'),
app = express(), // Create the app
http = require('http'),
socketIo = require('socket.io');
const PORT = process.env.PORT || 8080;
//////////////////////////////////////////////////////
// start webserver on port 8080
//////////////////////////////////////////////////////
var server = http.createServer(app);
var io = socketIo.listen(server);
server.listen(PORT);
console.log('Server Listening on ' + PORT);
//////////////////////////////////////////////////////
// Tell express web server where our html lives
//////////////////////////////////////////////////////
app.use(express.static(__dirname + '/www'));
//////////////////////////////////////////////////////
// Store the lines drawn
//////////////////////////////////////////////////////
var linesDrawn = [];
//////////////////////////////////////////////////////
// Hook up events for new connections that come in to the server
//////////////////////////////////////////////////////
io.on('connection', function (socket) {
// Send all history to the client that connected
for (var i in linesDrawn) {
socket.emit('drawEvent', { line: linesDrawn[i] } );
}
// Add "drawEvent" message handler.
socket.on('drawEvent', function (data) {
// Add received line to history
linesDrawn.push(data.line);
// Send line to all clients
io.emit('drawEvent', { line: data.line });
});
});