Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 41 additions & 41 deletions types/ws/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,41 +109,41 @@ declare class WebSocket extends EventEmitter {
removeEventListener(method: "open", cb: (event: WebSocket.Event) => void): void;

// Events
on(event: "close", listener: (this: WebSocket, code: number, reason: Buffer) => void): this;
on(event: "error", listener: (this: WebSocket, err: Error) => void): this;
on(event: "upgrade", listener: (this: WebSocket, request: IncomingMessage) => void): this;
on(event: "message", listener: (this: WebSocket, data: WebSocket.RawData, isBinary: boolean) => void): this;
on(event: "open", listener: (this: WebSocket) => void): this;
on(event: "ping" | "pong", listener: (this: WebSocket, data: Buffer) => void): this;
on(event: "close", listener: (this: this, code: number, reason: Buffer) => void): this;
on(event: "error", listener: (this: this, err: Error) => void): this;
on(event: "upgrade", listener: (this: this, request: IncomingMessage) => void): this;
on(event: "message", listener: (this: this, data: WebSocket.RawData, isBinary: boolean) => void): this;
on(event: "open", listener: (this: this) => void): this;
on(event: "ping" | "pong", listener: (this: this, data: Buffer) => void): this;
on(
event: "unexpected-response",
listener: (this: WebSocket, request: ClientRequest, response: IncomingMessage) => void,
listener: (this: this, request: ClientRequest, response: IncomingMessage) => void,
): this;
on(event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this;

once(event: "close", listener: (this: WebSocket, code: number, reason: Buffer) => void): this;
once(event: "error", listener: (this: WebSocket, err: Error) => void): this;
once(event: "upgrade", listener: (this: WebSocket, request: IncomingMessage) => void): this;
once(event: "message", listener: (this: WebSocket, data: WebSocket.RawData, isBinary: boolean) => void): this;
once(event: "open", listener: (this: WebSocket) => void): this;
once(event: "ping" | "pong", listener: (this: WebSocket, data: Buffer) => void): this;
on(event: string | symbol, listener: (this: this, ...args: any[]) => void): this;

once(event: "close", listener: (this: this, code: number, reason: Buffer) => void): this;
once(event: "error", listener: (this: this, err: Error) => void): this;
once(event: "upgrade", listener: (this: this, request: IncomingMessage) => void): this;
once(event: "message", listener: (this: this, data: WebSocket.RawData, isBinary: boolean) => void): this;
once(event: "open", listener: (this: this) => void): this;
once(event: "ping" | "pong", listener: (this: this, data: Buffer) => void): this;
once(
event: "unexpected-response",
listener: (this: WebSocket, request: ClientRequest, response: IncomingMessage) => void,
listener: (this: this, request: ClientRequest, response: IncomingMessage) => void,
): this;
once(event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this;

off(event: "close", listener: (this: WebSocket, code: number, reason: Buffer) => void): this;
off(event: "error", listener: (this: WebSocket, err: Error) => void): this;
off(event: "upgrade", listener: (this: WebSocket, request: IncomingMessage) => void): this;
off(event: "message", listener: (this: WebSocket, data: WebSocket.RawData, isBinary: boolean) => void): this;
off(event: "open", listener: (this: WebSocket) => void): this;
off(event: "ping" | "pong", listener: (this: WebSocket, data: Buffer) => void): this;
once(event: string | symbol, listener: (this: this, ...args: any[]) => void): this;

off(event: "close", listener: (this: this, code: number, reason: Buffer) => void): this;
off(event: "error", listener: (this: this, err: Error) => void): this;
off(event: "upgrade", listener: (this: this, request: IncomingMessage) => void): this;
off(event: "message", listener: (this: this, data: WebSocket.RawData, isBinary: boolean) => void): this;
off(event: "open", listener: (this: this) => void): this;
off(event: "ping" | "pong", listener: (this: this, data: Buffer) => void): this;
off(
event: "unexpected-response",
listener: (this: WebSocket, request: ClientRequest, response: IncomingMessage) => void,
listener: (this: this, request: ClientRequest, response: IncomingMessage) => void,
): this;
off(event: string | symbol, listener: (this: WebSocket, ...args: any[]) => void): this;
off(event: string | symbol, listener: (this: this, ...args: any[]) => void): this;

addListener(event: "close", listener: (code: number, reason: Buffer) => void): this;
addListener(event: "error", listener: (err: Error) => void): this;
Expand Down Expand Up @@ -315,23 +315,23 @@ declare namespace WebSocket {
shouldHandle(request: IncomingMessage): boolean | Promise<boolean>;

// Events
on(event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this;
on(event: "error", cb: (this: Server, error: Error) => void): this;
on(event: "headers", cb: (this: Server, headers: string[], request: IncomingMessage) => void): this;
on(event: "close" | "listening", cb: (this: Server) => void): this;
on(event: string | symbol, listener: (this: Server, ...args: any[]) => void): this;

once(event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this;
once(event: "error", cb: (this: Server, error: Error) => void): this;
once(event: "headers", cb: (this: Server, headers: string[], request: IncomingMessage) => void): this;
once(event: "close" | "listening", cb: (this: Server) => void): this;
on(event: "connection", cb: (this: this, socket: WebSocket, request: IncomingMessage) => void): this;
on(event: "error", cb: (this: this, error: Error) => void): this;
on(event: "headers", cb: (this: this, headers: string[], request: IncomingMessage) => void): this;
on(event: "close" | "listening", cb: (this: this) => void): this;
on(event: string | symbol, listener: (this: this, ...args: any[]) => void): this;

once(event: "connection", cb: (this: this, socket: WebSocket, request: IncomingMessage) => void): this;
once(event: "error", cb: (this: this, error: Error) => void): this;
once(event: "headers", cb: (this: this, headers: string[], request: IncomingMessage) => void): this;
once(event: "close" | "listening", cb: (this: this) => void): this;
once(event: string | symbol, listener: (...args: any[]) => void): this;

off(event: "connection", cb: (this: Server, socket: WebSocket, request: IncomingMessage) => void): this;
off(event: "error", cb: (this: Server, error: Error) => void): this;
off(event: "headers", cb: (this: Server, headers: string[], request: IncomingMessage) => void): this;
off(event: "close" | "listening", cb: (this: Server) => void): this;
off(event: string | symbol, listener: (this: Server, ...args: any[]) => void): this;
off(event: "connection", cb: (this: this, socket: WebSocket, request: IncomingMessage) => void): this;
off(event: "error", cb: (this: this, error: Error) => void): this;
off(event: "headers", cb: (this: this, headers: string[], request: IncomingMessage) => void): this;
off(event: "close" | "listening", cb: (this: this) => void): this;
off(event: string | symbol, listener: (this: this, ...args: any[]) => void): this;

addListener(event: "connection", cb: (client: WebSocket, request: IncomingMessage) => void): this;
addListener(event: "error", cb: (err: Error) => void): this;
Expand Down
12 changes: 12 additions & 0 deletions types/ws/ws-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,15 @@ function f() {
}
});
}

{
class CustomWebSocket extends WebSocket {
example: boolean;
}
const webSocketServer = new WebSocket.WebSocketServer();
webSocketServer.on('connection', (ws: CustomWebSocket) => {
ws.on('message', function(this: CustomWebSocket) {
console.log(this.example);
});
});
}