webserv
Version:
a quick, flexible, fully typed development server
31 lines (30 loc) • 1.34 kB
TypeScript
import { UpgradeListenerFactory } from '../interface';
import WebSocket = require('ws');
export interface RealtimeUpgradeProperties {
onInit?: (methods: ConnectionMethods) => void;
onConnect?: (connection: Connection, methods: ConnectionMethods) => void;
onDisconnect?: (connection: Connection, methods: ConnectionMethods) => void;
onError?: (error: Error, connection: Connection | string) => void;
onMessage?: (data: any, connection: Connection, methods: ConnectionMethods) => void;
}
/**
* ConnectionMethods give proxied access to the underlying connection map
*/
export interface ConnectionMethods {
get(socketId: string): Connection | undefined;
getAll(): Iterable<Connection>;
getSize(): number;
}
export declare class Connection {
readonly id: string;
readonly client: WebSocket;
constructor(id: string, client: WebSocket);
}
/**
* This extends the functionality of websocket.upgrade to provide more robust connection tracking of
* WebSocket connections.
*
* Connections are given a unique ID that can retrieve the user's WebSocket client via ConnectionMethods
* or can be used to link internal data (such as user name or other user information) to the connection.
*/
export declare const realtimeUpgrade: UpgradeListenerFactory<RealtimeUpgradeProperties>;