Spaces:
Runtime error
Runtime error
File size: 1,140 Bytes
fb38ec5 | 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 39 | import { IncomingMessage } from "http";
import { Duplex } from "stream";
import { WebSocket } from "ws";
import { WebSocketHandler, WebSocketHandlerContext } from "../../../types/websocket.js";
function handlePageIdWebSocket(context: WebSocketHandlerContext, ws: WebSocket) {
const { fastify } = context;
const messageHandler = (payload: { pageId: string }) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(payload));
}
};
fastify.cdpService.on("pageId", messageHandler);
ws.on("error", (err) => {
fastify.log.error({ err }, "PageId WebSocket error");
});
ws.on("close", () => {
fastify.log.info("PageId WebSocket connection closed");
fastify.cdpService.removeListener("pageId", messageHandler);
});
}
export const pageIdHandler: WebSocketHandler = {
path: "/v1/sessions/pageId",
handler: (
request: IncomingMessage,
socket: Duplex,
head: Buffer,
context: WebSocketHandlerContext,
) => {
context.fastify.log.info("Connecting to pageId...");
context.wss.handleUpgrade(request, socket, head, (ws) => handlePageIdWebSocket(context, ws));
},
};
|