lightweight-socket
Version:
lightweight socket
91 lines (83 loc) • 2.56 kB
text/typescript
import { WebSocket, WebSocketServer } from 'ws';
declare class Adapter {
private server;
private rooms;
private exceptions;
private namespace;
constructor(server: Server);
to(room: string): this;
except(clientId: string): this;
in(room: string): this;
of(namespace: string): this;
emit(name: string, ...args: any[]): void;
}
declare class Emitter {
protected listeners: {
[event: string]: Function[];
};
on(event: string, listener: Function): void;
once(event: string, listener: Function): void;
off(event: string, listener: Function): void;
}
declare class AbstractClient extends Emitter {
protected url: string;
socket: WebSocket;
id: string;
emit(name: string, ...args: any[]): void;
close(): void;
protected receive(data: string | Buffer): void;
protected init(): void;
}
declare class Client extends AbstractClient {
constructor(url: string);
}
declare class ServerSideClient extends AbstractClient {
private server;
rooms: Map<string, Room | ServerSideClient>;
constructor(url: string, socket: WebSocket, server: Server);
join(room: string): void;
leave(room: string): void;
to(room: string): Adapter;
in(room: string): Adapter;
except(room: string): Adapter;
emit(name: string, ...args: any[]): void;
}
declare class Room {
clients: Map<string, ServerSideClient>;
id: string;
constructor(name: string);
add(client: ServerSideClient): void;
remove(client: ServerSideClient): void;
}
declare class Namespace extends Emitter {
namespace: string;
server: Server;
ws: WebSocketServer;
rooms: Map<string, Room | ServerSideClient>;
constructor(namespace: string, server: Server, ws: WebSocketServer);
except(room: string): Adapter;
emit(name: string, ...args: any[]): void;
adapter(): Adapter;
to(room: string): Adapter;
in(room: string): Adapter;
}
interface ServerOptionInterface {
port: number;
}
declare class Server {
private server;
ws: WebSocketServer;
clients: Map<string, ServerSideClient>;
private port;
namespaces: Map<string, Namespace>;
static isInitialized: boolean;
constructor(options: ServerOptionInterface | number);
private upgrade;
private handler;
getNamespace(namespacePath: string): Namespace;
emit(name: string, ...args: any[]): void;
close(): void;
on(event: string, listener: any): void;
of(namespace: string): Namespace;
}
export { Client, Server, ServerSideClient };