-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
213 lines (169 loc) Β· 5.48 KB
/
server.js
File metadata and controls
213 lines (169 loc) Β· 5.48 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
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const cors = require('cors');
const { v4: uuidv4 } = require('uuid');
const app = express();
const server = http.createServer(app);
// Configure CORS for both Express and Socket.IO
const corsOptions = {
origin: ["http://localhost:5173", "http://127.0.0.1:5173"],
methods: ["GET", "POST"],
credentials: true
};
app.use(cors(corsOptions));
app.use(express.json());
const io = socketIo(server, {
cors: corsOptions
});
// In-memory storage
const users = new Map();
const activeCalls = new Map();
// API Routes
app.get('/api/users', (req, res) => {
const userList = Array.from(users.values());
res.json(userList);
});
app.post('/api/login', (req, res) => {
const { username, password } = req.body;
// Simple demo authentication - accept any username/password
const user = {
id: uuidv4(),
username,
isOnline: false
};
const token = 'demo-token-' + user.id;
res.json({
user,
token
});
});
// Socket.IO connection handling
io.on('connection', (socket) => {
console.log('π User connected:', socket.id);
socket.on('join', (userData) => {
console.log('π€ User joining:', userData.username);
const user = {
id: userData.id,
username: userData.username,
socketId: socket.id,
isOnline: true
};
users.set(socket.id, user);
// Send success response with current online users
const onlineUsers = Array.from(users.values());
socket.emit('join_success', {
message: `Welcome ${userData.username}!`,
onlineUsers
});
// Broadcast updated user list to all clients
io.emit('users_updated', onlineUsers);
console.log('π Online users:', onlineUsers.length);
});
socket.on('call_user', async (data) => {
const { to, from, callType, offer } = data;
console.log(`π Call request: ${from.username} -> ${to} (${callType})`);
// Find target user
const targetUser = Array.from(users.values()).find(user => user.id === to);
if (!targetUser) {
socket.emit('call_status', { status: 'user_offline' });
return;
}
const callId = uuidv4();
activeCalls.set(callId, {
id: callId,
caller: from,
callee: targetUser,
callType,
status: 'ringing'
});
// Send incoming call to target user
io.to(targetUser.socketId).emit('incoming_call', {
callId,
from,
callType,
offer
});
// Send ringing status to caller
socket.emit('call_status', { status: 'ringing' });
console.log('π Call initiated:', callId);
});
socket.on('accept_call', (data) => {
const { callId, answer } = data;
console.log('β
Call accepted:', callId);
const call = activeCalls.get(callId);
if (!call) {
console.log('β Call not found:', callId);
return;
}
call.status = 'connected';
// Send acceptance to caller
io.to(call.caller.socketId || call.caller.id).emit('call_accepted', {
callId,
answer
});
console.log('π Call connected:', callId);
});
socket.on('reject_call', (data) => {
const { callId } = data;
console.log('β Call rejected:', callId);
const call = activeCalls.get(callId);
if (call) {
// Notify caller
io.to(call.caller.socketId || call.caller.id).emit('call_rejected');
activeCalls.delete(callId);
}
});
socket.on('end_call', (data) => {
const { callId } = data;
console.log('π΄ Call ended:', callId);
const call = activeCalls.get(callId);
if (call) {
// Notify both parties
io.to(call.caller.socketId || call.caller.id).emit('call_ended');
io.to(call.callee.socketId).emit('call_ended');
activeCalls.delete(callId);
}
});
socket.on('ice_candidate', (data) => {
const { to, candidate } = data;
console.log('π§ ICE candidate relay to:', to);
// Find target user and relay ICE candidate
const targetUser = Array.from(users.values()).find(user => user.id === to);
if (targetUser) {
io.to(targetUser.socketId).emit('ice_candidate', {
candidate
});
}
});
socket.on('disconnect', () => {
console.log('β User disconnected:', socket.id);
const user = users.get(socket.id);
if (user) {
console.log('π User left:', user.username);
// End any active calls involving this user
for (const [callId, call] of activeCalls.entries()) {
if (call.caller.socketId === socket.id || call.callee.socketId === socket.id) {
console.log('π΄ Ending call due to disconnect:', callId);
// Notify the other party
const otherSocketId = call.caller.socketId === socket.id
? call.callee.socketId
: call.caller.socketId;
io.to(otherSocketId).emit('call_ended');
activeCalls.delete(callId);
}
}
users.delete(socket.id);
// Broadcast updated user list
const onlineUsers = Array.from(users.values());
io.emit('users_updated', onlineUsers);
console.log('π Remaining online users:', onlineUsers.length);
}
});
});
const PORT = process.env.PORT || 3001;
server.listen(PORT, () => {
console.log(`π Server running on port ${PORT}`);
console.log(`π‘ Socket.IO server ready`);
console.log(`π CORS enabled for: ${corsOptions.origin.join(', ')}`);
});