-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
97 lines (89 loc) · 3.12 KB
/
socket.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
let url = require("url"),
{ EventEmitter } = require("events"),
User = function (username, user_profile, color) {
this.username = username;
this.user_profile = user_profile;
this.color = colors[color++];
},
ee = new EventEmitter();
const colors = require("./colors"); // To Assign a new Color to each new User
const namespace = {}; // will store the existing namespaces
namespace["/"] = true; // make sure no room exist on root path
namespace["/chat/"] = true; // make sure no room exist on chat path
namespace["/chat"] = true; // make sure no room exist on chat path
module.exports = (io) => {
io.on("connection", (user) => {
const { ns } = url.parse(user.handshake.url, true).query;
if (!ns) {
user.disconnect();
return { err: "ns not provided" };
}
if (!namespace[ns]) {
namespace[ns] = true;
console.log(ns);
io.of(ns).on("connection", (socket) => {
console.log(ns);
let room = ns.split("/")[2].toUpperCase();
ee.emit("chat", socket, room);
});
}
});
return ee;
};
let nsps = {};
ee.on("chat", (socket, ns) => {
if (!nsps[ns]) {
nsps[ns] = [];
}
// Registers each user after checking if they have a unique nickname or not
// and sends a signal to all clients to register the user on their side
socket.on("hey", (data) => {
let taken = false;
nsps[ns].forEach((user) => {
if (user.username === data.username) {
socket.emit("nickname_error");
console.log(`[${ns} Room] [NICKNAME ERROR] ${data.username} is Taken`);
taken = true;
socket.disconnect();
}
});
if (!taken) {
let newUser = new User(data.username, data.user_profile, nsps[ns].length);
console.log(
`[${ns} Room] [NEW CONNECTION] ${newUser.username} is Now Connected.`
);
nsps[ns].push(newUser);
console.log(`[${ns} Room] [USER COUNT] ${nsps[ns].length} users online`);
socket.emit("user", { userList: nsps[ns], newUser: newUser });
socket.broadcast.emit("joined", newUser);
}
});
// Listens for an incoming message and passes it on to the other users
socket.on("sendMsg", (data) => {
socket.broadcast.emit("recieveMsg", {
user: data.user,
message: data.message,
});
});
socket.on("getUsers", () => {
socket.emit("allUsers", nsps[ns]);
});
// Listens for the 'typing' signal and tell other clients who is typing
socket.on("typing", (data) => {
socket.broadcast.emit("isTyping", data);
});
// Listens for the 'notTyping' signal and tell other clients that the user stopped typing
socket.on("notTyping", (data) => {
socket.broadcast.emit("isNotTyping", data);
});
// Listens for an incoming request to leave the server and takes it off the users list
// and tells other clients tabout the same
socket.on("leave", (data) => {
nsps[ns] = nsps[ns].filter((user) => {
return user.username !== data.username;
});
console.log(`[${ns} Room] [USER LEFT] ${data.username} is now Offline`);
console.log(`[${ns} Room] [USER COUNT] ${nsps[ns].length} users online`);
socket.broadcast.emit("left", data);
});
});