Initial Commit

This commit is contained in:
2025-09-04 23:09:08 -04:00
commit 9149d0ac20
9 changed files with 2245 additions and 0 deletions

20
src/index.ts Normal file
View File

@@ -0,0 +1,20 @@
import {Server} from './server';
const server = new Server();
process.on('SIGINT', () => {
console.log('Received [SIGINT]. Shutting down...');
server.dispose();
});
process.on('SIGTERM', () => {
console.log('Received [SIGTERM]. Shutting down...');
server.dispose();
});
process.on('SIGBREAK', () => {
console.log('Received [SIGBREAK]. Shutting down...');
server.dispose();
});
server.listen(port => {
console.log(`Server is listening on [http://localhost:${port}]`);
});

84
src/server.ts Normal file
View File

@@ -0,0 +1,84 @@
import path from 'path';
import express, { Application } from 'express';
import { Server as SocketIOServer, Socket } from 'socket.io';
import { createServer, Server as HTTPServer } from 'http';
export class Server {
private httpServer: HTTPServer;
private app: Application;
private io: SocketIOServer;
private activeSockets: Set<string> = new Set();
private readonly DEFAULT_PORT = 5555;
constructor() {
this.app = express();
this.httpServer = createServer(this.app);
this.io = new SocketIOServer(this.httpServer);
this.configureApp();
this.handleRoutes();
this.handleSocketConnection();
}
private configureApp(): void {
this.app.use(express.static(path.join(__dirname, "../public")));
}
private handleRoutes(): void {
this.app.get('/', (req, res) => {
res.send(`<h1>Hello World</h1>`);
});
}
private handleSocketConnection(): void {
this.io.on('connection', socket => {
if (!this.activeSockets.has(socket.id)) {
this.activeSockets.add(socket.id);
console.log(this.activeSockets.entries());
socket.emit("update-user-list", {
users: Array.from(this.activeSockets)
});
socket.broadcast.emit("update-user-list", {
users: [socket.id]
});
socket.on("disconnect", () => {
this.activeSockets.delete(socket.id);
socket.broadcast.emit("remove-user", {
socketId: socket.id
});
});
socket.on("call-user", data => {
console.log('[Server] call received [%o]', data);
socket.to(data.to).emit("call-made", {
offer: data.offer,
socket: socket.id
});
});
socket.on("make-answer", data => {
socket.to(data.to).emit("answer-made", {
socket: socket.id,
answer: data.answer
});
});
}
});
}
public listen(callback: (port: number) => void): void {
this.httpServer.listen(this.DEFAULT_PORT, () => {
callback(this.DEFAULT_PORT);
});
}
public dispose(): void {
this.httpServer.close();
this.io.close();
}
}