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

Function init

packages/socket.io/test/connection-state-recovery.ts:7–36  ·  view source on GitHub ↗
(httpServer: HttpServer, io: Server)

Source from the content-addressed store, hash-verified

5import { Adapter } from "socket.io-adapter";
6
7async function init(httpServer: HttpServer, io: Server) {
8 // Engine.IO handshake
9 const sid = await eioHandshake(httpServer);
10
11 // Socket.IO handshake
12 await eioPush(httpServer, sid, "40");
13 const handshakeBody = await eioPoll(httpServer, sid);
14
15 expect(handshakeBody.startsWith("40")).to.be(true);
16
17 const handshake = JSON.parse(handshakeBody.substring(2));
18
19 expect(handshake.sid).to.not.be(undefined);
20 // in that case, the handshake also contains a private session ID
21 expect(handshake.pid).to.not.be(undefined);
22
23 io.emit("hello");
24
25 const message = await eioPoll(httpServer, sid);
26
27 expect(message.startsWith('42["hello"')).to.be(true);
28
29 const offset = JSON.parse(message.substring(2))[1];
30 // in that case, each packet also includes an offset in the data array
31 expect(offset).to.not.be(undefined);
32
33 await eioPush(httpServer, sid, "1");
34
35 return [handshake.sid, handshake.pid, offset];
36}
37
38describe("connection state recovery", () => {
39 it("should restore session and missed packets", async () => {

Callers 1

Calls 4

eioHandshakeFunction · 0.90
eioPushFunction · 0.90
eioPollFunction · 0.90
emitMethod · 0.65

Tested by

no test coverage detected