-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (102 loc) · 3.35 KB
/
index.js
File metadata and controls
127 lines (102 loc) · 3.35 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
const http = require("http");
const express = require("express");
require("dotenv").config();
const cors = require("cors");
const socketio = require("socket.io");
const { connectDB } = require("./utils/db.connection");
// Create an Express application
const app = express();
app.use(express.json());
// Create both HTTP and HTTPS servers
const httpServer = http.createServer(app);
// Define allowed origins
const allowedOrigins = [
"https://chatter-ji.vercel.app",
"http://localhost:5173",
];
// CORS options
const corsOptions = {
origin: allowedOrigins,
methods: ["GET", "POST"],
credentials: true,
optionsSuccessStatus: 204,
};
app.use(cors(corsOptions));
app.options("", cors(corsOptions));
//connect db
connectDB();
// Initialize Socket.IO to work with both servers
const io = socketio(httpServer, {
cors: corsOptions,
});
// Start both servers
const HTTP_PORT = 8000;
httpServer.listen(HTTP_PORT, () => {
console.log(`HTTP server listening on port ${HTTP_PORT}`);
});
const emailToSocketMap = new Map();
const socketToEmailMap = new Map();
// Socket.IO connection logic (shared between HTTP and HTTPS)
const handleSocketConnection = (socket) => {
console.log("A client connected with id:", socket.id);
socket.on("join-room", (data) => {
const { emailId } = data;
const roomId = 12;
console.log(">>>>>>>>>>>user join in : ", roomId, emailId);
emailToSocketMap.set(emailId, socket.id);
socketToEmailMap.set(socket.id, emailId);
socket.join(roomId);
socket.emit("joined-room", { roomId, emailId });
socket.broadcast.to(roomId).emit("user-joined", { roomId, emailId });
});
socket.on("call-user", (data) => {
const { offer, emailId, to } = data;
//emailId --> current user
//to --> user to call
const from = socketToEmailMap.get(socket.id); // get current user email
const socketId = emailToSocketMap.get(to); //remote user socket id
if (socketId) {
io.to(socketId).emit("incoming-call", { from, to, offer });
}
});
socket.on("call-declined", ({ from }) => {
const socketId = emailToSocketMap.get(from);
console.log(">>>>>>>>>>>call declined", socketId, from);
io.to(socketId || from).emit("call-declined", { from });
});
socket.on("call-accepted", ({ emailId, ans }) => {
const socketId = emailToSocketMap.get(emailId);
if (socketId) io.to(socketId).emit("call-accepted", { ans });
});
// Handle ICE candidate exchange
socket.on("ice-candidate", ({ candidate, to }) => {
const socketId = emailToSocketMap.get(to);
if (socketId) {
io.to(socketId).emit("ice-candidate", { candidate });
}
});
socket.on('negotiation-needed', ({ offer, to }) => {
const from = socketToEmailMap.get(socket.id);
const socketId = emailToSocketMap.get(to);
if (socketId) {
io.to(socketId).emit('negotiation-needed', { offer, from });
}
});
socket.on('negotiation-done', ({ answer, to }) => {
const socketId = emailToSocketMap.get(to);
if (socketId) {
io.to(socketId).emit('negotiation-done', {from:socket.id, answer });
}
});
socket.on("disconnect", () => {
console.log(`Client disconnected: ${socket.id}`);
});
};
io.on("connection", handleSocketConnection);
// Define the `/` route
app.get("/", (req, res) => {
res.send("Hello");
});
//router
const user = require("./router/user.router");
app.use("/api/v1/user", user);