-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (73 loc) · 2.08 KB
/
index.js
File metadata and controls
77 lines (73 loc) · 2.08 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
const express = require("express"); //import express
const http = require("http");
const path = require("path");
const cluster = require("cluster");
const { cpus } = require("os");
const socket = require("socket.io");
const { isUuid } = require("uuidv4");
require("dotenv").config(); //using .env file
var app = express();
const server = http.createServer(app);
const io = socket(server);
var id = 500;
const numOfCpus = cpus().length;
app.set("view engine", "ejs");
app.use(express.static("public"));
app.get("/", (req, res) => {
res.render("index");
// console.log(process.pid);
});
app.get("/:callid", (req, res) => {
isUuid(req.params.callid)
? res.render("call", { callID: req.params.callid })
: res.status(404).render("error");
});
app.get("*", (req, res) => {
res.status(404).render("error");
});
io.on("connection", (socketio) => {
socketio.on("join-room", (callID, user, id) => {
// console.log(callID, user)
socketio.join(callID);
socketio.emit("message", {
user: null,
id: id,
message: `Hi ${user}, Welcome to ByteCall`,
}); //Welcome message for us
socketio.to(callID).broadcast.emit("connected-user", {
user: user,
id: id,
message: `${user} joined the meeting!`,
});
socketio.on("chatting", (msg) => {
// console.log(msg)
io.to(callID).emit("message", msg);
});
socketio.on("disconnect", () => {
io.to(callID).emit("disconnect-user", {
user: user,
id: id,
message: `${user} just disconnected!`,
});
});
});
});
const PORT = process.env.PORT || 3000; //configure port like (localhost:3000)
// if (cluster.isMaster) {
// for (let cpu = 0; cpu < numOfCpus; cpu++) {
// cluster.fork();
// }
// cluster.on("exit", (worker, code, signal) => {
// cluster.fork();
// });
// cluster.on("disconnect", () => {
// cluster.fork();
// });
// } else {
// server.listen(PORT, () => {
// console.log(`Server started at: http://127.0.0.1:${PORT}`);
// });
// }
server.listen(PORT, () => {
console.log(`Server started at: http://127.0.0.1:${PORT}`);
});