MCPcopy
hub / github.com/socketio/socket.io / initServer

Function initServer

examples/client-side-load-balancing-example/index.js:5–45  ·  view source on GitHub ↗
(port)

Source from the content-addressed store, hash-verified

3import { Server } from "socket.io";
4
5function initServer(port) {
6 const httpServer = createServer((req, res) => {
7 if (req.method === "GET" && req.url === "/") {
8 const content = readFileSync("./index.html");
9 res.writeHead(200, {
10 "content-type": "text/html",
11 });
12 res.write(content);
13 res.end();
14 } else {
15 res.writeHead(404).end();
16 }
17 });
18
19 const io = new Server(httpServer, {
20 cors: {
21 origin: [
22 "http://localhost:3000",
23 "http://localhost:3001",
24 "http://localhost:3002",
25 ],
26 },
27 // TODO use an adapter to broadcast messages between the Socket.IO servers
28 });
29
30 io.on("connection", (socket) => {
31 console.log(`connect ${socket.id}`);
32
33 socket.conn.on("upgrade", (transport) => {
34 console.log(`transport upgraded to ${transport.name}`);
35 });
36
37 socket.on("disconnect", (reason) => {
38 console.log(`disconnect ${socket.id} due to ${reason}`);
39 });
40 });
41
42 httpServer.listen(port, () => {
43 console.log(`server listening at http://localhost:${port}`);
44 });
45}
46
47initServer(3000);
48initServer(3001);

Callers 1

index.jsFile · 0.70

Calls 6

createServerFunction · 0.85
listenMethod · 0.80
writeHeadMethod · 0.45
writeMethod · 0.45
endMethod · 0.45
onMethod · 0.45

Tested by

no test coverage detected