-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
238 lines (211 loc) · 6.77 KB
/
server.js
File metadata and controls
238 lines (211 loc) · 6.77 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
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
//requirements
const app = require("express")();
const http = require("http").createServer(app);
const io = require("socket.io")(http);
const cors = require("cors");
//api variables
let users = [];
let online = [];
let messages = [];
//enable cors
app.use(cors());
//api access
app.get("/api/messages/", (req, res) => {
res.send(messages);
});
app.get("/api/online/", (req, res) => {
res.send(online);
});
//get date for time stamp
function getDate() {
const date = new Date(Date.now());
// Hours part from the timestamp
let hours = date.getHours();
let ampm = "am";
//conver to 12 hour time
if (hours > 12) {
hours = hours - 12;
ampm = "pm";
}
// Minutes part from the timestamp
let minutes = "0" + date.getMinutes();
// Will display time in 10:30:23 format
let formattedTime = hours + ":" + minutes.substr(-2) + ampm;
return formattedTime;
}
//when user connects
io.on("connection", socket => {
//get adress
const address = socket.handshake.address;
let go = -1;
//find adress in users
while (go === -1) {
go = users
.map(e => {
return e.id;
})
.indexOf(address);
//if can find user make one
if (go === -1) {
createUser(address);
}
}
if (go !== -1) {
//set cur to the user that interacted
const cur = users[go];
//figure out if user is already connected
const idConnected = online
.map(user => {
return user.id;
})
.indexOf(cur.id);
//if this is user's first open tab
if (idConnected === -1) {
//log in server console
console.log("User connected: ", cur.name, " @ ", address);
//push message to messages
messages.push({
time: getDate(),
type: "connection",
content: cur.name + " connected",
reactions: []
});
//prompt front end to load messages
io.emit("new message");
//add user to the list of online user
online.push(cur);
cur.tabs += 1;
} else {
//prompt front end to load messages
io.emit("new message");
//set users tabs to tabs +1
cur.tabs += 1;
}
//when user is typing
socket.on("typing", () => {
//log in server console
console.log(cur.name, " is typing...");
//push a message
messages.push({
time: getDate(),
type: "connection",
content: cur.name + " is typing...",
reactions: []
});
//emit something telling the front end that a new message was sent and that it should fetch messages again
io.emit("new message");
});
//when user disconnects
socket.on("disconnect", () => {
//if this is user's only or last tab and disconnecting will mean they are not on the app on any tab
if (cur.tabs === 1) {
//log in server console
console.log("User disconnected: ", cur.name, " @ ", address);
//push a message
messages.push({
time: getDate(),
type: "connection",
content: cur.name + " disconnected",
reactions: []
});
//remove the user from online
online.pop(cur);
//emit something telling the front end that a new message was sent and that it should fetch messages again
io.emit("new message");
}
});
//when a message is sent
socket.on("chat message", msg => {
//only send if message is not blank (spam prevention)
if (msg.length > 0 && msg.length < 300) {
//log in server console
console.log(cur.name + ": " + msg);
//find index of last typing message from this user (message was sent, user is therefore not typing anymore);
const lastTyping = messages
.map(message => {
return message.content;
})
.indexOf(cur.name + " is typing...");
//remove last instance of (whoever) is typing from the chat log
messages.splice(lastTyping, 1);
//push message
messages.push({
time: getDate(),
type: "message",
user: cur.name,
content: msg,
reactions: [],
reactors: []
});
//prompt front end to fetch new messages
io.emit("new message");
}
});
//when someone reacts to a message
socket.on("reaction", (msg, moji) => {
//if is not undefined
if (messages[msg] !== undefined) {
//if person has not already reacted
if (messages[msg].reactors.indexOf(cur.id) === -1) {
//find moji in msg
const prereacted = messages[msg].reactions
.map(emojis => {
return emojis.emoji;
})
.indexOf(moji);
//if no one already reacted with this emoji
if (prereacted === -1) {
//set message index reactions to that emoji
messages[msg].reactions.push({ emoji: moji, amt: 1 });
}
//if was found
else {
//set message index reactions to that emoji
messages[msg].reactions[prereacted].amt += 1;
}
//add reactor to reactors array
messages[msg].reactors.push(cur.id);
//prompt front end to fetch new messages
io.emit("new message");
}
}
});
//when someone sets their username
socket.on("set user", user => {
//only set if name is not same as old and is not blank (spam prevention)
if (user.length > 0 && user !== cur.name && user.length < 20) {
//variable for previous name
let old = cur.name;
//set user's name to recieved name
cur.name = user;
//log in console so users know who changed their name to what
console.log(old + " changed their name to " + cur.name);
//push to messages
messages.push({
time: getDate(),
type: "connection",
content: old + " changed their name to " + cur.name,
reactions: []
});
//set name in online to new name
online[online.indexOf(old)] = cur.name;
//prompt frontend that there is a new message
io.emit("new message", console.log("user changed and message sent"));
}
});
}
});
//create a user for a new ip
function createUser(ip) {
let user = { tabs: 0, id: ip, name: "Anonymous" };
//add new user to list of users
users.push(user);
//log that new user was created (exclusively in server console)
console.log("New user created @ ", ip);
}
//set port to 3001
const PORT = process.env.PORT || 3000;
//listen on the port provided
http.listen(PORT, () => {
console.log("Listening on port:", PORT);
});