-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_server.js
266 lines (220 loc) · 8.34 KB
/
chat_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
var Rooms = {};
var userIdName={};
var colorwheel={
0: "#FF0000",
1: "#0004ff",
2: "#b34a1d",
3: "#07632a",
4:"#460763"
};
const len = 5;
var curcolor=0;
var userIdToColor={};
var nameEnd = 0;
var roomKey=0;
// Require the packages we will use:
var http = require('http'),
url = require('url'),
path = require('path'),
mime = require('mime'),
fs = require('fs');
// Make a simple fileserver for all of our static content.
// Everything underneath <STATIC DIRECTORY NAME> will be served.
var server = http.createServer(function(req, resp){
var filename = path.join(__dirname, "chatFiles", url.parse(req.url).pathname);
(fs.exists || path.exists)(filename, function(exists){
if (exists) {
fs.readFile(filename, function(err, data){
if (err) {
// File exists but is not readable (permissions issue?)
resp.writeHead(500, {
"Content-Type": "text/plain"
});
resp.write("Internal server error: could not read file");
resp.end();
return;
}
// File exists and is readable
var mimetype = mime.getType(filename);
resp.writeHead(200, {
"Content-Type": mimetype
});
resp.write(data);
resp.end();
return;
});
}else{
// File does not exist
resp.writeHead(404, {
"Content-Type": "text/plain"
});
resp.write("Requested file not found: "+filename);
resp.end();
return;
}
});
});
server.listen(3456);
// Import Socket.IO and pass our HTTP server object to it.
const socketio = require("socket.io")(http, {
wsEngine: 'ws'
});
// Attach our Socket.IO server to our HTTP server to listen
const io = socketio.listen(server);
io.sockets.on("connection", function (socket) {
// This callback runs when a new Socket.IO connection is established.
//init user
var id = socket.id;
var name = "";
if(id in userIdName){
name = userIdName[id];
}else{
name = "user"+nameEnd;
nameEnd+=1;
userIdName[id]=name;
userIdToColor[id]= colorwheel[curcolor%len];
curcolor++;
}
io.to(id).emit("sendUserBack", { "username": name, "id": id });
for(room in Rooms){
roomName = Rooms[room]["roomName"];
io.to(id).emit("message_to_client", { "message": roomName }) // broadcast the message to other users
}
//room maker
socket.on('message_to_server', function (data) {
// This callback runs when the server receives a new message from the client.
roomName = roomKey+". "+data["message"]
Rooms[roomKey]={"creator": socket.id,
"roomName": roomName,"password":data["password"], "bannedUsers":[], "currentUsers": []};
roomKey+=1;
io.sockets.emit("message_to_client", { "message": roomName, "user": userIdName[socket.id] }) // broadcast the message to other users
});
//username change
socket.on('userToServer', function (data) {
var id = socket.id;
var newUse = data["newUse"];
userIdName[id] = newUse;
io.to(id).emit("newUseClient", { "newUse": data["newUse"] });
});
//enter room, check if banned or need pass
socket.on('enterRoomToServer', function (data) {
k=data["roomKeyIn"];
var ban = false;
for(i in Rooms[k]["bannedUsers"]){
if(Rooms[k]["bannedUsers"][i]===socket.id){
ban=true;
}
}
if(ban){
io.to(socket.id).emit("banRemind", {"roomName": Rooms[k]["roomName"] });
}else{
if(Rooms[k]["password"]!=null){
io.to(socket.id).emit("askForPass", { "roomKeyIn": k });
}else{
socket.join(data["roomKeyIn"]);
var key =data["roomKeyIn"];
var cre =Rooms[key]["creator"];
if(cre===socket.id){
owner=true;
}else{
owner=false;
}
Rooms[key]["currentUsers"].push(socket.id);
var n = [];
for(i in Rooms[key]["currentUsers"]){
n.push(userIdName[Rooms[key]["currentUsers"][i]]);
}
io.to(socket.id).emit("RoomEnterClient", { "owner": owner, "roomName": Rooms[key]["roomName"], "color": userIdToColor[socket.id] });
io.to(data["roomKeyIn"]).emit("userInRoomClient", { "user": n }) // broadcast the message to other users
}
}
});
//leave the room remove from user list
socket.on('leaveRoomServer', function (data) {
k=data["roomKeyIn"];
//const index = Rooms[k]["currentUsers"].findIndex(userIdName[socket.id]);
var n = [];
for(i in Rooms[data["roomKeyIn"]]["currentUsers"]){
if(Rooms[data["roomKeyIn"]]["currentUsers"][i]===socket.id){
Rooms[data["roomKeyIn"]]["currentUsers"].splice(i,1);
}else{
n.push(userIdName[Rooms[data["roomKeyIn"]]["currentUsers"][i]]);
}
}
socket.leave(data["roomKeyIn"]);
io.to(data["roomKeyIn"]).emit("userInRoomClient", { "user": n }) // broadcast the message to other users
});
//volley message
socket.on('sendMsgServer', function (data) {
// This callback runs when the server receives a new message from the client.
io.to(data["roomKeyIn"]).emit("sendMessageClient", { "message": data["message"], "user": userIdName[socket.id], "color":userIdToColor[socket.id] }) // broadcast the message to other users
});
//send private message and check to see they are in the room
socket.on('sendPMServer', function (data) {
var k=data["roomKeyIn"];
id=Object.keys(userIdName).find(key => userIdName[key] === data["PMUser"]);
var inRoom=false;
for(i in Rooms[k]["currentUsers"]){
if(Rooms[k]["currentUsers"][i]===id){
inRoom=true;
}
}
if(inRoom){
io.to(socket.id).emit("sendPMClient", { "message": data["message"], "user": userIdName[socket.id] }) // broadcast the message to other users
io.to(id).emit("sendPMClient", { "message": data["message"], "user": userIdName[socket.id] }) // broadcast the message to other users
}
});
//get rid of user on disconnect
socket.on("disconnect", (reason) => {
var n = [];
for(room in Rooms){
for(i in Rooms[room]["currentUsers"]){
if(Rooms[room]["currentUsers"][i]===socket.id){
Rooms[room]["currentUsers"].splice(i,1);
}
}
}
});
//enter room with a password
socket.on('enterRoomToServerPassword', function (data) {
k=data["roomKeyIn"];
if(Rooms[k]["password"]!=data["passGuess"]){
io.to(socket.id).emit("wrongPass", { "roomKeyIn": k });
}else{
socket.join(data["roomKeyIn"]);
var key =data["roomKeyIn"];
var cre =Rooms[key]["creator"];
if(cre===socket.id){
owner=true;
}else{
owner=false;
}
Rooms[key]["currentUsers"].push(socket.id);
var n = [];
for(i in Rooms[key]["currentUsers"]){
n.push(userIdName[Rooms[key]["currentUsers"][i]]);
}
io.to(socket.id).emit("RoomEnterClient", { "owner": owner, "roomName": Rooms[key]["roomName"] });
io.to(data["roomKeyIn"]).emit("userInRoomClient", { "user": n }); // broadcast the message to other users
}
});
//kick user
socket.on('kickUserServer', function (data) {
k=data["roomKeyIn"];
id=Object.keys(userIdName).find(key => userIdName[key] === data["userToKick"]);
io.to(id).emit("beingKickedClient", { "roomKeyIn": k }); // broadcast the message to other users
});
//put user on ban list
socket.on('banUserServer', function (data) {
k=data["roomKeyIn"];
id=Object.keys(userIdName).find(key => userIdName[key] === data["userToKick"]);
Rooms[k]["bannedUsers"].push(id);
io.to(id).emit("beingBannedClient", { "roomKeyIn": k }); // broadcast the message to other users
});
//typing indicaror
socket.on('typingIndc', function (data) {
k=data["roomKeyIn"];
var message = " is typing....."
io.to(data["roomKeyIn"]).emit("typingIncClient", { "message": message, "user": userIdName[socket.id] }) // broadcast the message to other users
});
});