-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
63 lines (44 loc) · 1.23 KB
/
server.js
File metadata and controls
63 lines (44 loc) · 1.23 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
let express = require('express');
let app = express();
let server = require('http').Server(app);
let io = require('socket.io')(server);
let nameObj = {};
let initChat = [];
let PORT = process.env.PORT || 5000;
/*
console.log(app);
console.log();
console.log(server);
console.log();
console.log(io);
console.log();
*/
app.use('/', express.static('./public'));
io.on('connection', function (socket) {
socket.on('name', function (data) {
console.log(data);
nameObj[socket.id] = data;
console.log('name');
console.log(nameObj);
io.sockets.emit('initConnect', nameObj);
});
socket.emit('chat', initChat);
socket.on('message', function (data) {
socket.broadcast.emit('reciveData', data);
console.log(data);
initChat.push(data);
});
socket.on('disconnect', function () {
delete nameObj[socket.id];
console.log('disconnect');
console.log(nameObj);
io.sockets.emit('initConnect', nameObj);
console.log(Object.keys(nameObj).length);
if (Object.keys(nameObj).length === 0) {
initChat = [];
}
})
});
server.listen(PORT, function () {
console.log('Server is listening on port ' + PORT);
});