-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
91 lines (77 loc) · 2.66 KB
/
server.js
File metadata and controls
91 lines (77 loc) · 2.66 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
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
var express = require('express'),
http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server, {log: false});
app.use("/js", express.static(__dirname + '/js'));
server.listen(8080);
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
// usernames which are currently connected to the chat
var usernames = {};
var chatrooms = {};
io.sockets.on('connection', function(socket) {
socket.on('sendchat', function(data) {
// we tell the client to execute 'updatechat with 2 parameters'
io.sockets.in(socket.chatroom).emit('updatechat', socket.username, data);
});
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(info) {
username = info.username;
chatroom = info.chatroom;
if (!(usernames[username] == undefined || usernames[username] == null))
{
socket.emit('errorConnect', "The username '" + username + "' is already in use.");
return;
}
// we store the username in the socket session for this client
socket.username = username;
// we store the room
socket.chatroom = chatroom;
if (chatrooms[chatroom] == undefined)
{
chatrooms[chatroom] = {
name: chatroom,
usernames: {},
usercount: 0
}
}
// add the client's username to the global list
usernames[username] = username;
chatrooms[chatroom].usernames[username] = username;
chatrooms[chatroom].usercount++;
socket.join(chatroom);
// echo to the client the chatroom name
socket.emit('successfullyjoinchat', chatroom);
// echo to the client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected');
// echo globablly (all clients) that a person has connected
socket.broadcast.to(chatroom).emit('updatechat', 'SERVER', username + ' has connected');
// update the list of users in chat, client-side
io.sockets.in(chatroom).emit('updateusers', chatrooms[chatroom].usernames);
});
// when the user disconnects.. perform this
socket.on('disconnect', function() {
// remove the username from global usernames list
try
{
delete usernames[socket.username];
delete chatrooms[socket.chatroom].usernames[username];
chatrooms[socket.chatroom].usercount--;
// echo globally that this client has left
if (chatrooms[socket.chatroom].usercount > 0)
{
io.sockets.in(socket.chatroom).emit('updatechat', 'SERVER', socket.username + ' has disconnected');
// update list of users in chat, client-side
io.sockets.in(socket.chatroom).emit('updateusers', usernames);
}
socket.leave(socket.chatroom);
}
catch(e)
{
console.log(socket.username, socket.chatroom, chatrooms);
}
});
});