Files
parcer/server/src/socket-connection/connection.ts

45 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-06-08 23:12:15 +05:30
import { Namespace, Socket } from 'socket.io';
2024-06-01 00:23:12 +05:30
import logger from "../logger";
2024-06-08 23:11:54 +05:30
import registerInputHandlers from '../browser-management/inputHandlers'
2024-06-01 00:23:12 +05:30
2024-06-01 00:25:02 +05:30
/**
* Opens a websocket canal for duplex data transfer and registers all handlers for this data for the recording session.
* Uses socket.io dynamic namespaces for multiplexing the traffic from different running remote browser instances.
* @param io dynamic namespace on the socket.io server
* @param callback function called after the connection is created providing the socket resource
* @category BrowserManagement
*/
2024-06-01 00:23:12 +05:30
export const createSocketConnection = (
io: Namespace,
callback: (socket: Socket) => void,
2024-06-08 23:12:15 +05:30
) => {
2024-06-01 00:23:12 +05:30
const onConnection = async (socket: Socket) => {
2024-06-08 23:12:15 +05:30
logger.log('info', "Client connected " + socket.id);
2024-06-08 23:11:54 +05:30
registerInputHandlers(socket);
2024-06-01 00:23:12 +05:30
socket.on('disconnect', () => logger.log('info', "Client disconnected " + socket.id));
callback(socket);
}
io.on('connection', onConnection);
};
2024-06-01 00:25:02 +05:30
/**
* Opens a websocket canal for duplex data transfer for the recording run.
* Uses socket.io dynamic namespaces for multiplexing the traffic from different running remote browser instances.
* @param io dynamic namespace on the socket.io server
* @param callback function called after the connection is created providing the socket resource
* @category BrowserManagement
*/
export const createSocketConnectionForRun = (
2024-06-08 23:12:15 +05:30
io: Namespace,
callback: (socket: Socket) => void,
) => {
const onConnection = async (socket: Socket) => {
2024-06-08 23:12:15 +05:30
logger.log('info', "Client connected " + socket.id);
socket.on('disconnect', () => logger.log('info', "Client disconnected " + socket.id));
callback(socket);
}
io.on('connection', onConnection);
};