@alipay/faas-biz-server-sdk
Advanced tools
Comparing version 1.1.2-alpha.2 to 1.1.2-alpha.3
@@ -5,3 +5,2 @@ import { FaasWebSocketIOConfig } from './types/Websocket'; | ||
import WSSet from './base/WsSet'; | ||
import { WebSocketEvent } from './base/Constants'; | ||
import Room from './Room'; | ||
@@ -27,2 +26,4 @@ import Socket from './Socket'; | ||
* 获取房间列表 | ||
* get room list | ||
* @example console.log(await io.rooms); | ||
* @return | ||
@@ -33,2 +34,4 @@ */ | ||
* 获取房间内的所有连接 | ||
* get all connections in the room | ||
* @example console.log(await io.sockets); | ||
* @return {Promise<SocketSet>} | ||
@@ -38,3 +41,5 @@ */ | ||
/** | ||
* 广播 | ||
* 发送数据除了当前连接外广播给所有连接,如果没有设置 currentConnectionId 则广播给所有连接 | ||
* send data to all connections except the current connection, if currentConnectionId is not set, broadcast to all connections | ||
* @example await io.broadcast.emit('event', 'data'); | ||
*/ | ||
@@ -44,2 +49,4 @@ get broadcast(): this; | ||
* 当前所有 sockets 加入指定的房间 | ||
* all current sockets join the specified room | ||
* @example await io.socketsJoin('roomId'); | ||
* @param roomId 房间 ID, 可以是数组 | ||
@@ -50,2 +57,4 @@ */ | ||
* 当前所有 sockets 离开指定的房间 | ||
* all current sockets leave the specified room | ||
* @example await io.socketsLeave('roomId'); | ||
* @param roomId 房间 ID, 可以是数组 | ||
@@ -55,3 +64,5 @@ */ | ||
/** | ||
* specify the namespace to filter | ||
* 筛选指定命名空间 | ||
* @example io.of('/namespace').to('roomId').emit('event', 'data'); | ||
* @param namespace | ||
@@ -62,3 +73,5 @@ * @return | ||
/** | ||
* specify the room to filter | ||
* 筛选指定房间 | ||
* @example await io.to('roomId').emit('event', 'data'); | ||
* @param roomId | ||
@@ -69,3 +82,5 @@ * @return | ||
/** | ||
* specify the room to filter | ||
* 筛选指定房间 | ||
* @example await io.in('roomId').emit('event', 'data'); | ||
* @param roomId | ||
@@ -76,3 +91,5 @@ * @return | ||
/** | ||
* exclude specified room | ||
* 排除指定房间 | ||
* @example await io.except('roomId').emit('event', 'data'); | ||
* @param roomId 房间 ID, 可以是数组 | ||
@@ -88,5 +105,6 @@ * @return | ||
*/ | ||
on(event: WebSocketEvent, handler: (socket: FaasWebSocket) => void): void; | ||
/** | ||
* 发送数据 | ||
* send data to all sockets in the room or all sockets in the namespace if no room is filtered | ||
* 发送数据 给房间内的所有 sockets 或者如果没有筛选房间则给命名空间内的所有 sockets | ||
* @example await io.emit('event', 'data'); | ||
* @param event 自定义事件 | ||
@@ -98,3 +116,5 @@ * @param data 数据 | ||
/** | ||
* 发送数据 | ||
* send data to all sockets in the room or all sockets in the namespace if no room is filtered | ||
* 发送数据 给房间内的所有 sockets 或者如果没有筛选房间则给命名空间内的所有 sockets | ||
* @example await io.send('data'); | ||
* @param data | ||
@@ -104,8 +124,17 @@ */ | ||
/** | ||
* 关闭连接 close sockets connection,如果没有筛选房间则关闭所有连接 | ||
* close sockets connection and delete storage data, if no room is filtered, close all connections, if there is a room filter, close all connections in the room | ||
* 主动关闭连接,同时删除 storage 中对应的数据,如果没有筛选房间则关闭所有连接, 如果有筛选房间则关闭房间内的所有连接 | ||
* @example await io.close(); | ||
* @return | ||
*/ | ||
close(): unknown; | ||
/** | ||
* only delete storage data, do not close the connection, if no room is filtered, delete all connections, if there is a room filter, delete all connections in the room | ||
* 仅删除 storage 中对应的数据,不会关闭连接,如果没有筛选房间则删除所有连接, 如果有筛选房间则删除房间内的所有连接 | ||
* @example await io.delete(); | ||
* @return | ||
*/ | ||
delete(): unknown; | ||
protected afterChain(): Promise<void>; | ||
} | ||
export {}; |
@@ -43,2 +43,4 @@ "use strict"; | ||
* 获取房间列表 | ||
* get room list | ||
* @example console.log(await io.rooms); | ||
* @return | ||
@@ -57,2 +59,4 @@ */ | ||
* 获取房间内的所有连接 | ||
* get all connections in the room | ||
* @example console.log(await io.sockets); | ||
* @return {Promise<SocketSet>} | ||
@@ -75,3 +79,5 @@ */ | ||
/** | ||
* 广播 | ||
* 发送数据除了当前连接外广播给所有连接,如果没有设置 currentConnectionId 则广播给所有连接 | ||
* send data to all connections except the current connection, if currentConnectionId is not set, broadcast to all connections | ||
* @example await io.broadcast.emit('event', 'data'); | ||
*/ | ||
@@ -84,2 +90,4 @@ get broadcast() { | ||
* 当前所有 sockets 加入指定的房间 | ||
* all current sockets join the specified room | ||
* @example await io.socketsJoin('roomId'); | ||
* @param roomId 房间 ID, 可以是数组 | ||
@@ -108,2 +116,4 @@ */ | ||
* 当前所有 sockets 离开指定的房间 | ||
* all current sockets leave the specified room | ||
* @example await io.socketsLeave('roomId'); | ||
* @param roomId 房间 ID, 可以是数组 | ||
@@ -131,3 +141,5 @@ */ | ||
/** | ||
* specify the namespace to filter | ||
* 筛选指定命名空间 | ||
* @example io.of('/namespace').to('roomId').emit('event', 'data'); | ||
* @param namespace | ||
@@ -154,3 +166,5 @@ * @return | ||
/** | ||
* specify the room to filter | ||
* 筛选指定房间 | ||
* @example await io.to('roomId').emit('event', 'data'); | ||
* @param roomId | ||
@@ -173,3 +187,5 @@ * @return | ||
/** | ||
* specify the room to filter | ||
* 筛选指定房间 | ||
* @example await io.in('roomId').emit('event', 'data'); | ||
* @param roomId | ||
@@ -182,3 +198,5 @@ * @return | ||
/** | ||
* exclude specified room | ||
* 排除指定房间 | ||
* @example await io.except('roomId').emit('event', 'data'); | ||
* @param roomId 房间 ID, 可以是数组 | ||
@@ -207,17 +225,19 @@ * @return | ||
*/ | ||
on(event, handler) { | ||
addEventListener(`websocket:${event}`, (event) => { | ||
event.handle((async () => { | ||
const socket = new Socket_1.default({ | ||
connectionId: event.connectionId, | ||
payload: event.payload, | ||
namespace: this.namespace, | ||
}); | ||
socket.payload = event.payload; | ||
handler(socket); | ||
})()); | ||
}); | ||
} | ||
// on(event: WebSocketEvent, handler: (socket: FaasWebSocket) => void) { | ||
// addEventListener(`websocket:${event}`, (event: any) => { | ||
// event.handle((async () => { | ||
// const socket = new FaasWebSocket({ | ||
// connectionId: event.connectionId, | ||
// payload: event.payload, | ||
// namespace: this.namespace, | ||
// }); | ||
// socket.payload = event.payload; | ||
// handler(socket); | ||
// })()); | ||
// }); | ||
// } | ||
/** | ||
* 发送数据 | ||
* send data to all sockets in the room or all sockets in the namespace if no room is filtered | ||
* 发送数据 给房间内的所有 sockets 或者如果没有筛选房间则给命名空间内的所有 sockets | ||
* @example await io.emit('event', 'data'); | ||
* @param event 自定义事件 | ||
@@ -264,3 +284,5 @@ * @param data 数据 | ||
/** | ||
* 发送数据 | ||
* send data to all sockets in the room or all sockets in the namespace if no room is filtered | ||
* 发送数据 给房间内的所有 sockets 或者如果没有筛选房间则给命名空间内的所有 sockets | ||
* @example await io.send('data'); | ||
* @param data | ||
@@ -306,3 +328,5 @@ */ | ||
/** | ||
* 关闭连接 close sockets connection,如果没有筛选房间则关闭所有连接 | ||
* close sockets connection and delete storage data, if no room is filtered, close all connections, if there is a room filter, close all connections in the room | ||
* 主动关闭连接,同时删除 storage 中对应的数据,如果没有筛选房间则关闭所有连接, 如果有筛选房间则关闭房间内的所有连接 | ||
* @example await io.close(); | ||
* @return | ||
@@ -357,2 +381,55 @@ */ | ||
} | ||
/** | ||
* only delete storage data, do not close the connection, if no room is filtered, delete all connections, if there is a room filter, delete all connections in the room | ||
* 仅删除 storage 中对应的数据,不会关闭连接,如果没有筛选房间则删除所有连接, 如果有筛选房间则删除房间内的所有连接 | ||
* @example await io.delete(); | ||
* @return | ||
*/ | ||
delete() { | ||
this.later(async () => { | ||
if (!this._rooms) { | ||
const sockets = await Socket_3.default.getSockets(this.namespace); | ||
if (!sockets || !sockets.size) { | ||
return; | ||
} | ||
let currentSocket = null; | ||
for (const socket of sockets) { | ||
if (socket.id === this.currentConnectionId) { | ||
currentSocket = socket; | ||
continue; | ||
} | ||
await socket.delete(); | ||
} | ||
if (currentSocket) { | ||
await currentSocket.delete(); | ||
} | ||
return; | ||
} | ||
if (!this._rooms.size) { | ||
return; | ||
} | ||
let currentSocket = null; | ||
const deletedSet = new Set(); | ||
for (const room of this._rooms) { | ||
const sockets = await room.getSockets(); | ||
for (const socket of sockets) { | ||
if (deletedSet.has(socket.id)) { | ||
continue; | ||
} | ||
if (socket.id === this.currentConnectionId) { | ||
currentSocket = socket; | ||
continue; | ||
} | ||
await socket.delete(); | ||
deletedSet.add(socket.id); | ||
} | ||
if (currentSocket) { | ||
await currentSocket.delete(); | ||
} | ||
} | ||
return; | ||
}); | ||
// 此处终结调用链 | ||
return this; | ||
} | ||
async afterChain() { | ||
@@ -359,0 +436,0 @@ await super.afterChain(); |
@@ -14,2 +14,4 @@ import Socket from './models/Socket'; | ||
* get socketIds | ||
* 获取房间内的 socketIds | ||
* @example console.log(room.socketIds); | ||
* @return | ||
@@ -21,2 +23,4 @@ * @readonly | ||
* set property | ||
* 设置属性 | ||
* @example await room.setProperty('key', 'value'); | ||
* @param key | ||
@@ -30,2 +34,4 @@ * @param value | ||
* get property | ||
* 获取属性 | ||
* @example await room.getProperty('key'); | ||
* @param key | ||
@@ -38,2 +44,4 @@ * @return | ||
* socket join room | ||
* 加入房间 | ||
* @example await room.join(ws); | ||
* @param ws | ||
@@ -46,2 +54,4 @@ * @return | ||
* socket leave room | ||
* 离开房间 | ||
* @example await room.leave(ws); | ||
* @param ws | ||
@@ -53,2 +63,4 @@ * @return | ||
* get sockets in the room | ||
* 获取房间内的 sockets | ||
* @example await room.getSockets(); | ||
* @return | ||
@@ -59,2 +71,4 @@ */ | ||
* get sockets in the room | ||
* 获取房间内的 sockets | ||
* @example await room.sockets(); | ||
* @return | ||
@@ -65,2 +79,4 @@ */ | ||
* get sockets in the room | ||
* 获取房间内的 sockets | ||
* @example await room.members(); | ||
* @return | ||
@@ -71,2 +87,4 @@ */ | ||
* broadcast data to all sockets in the room | ||
* 广播数据给房间内的所有 sockets | ||
* @example await room.broadcast('data'); | ||
* @param data | ||
@@ -78,2 +96,4 @@ * @return | ||
* get room by id | ||
* 根据 roomId 获取房间 | ||
* @example await Room.get('roomId'); | ||
* @param roomId | ||
@@ -86,2 +106,4 @@ * @param namespace | ||
* find rooms by namespace | ||
* 根据 namespace 获取房间列表 | ||
* @example await Room.find('namespace'); | ||
* @param namespace | ||
@@ -93,2 +115,4 @@ * @return rooms | ||
* init room from dto | ||
* 从 DTO 初始化房间 | ||
* @example Room.init(room); | ||
* @param room | ||
@@ -95,0 +119,0 @@ * @return room |
@@ -21,2 +21,4 @@ "use strict"; | ||
* get socketIds | ||
* 获取房间内的 socketIds | ||
* @example console.log(room.socketIds); | ||
* @return | ||
@@ -30,2 +32,4 @@ * @readonly | ||
* set property | ||
* 设置属性 | ||
* @example await room.setProperty('key', 'value'); | ||
* @param key | ||
@@ -46,2 +50,4 @@ * @param value | ||
* get property | ||
* 获取属性 | ||
* @example await room.getProperty('key'); | ||
* @param key | ||
@@ -64,2 +70,4 @@ * @return | ||
* socket join room | ||
* 加入房间 | ||
* @example await room.join(ws); | ||
* @param ws | ||
@@ -81,2 +89,4 @@ * @return | ||
* socket leave room | ||
* 离开房间 | ||
* @example await room.leave(ws); | ||
* @param ws | ||
@@ -93,2 +103,4 @@ * @return | ||
* get sockets in the room | ||
* 获取房间内的 sockets | ||
* @example await room.getSockets(); | ||
* @return | ||
@@ -101,2 +113,4 @@ */ | ||
* get sockets in the room | ||
* 获取房间内的 sockets | ||
* @example await room.sockets(); | ||
* @return | ||
@@ -112,2 +126,4 @@ */ | ||
* get sockets in the room | ||
* 获取房间内的 sockets | ||
* @example await room.members(); | ||
* @return | ||
@@ -123,2 +139,4 @@ */ | ||
* broadcast data to all sockets in the room | ||
* 广播数据给房间内的所有 sockets | ||
* @example await room.broadcast('data'); | ||
* @param data | ||
@@ -138,2 +156,4 @@ * @return | ||
* get room by id | ||
* 根据 roomId 获取房间 | ||
* @example await Room.get('roomId'); | ||
* @param roomId | ||
@@ -149,2 +169,4 @@ * @param namespace | ||
* find rooms by namespace | ||
* 根据 namespace 获取房间列表 | ||
* @example await Room.find('namespace'); | ||
* @param namespace | ||
@@ -164,2 +186,4 @@ * @return rooms | ||
* init room from dto | ||
* 从 DTO 初始化房间 | ||
* @example Room.init(room); | ||
* @param room | ||
@@ -166,0 +190,0 @@ * @return room |
@@ -16,2 +16,6 @@ import type WS from '@alipay/faas-server-sdk/lib/Websocket'; | ||
* get socket connection id | ||
* 获取 socket 连接 id | ||
* @example console.log(socket.id); | ||
* @return | ||
* @readonly | ||
*/ | ||
@@ -22,2 +26,4 @@ get id(): string; | ||
* set property | ||
* 设置属性 | ||
* @example await socket.setProperty('key', 'value'); | ||
* @param key | ||
@@ -31,2 +37,4 @@ * @param value | ||
* get property | ||
* 获取属性 | ||
* @example await socket.getProperty('key'); | ||
* @param key | ||
@@ -38,3 +46,5 @@ * @return | ||
/** | ||
* join room | ||
* join room and if the room does not exist, it will be created automatically | ||
* 加入房间,如果房间不存在会自动创建 | ||
* @example await socket.join('room'); | ||
* @param roomId | ||
@@ -46,3 +56,5 @@ * @return | ||
/** | ||
* leave room | ||
* 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 | ||
@@ -54,2 +66,4 @@ * @return | ||
* get rooms | ||
* 获取房间列表 | ||
* @example await socket.rooms(); | ||
* @return | ||
@@ -59,2 +73,5 @@ */ | ||
/** | ||
* emit event | ||
* 发送自定义事件 | ||
* @example await socket.emit('event', 'data'); | ||
* @param event 自定义事件 | ||
@@ -67,2 +84,4 @@ * @param data | ||
* send data | ||
* 发送数据 | ||
* @example await socket.send({ event: 'event', data: 'data' }); | ||
* @param data | ||
@@ -73,3 +92,11 @@ * @return | ||
/** | ||
* close socket | ||
* 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 连接 | ||
* @example await socket.close(); | ||
* @return | ||
@@ -80,2 +107,4 @@ */ | ||
* broadcast data to all sockets in the namespace | ||
* 广播数据给命名空间内的除自己外所有 socket 连接,一般用于群发消息 | ||
* @example await socket.broadcast.send('data'); | ||
* @param data | ||
@@ -87,2 +116,4 @@ * @return | ||
* get all sockets in the namespace | ||
* 获取命名空间内的所有 socket 连接 | ||
* @example await Socket.getSockets(); | ||
* @param namespace | ||
@@ -89,0 +120,0 @@ * @return |
@@ -24,2 +24,6 @@ "use strict"; | ||
* get socket connection id | ||
* 获取 socket 连接 id | ||
* @example console.log(socket.id); | ||
* @return | ||
* @readonly | ||
*/ | ||
@@ -34,2 +38,4 @@ get id() { | ||
* set property | ||
* 设置属性 | ||
* @example await socket.setProperty('key', 'value'); | ||
* @param key | ||
@@ -50,2 +56,4 @@ * @param value | ||
* get property | ||
* 获取属性 | ||
* @example await socket.getProperty('key'); | ||
* @param key | ||
@@ -68,3 +76,5 @@ * @return | ||
/** | ||
* join room | ||
* join room and if the room does not exist, it will be created automatically | ||
* 加入房间,如果房间不存在会自动创建 | ||
* @example await socket.join('room'); | ||
* @param roomId | ||
@@ -88,3 +98,5 @@ * @return | ||
/** | ||
* leave room | ||
* 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 | ||
@@ -101,2 +113,4 @@ * @return | ||
* get rooms | ||
* 获取房间列表 | ||
* @example await socket.rooms(); | ||
* @return | ||
@@ -111,2 +125,5 @@ */ | ||
/** | ||
* emit event | ||
* 发送自定义事件 | ||
* @example await socket.emit('event', 'data'); | ||
* @param event 自定义事件 | ||
@@ -132,3 +149,16 @@ * @param data | ||
else { | ||
await this.ws.send(this.dto.id, { event, data }); | ||
try { | ||
await this.ws.send(this.dto.id, { event, data }); | ||
} | ||
catch (error) { | ||
if (error?.message?.includes('connection not found')) { | ||
await this.dto.close(); | ||
if (process.env.FAAS_DEBUG) { | ||
console.debug(error); | ||
} | ||
} | ||
else { | ||
throw error; | ||
} | ||
} | ||
} | ||
@@ -141,2 +171,4 @@ }); | ||
* send data | ||
* 发送数据 | ||
* @example await socket.send({ event: 'event', data: 'data' }); | ||
* @param data | ||
@@ -160,3 +192,16 @@ * @return | ||
else { | ||
await this.ws.send(this.dto.id, data); | ||
try { | ||
await this.ws.send(this.dto.id, data); | ||
} | ||
catch (error) { | ||
if (error?.message?.includes('connection not found')) { | ||
await this.dto.close(); | ||
if (process.env.FAAS_DEBUG) { | ||
console.debug(error); | ||
} | ||
} | ||
else { | ||
throw error; | ||
} | ||
} | ||
} | ||
@@ -168,3 +213,16 @@ }); | ||
/** | ||
* close socket | ||
* 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.later(async () => { | ||
await this.dto.close(); | ||
}); | ||
return this; | ||
} | ||
/** | ||
* close socket connection and delete socket data in mongo/redis storage (usually used to close socket connection actively) | ||
* 关闭 socket 连接 并删除存储在 mongo/redis 中的 socket 数据,一般用于主动关闭 socket 连接 | ||
* @example await socket.close(); | ||
* @return | ||
@@ -174,3 +232,2 @@ */ | ||
this.later(async () => { | ||
await this.dto.close(); | ||
try { | ||
@@ -189,2 +246,5 @@ await this.ws.close(this.dto.id); | ||
} | ||
finally { | ||
await this.dto.close(); | ||
} | ||
}); | ||
@@ -195,2 +255,4 @@ return this; | ||
* broadcast data to all sockets in the namespace | ||
* 广播数据给命名空间内的除自己外所有 socket 连接,一般用于群发消息 | ||
* @example await socket.broadcast.send('data'); | ||
* @param data | ||
@@ -205,2 +267,4 @@ * @return | ||
* get all sockets in the namespace | ||
* 获取命名空间内的所有 socket 连接 | ||
* @example await Socket.getSockets(); | ||
* @param namespace | ||
@@ -207,0 +271,0 @@ * @return |
{ | ||
"name": "@alipay/faas-biz-server-sdk", | ||
"version": "1.1.2-alpha.2", | ||
"version": "1.1.2-alpha.3", | ||
"description": "支付宝云开发业务 SDK(函数端)", | ||
@@ -8,3 +8,3 @@ "main": "dist/index.js", | ||
"dist", | ||
"docs" | ||
"WS.README.md" | ||
], | ||
@@ -11,0 +11,0 @@ "scripts": { |
300846
9488
10