-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
243 lines (213 loc) · 7.13 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//Server.js
const WebSocket = require('ws');
const express = require('express');
const path = require('path');
const crypto = require('crypto');
const app = express();
const port = process.env.PORT || 3000;
// Session management
const sessions = new Map();
// Generate a random session ID
function generateSessionId() {
return crypto.randomBytes(4).toString('hex');
}
// Session class to manage connections
class Session {
constructor(id) {
this.id = id;
this.games = new Set();
this.controllers = new Set();
this.createdAt = Date.now();
console.log(`Session created: ${id}`);
}
addClient(ws, type) {
if (type === 'game') {
this.games.add(ws);
console.log(`Game added to session ${this.id}`);
} else if (type === 'controller') {
this.controllers.add(ws);
console.log(`Controller added to session ${this.id}`);
}
this.broadcastStatus();
}
removeClient(ws) {
this.games.delete(ws);
this.controllers.delete(ws);
console.log(`Client removed from session ${this.id}`);
this.broadcastStatus();
}
broadcast(data, sender) {
try {
const messageString = typeof data === 'string' ? data : JSON.stringify(data);
// If sender is a controller, send to all games in this session
if (this.controllers.has(sender)) {
this.games.forEach(game => {
if (game.readyState === WebSocket.OPEN) {
try {
game.send(messageString);
} catch (e) {
console.error(`Error sending to game in session ${this.id}:`, e);
}
}
});
}
} catch (e) {
console.error(`Broadcast error in session ${this.id}:`, e);
}
}
broadcastStatus() {
const status = {
type: 'session_status',
sessionId: this.id,
games: this.games.size,
controllers: this.controllers.size,
timestamp: Date.now()
};
const message = JSON.stringify(status);
[...this.games, ...this.controllers].forEach(client => {
if (client.readyState === WebSocket.OPEN) {
try {
client.send(message);
} catch (e) {
console.error(`Error sending status in session ${this.id}:`, e);
}
}
});
}
isEmpty() {
return this.games.size === 0 && this.controllers.size === 0;
}
}
// Middleware
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`);
next();
});
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
// Routes
app.post('/api/create-session', (req, res) => {
const sessionId = generateSessionId();
sessions.set(sessionId, new Session(sessionId));
console.log(`New session created: ${sessionId}`);
res.json({ sessionId });
});
app.get('/api/session/:sessionId', (req, res) => {
const session = sessions.get(req.params.sessionId);
if (session) {
res.json({
exists: true,
games: session.games.size,
controllers: session.controllers.size
});
} else {
res.json({ exists: false });
}
});
// Create HTTP server
const server = app.listen(port, '0.0.0.0', () => {
console.log(`Server running on port ${port}`);
console.log(`Server time: ${new Date().toISOString()}`);
});
// WebSocket server
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws, req) => {
const clientIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log(`New connection from ${clientIp}`);
let session = null;
let clientType = null;
ws.on('message', (message) => {
try {
// Convert buffer or blob to string
const messageString = message.toString();
let data;
try {
data = JSON.parse(messageString);
} catch (e) {
console.error('Invalid JSON:', messageString);
return;
}
if (data.type === 'register') {
// Handle registration with session
session = sessions.get(data.sessionId);
clientType = data.client;
if (session) {
session.addClient(ws, clientType);
ws.send(JSON.stringify({
type: 'registered',
sessionId: session.id,
client: clientType,
timestamp: Date.now()
}));
console.log(`Client registered as ${clientType} in session ${session.id}`);
} else {
ws.send(JSON.stringify({
type: 'error',
message: 'Invalid session',
timestamp: Date.now()
}));
console.error(`Invalid session ID: ${data.sessionId}`);
}
} else if (session) {
// Add timestamp if not present
if (!data.timestamp) {
data.timestamp = Date.now();
}
// Handle regular messages within session
session.broadcast(data, ws);
}
} catch (e) {
console.error('Message handling error:', e);
}
});
ws.on('close', () => {
if (session) {
session.removeClient(ws);
console.log(`Client disconnected from session ${session.id}`);
if (session.isEmpty()) {
sessions.delete(session.id);
console.log(`Empty session removed: ${session.id}`);
}
}
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
if (session) {
session.removeClient(ws);
}
});
});
// Clean up old sessions periodically
setInterval(() => {
const now = Date.now();
for (const [id, session] of sessions.entries()) {
if (session.isEmpty() && now - session.createdAt > 3600000) { // 1 hour
sessions.delete(id);
console.log(`Cleaned up inactive session ${id}`);
}
}
}, 300000); // Every 5 minutes
// Handle server shutdown
process.on('SIGTERM', () => {
console.log('SIGTERM received. Shutting down...');
wss.close(() => {
console.log('WebSocket server closed');
server.close(() => {
console.log('HTTP server closed');
process.exit(0);
});
});
});
// Error handling
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
// Attempt graceful shutdown
wss.close(() => {
server.close(() => {
process.exit(1);
});
});
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});