mirror of
https://github.com/nvms/prsm.git
synced 2025-12-16 16:10:54 +00:00
add removeFromAllRooms
This commit is contained in:
parent
5546e845ac
commit
2214a58113
@ -13,37 +13,89 @@ type LatencyPayload = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export declare interface Connection extends EventTarget {
|
export declare interface Connection extends EventTarget {
|
||||||
addEventListener(type: "message", listener: (ev: CustomEvent) => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: "message",
|
||||||
|
listener: (ev: CustomEvent) => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
|
|
||||||
/** Emits when a connection is made. */
|
/** Emits when a connection is made. */
|
||||||
addEventListener(type: "connection", listener: () => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: "connection",
|
||||||
|
listener: () => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
/** Emits when a connection is made. */
|
/** Emits when a connection is made. */
|
||||||
addEventListener(type: "connected", listener: () => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: "connected",
|
||||||
|
listener: () => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
/** Emits when a connection is made. */
|
/** Emits when a connection is made. */
|
||||||
addEventListener(type: "connect", listener: () => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: "connect",
|
||||||
|
listener: () => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
|
|
||||||
/** Emits when a connection is closed. */
|
/** Emits when a connection is closed. */
|
||||||
addEventListener(type: "close", listener: () => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: "close",
|
||||||
|
listener: () => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
/** Emits when a connection is closed. */
|
/** Emits when a connection is closed. */
|
||||||
addEventListener(type: "closed", listener: () => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: "closed",
|
||||||
|
listener: () => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
/** Emits when a connection is closed. */
|
/** Emits when a connection is closed. */
|
||||||
addEventListener(type: "disconnect", listener: () => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: "disconnect",
|
||||||
|
listener: () => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
/** Emits when a connection is closed. */
|
/** Emits when a connection is closed. */
|
||||||
addEventListener(type: "disconnected", listener: () => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: "disconnected",
|
||||||
|
listener: () => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
|
|
||||||
/** Emits when a reconnect event is successful. */
|
/** Emits when a reconnect event is successful. */
|
||||||
addEventListener(type: "reconnect", listener: () => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: "reconnect",
|
||||||
|
listener: () => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
|
|
||||||
/** Emits when a reconnect fails after @see KeepAliveClientOptions.maxReconnectAttempts attempts. */
|
/** Emits when a reconnect fails after @see KeepAliveClientOptions.maxReconnectAttempts attempts. */
|
||||||
addEventListener(type: "reconnectfailed", listener: () => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: "reconnectfailed",
|
||||||
|
listener: () => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
|
|
||||||
/** Emits when a ping message is received from @see KeepAliveServer from `@prsm/keepalive-ws/server`. */
|
/** Emits when a ping message is received from @see KeepAliveServer from `@prsm/keepalive-ws/server`. */
|
||||||
addEventListener(type: "ping", listener: (ev: CustomEventInit<{}>) => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: "ping",
|
||||||
|
listener: (ev: CustomEventInit<{}>) => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
|
|
||||||
/** Emits when a latency event is received from @see KeepAliveServer from `@prsm/keepalive-ws/server`. */
|
/** Emits when a latency event is received from @see KeepAliveServer from `@prsm/keepalive-ws/server`. */
|
||||||
addEventListener(type: "latency", listener: (ev: CustomEventInit<LatencyPayload>) => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: "latency",
|
||||||
|
listener: (ev: CustomEventInit<LatencyPayload>) => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
|
|
||||||
addEventListener(type: string, listener: (ev: CustomEvent) => any, options?: boolean | AddEventListenerOptions): void;
|
addEventListener(
|
||||||
|
type: string,
|
||||||
|
listener: (ev: CustomEvent) => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Connection extends EventTarget {
|
export class Connection extends EventTarget {
|
||||||
@ -64,7 +116,11 @@ export class Connection extends EventTarget {
|
|||||||
* @param listener The function to call when the event is fired.
|
* @param listener The function to call when the event is fired.
|
||||||
* @param options An options object that specifies characteristics about the event listener.
|
* @param options An options object that specifies characteristics about the event listener.
|
||||||
*/
|
*/
|
||||||
on(event: string, listener: (ev: CustomEvent) => any, options?: boolean | AddEventListenerOptions) {
|
on(
|
||||||
|
event: string,
|
||||||
|
listener: (ev: CustomEvent) => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
) {
|
||||||
this.addEventListener(event, listener, options);
|
this.addEventListener(event, listener, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +130,11 @@ export class Connection extends EventTarget {
|
|||||||
* @param listener The event listener to be removed.
|
* @param listener The event listener to be removed.
|
||||||
* @param options An options object that specifies characteristics about the event listener.
|
* @param options An options object that specifies characteristics about the event listener.
|
||||||
*/
|
*/
|
||||||
off(event: string, listener: (ev: CustomEvent) => any, options?: boolean | AddEventListenerOptions) {
|
off(
|
||||||
|
event: string,
|
||||||
|
listener: (ev: CustomEvent) => any,
|
||||||
|
options?: boolean | AddEventListenerOptions,
|
||||||
|
) {
|
||||||
this.removeEventListener(event, listener, options);
|
this.removeEventListener(event, listener, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,24 +179,28 @@ export class Connection extends EventTarget {
|
|||||||
|
|
||||||
if (data.command === "latency:request") {
|
if (data.command === "latency:request") {
|
||||||
this.dispatchEvent(
|
this.dispatchEvent(
|
||||||
new CustomEvent<LatencyPayload>(
|
new CustomEvent<LatencyPayload>("latency:request", {
|
||||||
"latency:request",
|
detail: { latency: data.payload.latency ?? undefined },
|
||||||
{ detail: { latency: data.payload.latency ?? undefined }}
|
}),
|
||||||
)
|
);
|
||||||
|
this.command(
|
||||||
|
"latency:response",
|
||||||
|
{ latency: data.payload.latency ?? undefined },
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
this.command("latency:response", { latency: data.payload.latency ?? undefined }, null);
|
|
||||||
} else if (data.command === "latency") {
|
} else if (data.command === "latency") {
|
||||||
this.dispatchEvent(
|
this.dispatchEvent(
|
||||||
new CustomEvent<LatencyPayload>(
|
new CustomEvent<LatencyPayload>("latency", {
|
||||||
"latency",
|
detail: { latency: data.payload ?? undefined },
|
||||||
{ detail: { latency: data.payload ?? undefined }}
|
}),
|
||||||
)
|
|
||||||
);
|
);
|
||||||
} else if (data.command === "ping") {
|
} else if (data.command === "ping") {
|
||||||
this.dispatchEvent(new CustomEvent("ping", {}));
|
this.dispatchEvent(new CustomEvent("ping", {}));
|
||||||
this.command("pong", {}, null);
|
this.command("pong", {}, null);
|
||||||
} else {
|
} else {
|
||||||
this.dispatchEvent(new CustomEvent(data.command, { detail: data.payload }));
|
this.dispatchEvent(
|
||||||
|
new CustomEvent(data.command, { detail: data.payload }),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.callbacks[data.id]) {
|
if (this.callbacks[data.id]) {
|
||||||
@ -148,7 +212,12 @@ export class Connection extends EventTarget {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async command(command: string, payload: any, expiresIn: number = 30_000, callback: Function | null = null) {
|
async command(
|
||||||
|
command: string,
|
||||||
|
payload: any,
|
||||||
|
expiresIn: number = 30_000,
|
||||||
|
callback: Function | null = null,
|
||||||
|
) {
|
||||||
const id = this.ids.reserve();
|
const id = this.ids.reserve();
|
||||||
const cmd = { id, command, payload: payload ?? {} };
|
const cmd = { id, command, payload: payload ?? {} };
|
||||||
|
|
||||||
|
|||||||
@ -319,6 +319,13 @@ export class KeepAliveServer extends WebSocketServer {
|
|||||||
this.rooms[roomName].delete(connection.id);
|
this.rooms[roomName].delete(connection.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
removeFromAllRooms(connection: Connection | string) {
|
||||||
|
const connectionId = typeof connection === "string" ? connection : connection.id;
|
||||||
|
Object.keys(this.rooms).forEach((roomName) => {
|
||||||
|
this.rooms[roomName].delete(connectionId);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a "room", which is simply a Set of Connection ids.
|
* Returns a "room", which is simply a Set of Connection ids.
|
||||||
* @param roomName
|
* @param roomName
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user