-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
123 lines (104 loc) · 2.9 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var users = [];
var user_id_map = [];
var connections = [];
var name;
app.get('/', function(req, res){
res.sendFile(__dirname + '/main.html');
});
io.sockets.on('connection', function(socket){
connections.push(socket);
console.log("Connected: %s sockets connected", connections.length);
/* When a user disconnects from the chat */
socket.on('disconnect', function(data){
io.sockets.emit('delete user', {per: socket.username});
users.splice(users.indexOf(socket.username), 1);
updateUsernames();
for(var i=0;i<user_id_map.length;i++)
{
if(user_id_map[i][0] == socket.username)
{
user_id_map.splice(i,1);
break;
}
}
connections.splice(connections.indexOf(socket), 1);
console.log("Disconnected: %s sockets connected", connections.length);
});
/* Sending a message */
socket.on('send message', function(data){
console.log(data);
//this array contains all words in message starting with @
var words_starting_with_at = [];
var valid_names_with_at = [];
var ids_of_at_names = [];
var words = data.split(" ");
for(var i in words)
{
if(words[i].charAt(0) == '@')
{
var string_with_at = words[i].substr(1);
words_starting_with_at.push(string_with_at);
}
}
console.log("at: " + user_id_map);
for(var i in words_starting_with_at)
{
user_name = words_starting_with_at[i];
for(var j in user_id_map)
{
if(user_id_map[j][0] == user_name)
{
console.log("hwww");
valid_names_with_at.push(user_name);
ids_of_at_names.push(user_id_map[j][1]);
break;
}
}
}
if(ids_of_at_names.length > 0)
{
console.log("Sending message to: " + valid_names_with_at);
for(var i in ids_of_at_names)
{
var user_id = ids_of_at_names[i];
io.sockets.in(user_id).emit('new message', {msg:data, user: socket.username});
}
}
else
{
console.log("Broadcast message to all, no specific usernames with @")
console.log('username: ' + socket.username);
io.sockets.emit('new message', {msg:data, user: socket.username});
}
//var clients = io.sockets.clients();
//console.log(clients);
//io.sockets.in(socket.id).emit('new message', {msg:data, user: socket.username});
});
/* New User */
socket.on('new user', function(data, callback)
{
callback(true);
console.log(data);
// console.log(data +" : has joined the chat ");
socket.username = data;
users.push(data);
user_id_map.push([data,socket.id]);
//console.log("update: " + user_id);
updateUsernames();
joinUser(data);
// name = data;
// console.log('a');
});
function updateUsernames(){
io.sockets.emit('get_users', users);
}
function joinUser(data){
io.sockets.emit('new person', {name: data});
}
});
server.listen(3000);
console.log('Server Running on localhost:3000');