This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chat.js
118 lines (99 loc) · 3.26 KB
/
chat.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
/*****************
* REAL TIME CHAT
* ****************/
module.exports = function(io) {
var gravatar = require("gravatar");
// Initialize a new socket.io application, named 'chat'
var chat = io.on("connection", function(socket) {
// When the client emits the 'load' event, reply with the
// number of people in this chat room
socket.on("load", function(user, data) {
var room = findClientsSocket(io, data);
if (room.length === 0) {
socket.emit("peopleinchat", { number: 0 });
} else {
socket.emit("peopleinchat", {
number: 1,
user: user.firstName,
avatar: room[0].avatar, // change the avatar later
id: data
});
}
});
// When the client emits 'login', save his name and avatar,
// and add them to the room
socket.on("login", function(data) {
var room = findClientsSocket(io, data.id);
// Only two people per room are allowed
// Use the socket object to store data. Each client gets
// their own unique socket object
socket.username = data.user;
socket.room = data.id;
socket.avatar = gravatar.url(data.avatar, { s: "140", r: "x", d: "mm" });
// socket.avatar = new Image();
// socket.avatar.src = "/images/aboutConsultant/JemPortrait.png";
// Tell the person what he should use for an avatar
socket.emit("img", socket.avatar);
// Add the client to the room
socket.join(data.id);
if (room.length == 1) {
var usernames = [],
avatars = [];
usernames.push(room[0].username);
usernames.push(socket.username);
avatars.push(room[0].avatar);
avatars.push(socket.avatar);
// Send the startChat event to all the people in the
// room, along with a list of people that are in it.
chat.in(data.id).emit("startChat", {
boolean: true,
id: data.id,
users: usernames,
avatars: avatars
});
}
});
// Somebody left the chat
socket.on("disconnect", function() {
// Notify the other person in the chat room
// that his partner has left
socket.broadcast.to(this.room).emit("leave", {
boolean: true,
room: this.room,
user: this.username,
avatar: this.avatar
});
// leave the room
socket.leave(socket.room);
});
// Handle the sending of messages
socket.on("msg", function(data) {
// When the server receives a message, it sends it to the other person in the room.
socket.broadcast
.to(socket.room)
.emit("receive", { msg: data.msg, user: data.user, img: data.img });
});
});
function findClientsSocket(io, roomId) {
var res = [],
ns = io.of("/"); // the default namespace is "/"
if (ns) {
for (var id in ns.connected) {
if (roomId) {
var keys = Object.keys(ns.connected[id].rooms);
var values = keys.map(v => {
return ns.connected[id].rooms[v];
});
var index = values.indexOf(roomId);
// add this later
if (index != -1) {
res.push(ns.connected[id]);
}
} else {
res.push(ns.connected[id]);
}
}
}
return res;
}
};