-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
47 lines (41 loc) · 1.33 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require("path");
var port = process.env.PORT || 3001;
http.listen(port, function() {
console.log('Server running on port ' + port);
});
app.use(express.static(__dirname + '/dist/notepad-app'));
app.get('/', function(req, res) {
console.log(__dirname);
res.sendFile(path.join(__dirname + '/dist/notepad-app/index.html'));
});
var allClients = {};
io.on('connection', function (socket) {
emitClientSelf(allClients, socket);
socket.on('recieveUserName', function(data){
allClients[socket.id] = data;
socket.broadcast.emit('users', allClients);
});
socket.on('draw', function (data) {
socket.broadcast.emit('draw', data);
});
socket.on('logout', function () {
console.log("logout");
delete allClients[socket.id];
emitClientSelf(allClients, socket);
socket.broadcast.emit('users', allClients);
});
socket.on('disconnect', function (data) {
console.log("disconnect");
delete allClients[socket.id];
emitClientSelf(allClients, socket);
socket.broadcast.emit('users', allClients);
});
});
function emitClientSelf(client, socket) {
delete client[socket.id];
socket.emit('users', client);
}