-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
49 lines (38 loc) · 951 Bytes
/
app.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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var uuid = require('uuid');
var users = [];
app.use(express.static(path.resolve(__dirname, 'public')));
app.get('/', function( req, res ) {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket) {
var thisName;
socket.on('adduser', function(name) {
thisName = name;
users.push({
name: name,
flag: uuid.v1()
});
io.emit('adduser-todom', users);
});
socket.on('disconnect', function() {
var thisUser;
users = users.filter(function(user, index) {
if (user.name === thisName) {
thisUser = user;
return false;
} else {
return true;
}
});
io.emit('user-disconnect', thisUser);
});
socket.on('send-message', function(msg) {
io.emit('send-message-todom', msg);
});
});
http.listen(process.env.PORT || 5000);