-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (34 loc) · 999 Bytes
/
index.js
File metadata and controls
44 lines (34 loc) · 999 Bytes
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
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
var clients = {}
// initialize IO
io.on('connection', (socket) => {
socket.on('new-client',(userName) => {
// clients.push(userName);
for(i in clients){
if(clients[i] == userName){
userName = userName+"_1"
}
}
clients[socket.id] = userName;
io.emit('user-connected',userName);
io.emit('getAllClients',{clients});
});
socket.on('disconnect', ()=>{
client_name = clients[socket.id]
delete clients[socket.id]
io.emit('disconnect',client_name)
io.emit('getAllClients',{clients});
});
socket.on('chat message', (msg) => {
io.emit('chat message', msg);
});
});
// Define Port
http.listen(3000, () => {
console.log('listening on *:3000');
});