/
githubmirror
/
socket.io
Обзор
Документация
Войти
/
githubmirror
/
socket.io
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
examples/client-side-load-balancing-example/index.js
49 строк
1 KB
Damien Arrachequesne
docs(examples): add client-side load balancing example
02 июн 2026, 11:50
Не верифицирован
02 июн 2026, 11:50
20df6ae
Код
Авторство
О чём код?
import { readFileSync } from "node:fs"; import { createServer } from "node:http"; import { Server } from "socket.io"; function initServer(port) { const httpServer = createServer((req, res) => { if (req.method === "GET" && req.url === "/") { const content = readFileSync("./index.html"); res.writeHead(200, { "content-type": "text/html", }); res.write(content); res.end(); } else { res.writeHead(404).end(); } }); const io = new Server(httpServer, { cors: { origin: [ "http://localhost:3000", "http://localhost:3001", "http://localhost:3002", ], }, // TODO use an adapter to broadcast messages between the Socket.IO servers }); io.on("connection", (socket) => { console.log(`connect ${socket.id}`); socket.conn.on("upgrade", (transport) => { console.log(`transport upgraded to ${transport.name}`); }); socket.on("disconnect", (reason) => { console.log(`disconnect ${socket.id} due to ${reason}`); }); }); httpServer.listen(port, () => { console.log(`server listening at http://localhost:${port}`); }); } initServer(3000); initServer(3001); initServer(3002);