-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
127 lines (114 loc) · 3.37 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
const { host, PORT, SOCKET_PORT, APP_ENV, SSL } = require("./src/config");
console.clear();
const { UUID, Last } = require("./src/functions");
const express = require("express");
const app = express();
const path = require("path");
app
.use(express.static(path.join(__dirname, "public")))
.set("views", path.join(__dirname, "views"))
.set("view engine", "ejs")
.get("/", (req, res) => res.render("pages/index"));
let protocol = "http";
let options = {};
if (APP_ENV === "production") {
protocol = "https";
options = {
key: fs.readFileSync(SSL.KEY),
cert: fs.readFileSync(SSL.CERT),
};
}
const server = require("http").createServer(options, app);
const io = require("socket.io")(server);
const channels = [];
io.on("connection", (socket) => {
socket.on("createChannel", ({ username }, callback) => {
let channel = {
id: UUID(),
creator: username,
creatorID: socket.id,
messages: [],
};
channels.push(channel);
socket.join(channel.id);
if (callback) {
callback(channel);
process.nextTick(() => (channel = null));
}
});
socket.on("joinChannel", ({ username, channelID }, callback) => {
let channel = channels.find((ch) => ch.id === channelID);
if (!channel) {
return callback("Provided channel cannot find!", false);
}
if (channel && channel.visitor) {
return callback("Channel is full! :(", false);
}
channel.visitor = username;
channel.visitorID = socket.id;
socket.join(channel.id);
io.in(channel.creatorID).emit("userJoined", username);
if (callback) {
callback(channel);
process.nextTick(() => (channel = null));
}
});
socket.on("newMessage", ({ id, message }) => {
if (!id) {
return;
}
if (!message || message === "") {
return;
}
let channel = channels.find((ch) => ch.id === id);
if (channel) {
channel.messages.push({
userID: socket.id,
message,
});
io.in(channel.id).emit("broadcastMessage", Last(channel.messages));
process.nextTick(() => (channel = null));
}
});
socket.on("leaveChannel", (channelID) => {
let channel = channels.find((ch) => ch.id === channelID);
if (channel) {
if (channel.creatorID === socket.id) {
channel.creator = null;
channel.creatorID = null;
}
if (channel.visitorID === socket.id) {
channel.visitor = null;
channel.visitorID = null;
}
io.in(channel.id).emit("userLeft");
if (channel.visitor === null && channel.creator === null) {
channels.splice(channels.indexOf(channel), 1);
process.nextTick(() => (channel = null));
}
}
});
socket.on("disconnect", () => {
let channel = channels.find((ch) =>
[ch.creatorID, ch.visitorID].includes(socket.id)
);
if (channel) {
if (channel.creatorID === socket.id) {
channel.creator = null;
channel.creatorID = null;
}
if (channel.visitorID === socket.id) {
channel.visitor = null;
channel.visitorID = null;
}
io.in(channel.id).emit("userLeft");
if (channel.visitor === null && channel.creator === null) {
channels.splice(channels.indexOf(channel), 1);
process.nextTick(() => (channel = null));
}
}
});
});
server.listen(PORT, () => {
console.log(`Server listening on ${protocol}://${host}:${PORT}`);
});