-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
131 lines (100 loc) · 3.91 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
124
125
126
127
128
129
130
131
'use strict'
// Framework Setup
const express = require("express");
const socket = require("socket.io");
const app = express();
const server = app.listen(5000, function() {
console.log("Listening to port 5000.");
});
const io = socket(server);
// Server Connection Folder
app.use(express.static("public"));
// Global variables to hold all usernames and rooms created
var usernames = {
All_Names : [],
"Alpha" : [],
"Bravo" : [],
"Charlie" : []
};
var rooms = ["Alpha", "Bravo", "Charlie"];
io.on("connection", function(socket) {
console.log("User connected to server.");
socket.on("createUser", function(username) {
usernames.All_Names.push(username);
usernames.All_Names.sort();
usernames[username] = username;
socket.username = username;
socket.currentRoom = "Alpha";
usernames[socket.currentRoom].push(socket.username);
usernames[socket.currentRoom].sort();
socket.join("Alpha");
socket.emit("updateChat", "INFO", "You have joined Alpha room");
socket.broadcast
.to("Alpha")
.emit("updateChat", "INFO", username + " has joined Alpha room");
io.sockets.emit("updateClientUsers", usernames[socket.currentRoom]);
socket.emit("updateRooms", rooms, "Alpha");
});
// socket.broadcast.emit sends message to all other clients except newly created connection
// io.sockets.emit sends message to all clients
socket.on("updateRoomUsers", function(option){
if (option == 1){
if (usernames[socket.currentRoom]){
var index = usernames[socket.currentRoom].indexOf(socket.username);
if (index != -1) usernames[socket.currentRoom].splice(index, 1);
usernames[socket.currentRoom].sort();
io.sockets
.to(socket.currentRoom)
.emit("updateClientUsers", usernames[socket.currentRoom]);
}
} else if (option == 2){
if (usernames[socket.currentRoom]) usernames[socket.currentRoom].push(socket.username);
usernames[socket.currentRoom].sort();
io.sockets
.to(socket.currentRoom)
.emit("updateClientUsers", usernames[socket.currentRoom]);
}
});
socket.on("sendMessage", function(data) {
io.sockets
.to(socket.currentRoom)
.emit("updateChat", socket.username, data);
});
socket.on("createRoom", function(room) {
if (room != null) {
rooms.push(room);
io.sockets.emit("updateRooms", rooms, null);
}
});
socket.on("updateRooms", function(room) {
socket.broadcast
.to(socket.currentRoom)
.emit("updateChat", "INFO", socket.username + " left room");
socket.leave(socket.currentRoom);
socket.currentRoom = room;
socket.join(room);
socket.emit("updateChat", "INFO", "You have joined " + room + " room");
socket.broadcast
.to(room)
.emit("updateChat", "INFO", socket.username + " has joined " + room + " room");
});
socket.on("disconnect", function() {
delete usernames[socket.username];
var index = usernames.All_Names.indexOf(socket.username);
if (index != -1) usernames.All_Names.splice(index, 1);
var roomUsers = usernames[socket.currentRoom];
if (roomUsers){
index = roomUsers.indexOf(socket.username);
if (index != -1) roomUsers.splice(index, 1);
usernames[socket.currentRoom] = roomUsers;
}
socket.broadcast
.to(socket.currentRoom)
.emit("updateClientUsers", usernames[socket.currentRoom]);
if (socket.username) socket.broadcast.emit("updateChat", "INFO", socket.username + " has disconnected");
});
});
app.get('/getNames', function(req, res){
let ret = usernames.All_Names;
res.send(ret);
});