-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.js
96 lines (80 loc) · 2.83 KB
/
Main.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
// index.mjs
import express from "express";
import http from "http";
import { Server as SocketIOServer } from "socket.io";
import Db from "./Database.js";
import morgan from "morgan"
import { STATUS, EVENTS } from "./const.js";
const app = express();
const server = http.createServer(app);
app.use(morgan("dev"))
app.get("/", (req, res) => {
res.status(200).json({ Name: "hello World" });
});
const io = new SocketIOServer(server, {
cors: {
origin: ["http://localhost:3000", "https://experimental-chat-app.netlify.app", "https://chat.engine-app.com"],
},
});
const room = "room";
io.on("connect", (socket) => {
socket.on("user-landed", (id) => {
console.log("User has Landed", socket.id, id, new Date().toString());
});
socket.on("peer-user", (peerid) => {
Db.setItem(socket.id, { peerid, status: STATUS.IDLE });
socket.emit("active-users", Db.getAll(), new Date().toString());
socket.broadcast.emit("user-join", { [socket.id]: { peerid, status: STATUS.IDLE } });
console.log("DATABASE USERS", Db.getAll(), new Date().toString());
});
socket.on(EVENTS.CALLING_USER, (callObject) => {
io.to(callObject.socketid).emit(EVENTS.INCOMING_CALL, callObject);
});
socket.on(EVENTS.CALL_ANSWERED, (callObject) => {
// console.log("Call Object is ", callObject);
// callObject.socketid // who is receiving the call
// callObject.caller // who is calling
try {
const { caller = "", socketid = "" } = callObject ?? {};
if (!caller || !socketid) return;
const callerObject = Db.updateStatus(caller, socketid, true, "in call");
io.emit(EVENTS.UPDATE_USER_STATUS, callerObject);
} catch(err) {
console.log("error", err)
}
});
socket.on(EVENTS.CALL_DISCONNECT, (object) => {
try {
const { callId, socketId } = object;
const notifyuser = Db.findToBeNotifiedUser(socketId, callId);
if (!callId || !socketId) return;
const { socketIds = [] } = Db.getByCallId(callId);
if (!socketIds.length) return;
socketIds.push(false);
socketIds.push("idle");
const callerObject = Db.updateStatus(...socketIds);
io.emit(EVENTS.UPDATE_USER_STATUS, callerObject);
Db.resetCallDatabase(callId);
io.to(notifyuser).emit(EVENTS.USER_LEFT);
} catch (err) {
console.log("Error Happended", err);
}
});
socket.on(EVENTS.CALL_REJECTED, (socketId) => {
io.to(socketId).emit(EVENTS.CALL_REJECTED);
});
socket.on("disconnect", () => {
try {
Db.removeBySocketId(socket.id);
console.log("Current Users", Db.getAll(), new Date().toString());
socket.broadcast.emit("user-exit", socket.id);
} catch(err) {
console.log("Error Happended", err);
}
});
});
// Start the server
const port = process.env.PORT || 5000;
server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});