-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
45 lines (36 loc) · 1.14 KB
/
Copy pathserver.ts
File metadata and controls
45 lines (36 loc) · 1.14 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
import { createServer } from 'http';
import { Server } from 'socket.io';
import { Simulator } from './src/services/simulator';
import { config } from 'dotenv';
import path from 'path';
// Load environment variables from .env.local
config({ path: path.resolve(process.cwd(), '.env.local') });
const httpServer = createServer((req, res) => {
if (req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Simulation Engine Running');
} else {
res.writeHead(404);
res.end('Not Found');
}
});
const io = new Server(httpServer, {
cors: {
origin: '*', // For development, allow all origins
methods: ['GET', 'POST']
}
});
const simulator = new Simulator(io);
io.on('connection', (socket) => {
console.log(`Client connected: ${socket.id}`);
// Automatically join the fleet-update room upon connection
socket.join('fleet-update');
socket.on('disconnect', () => {
console.log(`Client disconnected: ${socket.id}`);
});
});
const PORT = process.env.PORT || 3001;
httpServer.listen(PORT, () => {
console.log(`WebSocket Simulation Server listening on port ${PORT}`);
simulator.start();
});