import type WS from '@alipay/faas-server-sdk/lib/Websocket'; import SocketDTO from './models/Socket'; import BaseDO from './base/BaseDO'; import WsSet from './base/WsSet'; import { CloseOptions } from './types/Websocket'; export default class Socket extends BaseDO { payload: any; ws: WS.WebsocketSDK; private _broadcast; constructor(options: { connectionId: string; payload?: any; namespace?: string; }); /** * get socket connection id * 获取 socket 连接 id * @example console.log(socket.id); * @return * @readonly */ get id(): string; /** * set property * 设置属性 * @example await socket.setProperty('key', 'value'); * @param key * @param value * @return */ setProperty(...args: Array): this; /** * get property * 获取属性 * @example await socket.getProperty('key'); * @example await socket.getProperty('key1', 'key2'); // get multiple properties * @param key * @return */ getProperty(...args: Array): unknown; private _join; /** * join room and if the room does not exist, it will be created automatically * 加入房间,如果房间不存在会自动创建 * @example await socket.join('room'); * @param roomId * @return */ join(roomId: string): this; private _leave; /** * leave room and if there is no other socket in the room, the room will be deleted * 离开房间,如果房间内没有其他 socket 了,房间会被删除 * @example await socket.leave('room'); * @param roomId room id * @return */ leave(roomId: string): this; /** * get rooms * 获取房间列表 * @example await socket.rooms(); * @return */ get rooms(): this; /** * emit event * 发送自定义事件 * @example await socket.emit('event', 'data'); * @param event 自定义事件 * @param data * @return */ emit(event: string, ...data: any[]): unknown; /** * send data * 发送数据 * @example await socket.send({ event: 'event', data: 'data' }); * @param data * @return */ send(data: any): unknown; /** * delete socket in the storage (mongo/redis), will not close the socket connection, usually used to clean up invalid socket data, called in the disconnect event * 删除存储在 mongo/redis 中的 socket 数据,不会关闭 socket 连接,一般用于清理无效的 socket 数据,在 disconnect 事件中调用 * @example await socket.delete(); */ delete(): this; /** * close socket connection and delete socket data in mongo/redis storage (usually used to close socket connection actively) * 关闭 socket 连接 并删除存储在 mongo/redis 中的 socket 数据,一般用于主动关闭 socket 连接 * @param options close options * @example await socket.close(options); * @return */ close(options?: CloseOptions): unknown; /** * is socket alive * 检查 socket 是否存活 * @example await socket.isAlive(); * @return Promise */ get isAlive(): Promise; /** * broadcast data to all sockets in the namespace * 广播数据给命名空间内的除自己外所有 socket 连接,一般用于群发消息 * @example await socket.broadcast.send('data'); * @param data * @return */ get broadcast(): this; /** * get all sockets in the namespace * 获取命名空间内的所有 socket 连接 * @example await Socket.getSockets(); * @param namespace * @return */ static getSockets(namespace?: string): Promise>; static init(socketDto: SocketDTO): Socket; /** * get sockets by room * @param roomId * @param namespace * @return */ static getSocketsByRoom(roomId: string, namespace?: string): Promise>; /** * get socket by connectionId * @param connectionId * @param namespace * @return */ static get(connectionId: string, namespace?: string): Promise; protected afterChain(): Promise; toJSON(): { id: string; namespace: string; }; toString(): string; [Symbol.toPrimitive](hint: string): string | true; }