mirror of
https://git.hmsn.ink/call/signal-server.git
synced 2026-03-19 15:54:59 +09:00
first
This commit is contained in:
23
signal-server.mjs
Normal file
23
signal-server.mjs
Normal file
@@ -0,0 +1,23 @@
|
||||
import express from 'express';
|
||||
import { WebSocketServer } from 'ws';
|
||||
|
||||
const app = express();
|
||||
const server = app.listen(3001, () => console.log('Signal server on port 3001'));
|
||||
const wss = new WebSocketServer({ server });
|
||||
|
||||
const peers = new Map();
|
||||
|
||||
wss.on('connection', (ws) => {
|
||||
const id = Math.random().toString(36).substring(2, 9);
|
||||
peers.set(id, ws);
|
||||
ws.send(JSON.stringify({ type: 'your-id', id }));
|
||||
|
||||
ws.on('message', (data) => {
|
||||
const msg = JSON.parse(data);
|
||||
if (msg.target && peers.has(msg.target)) {
|
||||
peers.get(msg.target).send(JSON.stringify(msg));
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', () => peers.delete(id));
|
||||
});
|
||||
Reference in New Issue
Block a user