-
Notifications
You must be signed in to change notification settings - Fork 10.1k
/
Copy pathsocket.io.ts
38 lines (33 loc) · 1.13 KB
/
socket.io.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { NitroApp } from "nitropack";
import { Server as Engine } from "engine.io";
import { Server } from "socket.io";
import { defineEventHandler } from "h3";
export default defineNitroPlugin((nitroApp: NitroApp) => {
const engine = new Engine();
const io = new Server();
io.bind(engine);
io.on("connection", (socket) => {
// ...
});
nitroApp.router.use("/socket.io/", defineEventHandler({
handler(event) {
engine.handleRequest(event.node.req, event.node.res);
event._handled = true;
},
websocket: {
open(peer) {
// crossws >= 0.3.0
// @ts-expect-error private method and property
engine.prepare(peer._internal.nodeReq);
// @ts-expect-error private method and property
engine.onWebSocket(peer._internal.nodeReq, peer._internal.nodeReq.socket, peer.websocket);
// crossws < 0.3.0
// const context = peer.ctx.node;
// // @ts-expect-error private method
// engine.prepare(context.req);
// // @ts-expect-error private method
// engine.onWebSocket(context.req, context.req.socket, context.ws);
}
}
}));
});