-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
58 lines (51 loc) · 1.42 KB
/
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
50
51
52
53
54
55
56
57
58
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
users = {};
server.listen(1111);
console.log('Listning on port 1111....');
app.get('/',function(req,res){
res.sendFile(__dirname + '/index.html');
});
io.sockets.on('connection',function(socket){
socket.on('new user',function(data,callback){
if(data in users){
callback(false);
} else {
callback(true);
socket.nickname = data;
users[socket.nickname] = socket;
updateNicknames();
}
});
function updateNicknames(){
io.sockets.emit('usernames',Object.keys(users));
}
socket.on('send message',function(data,callback){
var msg = data.trim();
if(msg.substr(0,3) === '/w '){
msg = msg.substr(3);
var ind = msg.indexOf(' ');
if(ind !== -1){
var name = msg.substring(0, ind);
var msg = msg.substring(ind + 1);
if(name in users){
users[name].emit('whisper',{msg: msg, nick: socket.nickname});
console.log('wisper!');
}else {
console.log('Error! Enter the valid user');
}
}else{
callback('Error! please enter msg for your whisper');
}
} else {
io.sockets.emit('new message',{msg: msg, nick: socket.nickname});
}
});
socket.on('disconnect',function(data){
if(!socket.nickname) return;
delete users[socket.nickname];
updateNicknames();
});
});