@alipay/faas-biz-server-sdk
Advanced tools
Comparing version 1.1.1-alpha.4 to 1.1.1-alpha.5
@@ -15,2 +15,3 @@ "use strict"; | ||
} | ||
this.laters = []; | ||
return result; | ||
@@ -17,0 +18,0 @@ } |
import { StorageOptions } from "../../types/Websocket"; | ||
import Chain from "./Chain"; | ||
import WSBaseStorage from "./Storage/BaseStorage"; | ||
import { Room } from "./Room"; | ||
@@ -10,2 +11,3 @@ export default class FaasWebSocketIO extends Chain { | ||
private initStorage; | ||
get storage(): WSBaseStorage; | ||
rooms(): Promise<Set<Room>>; | ||
@@ -17,2 +19,4 @@ getRoom(roomId: string): Promise<Room>; | ||
broadcast(roomId: string, event: string, data: any): this; | ||
close(roomId: string): this; | ||
closeAll(namespace?: string): this; | ||
} |
@@ -20,3 +20,3 @@ "use strict"; | ||
} | ||
initStorage(options) { | ||
initStorage(options = {}) { | ||
if (options.type === Websocket_1.WSStorageType.REDIS) { | ||
@@ -27,2 +27,8 @@ return new RedisStorage_1.default(options.config); | ||
} | ||
get storage() { | ||
if (!this._storage) { | ||
this._storage = new MongoDBStorage_1.default({}); | ||
} | ||
return this._storage; | ||
} | ||
async rooms() { | ||
@@ -67,3 +73,3 @@ return await this._storage.rooms(); | ||
event, | ||
data, | ||
data: data, | ||
}); | ||
@@ -87,3 +93,15 @@ } | ||
} | ||
close(roomId) { | ||
this.later(async () => { | ||
await this._storage.close(roomId); | ||
}); | ||
return this; | ||
} | ||
closeAll(namespace) { | ||
this.later(async () => { | ||
await this._storage.closeAll(namespace); | ||
}); | ||
return this; | ||
} | ||
} | ||
exports.default = FaasWebSocketIO; |
@@ -6,3 +6,3 @@ import WSStorage from './Storage/BaseStorage'; | ||
sockets: FaasWebSocket[]; | ||
properties: Map<string, any>; | ||
properties: Record<string, any>; | ||
constructor(roomId: string); | ||
@@ -15,3 +15,11 @@ setProperty(key: string, value: any): void; | ||
toString(): string; | ||
toJSON(): { | ||
roomId: string; | ||
sockets: { | ||
connectionId: string; | ||
properties: Record<string, any>; | ||
}[]; | ||
properties: Record<string, any>; | ||
}; | ||
static initByData(data: any, storage?: WSStorage): Room; | ||
} |
@@ -11,13 +11,13 @@ "use strict"; | ||
sockets; | ||
properties; | ||
properties = {}; | ||
constructor(roomId) { | ||
this.roomId = roomId; | ||
this.sockets = []; | ||
this.properties = new Map(); | ||
this.properties = {}; | ||
} | ||
setProperty(key, value) { | ||
this.properties.set(key, value); | ||
this.properties[key] = value; | ||
} | ||
getProperty(key) { | ||
this.properties.get(key); | ||
this.properties[key]; | ||
} | ||
@@ -37,4 +37,15 @@ async join(ws) { | ||
toString() { | ||
return JSON.stringify(this); | ||
return JSON.stringify({ | ||
roomId: this.roomId, | ||
sockets: this.sockets.map(socket => socket.toString()), | ||
properties: this.properties, | ||
}); | ||
} | ||
toJSON() { | ||
return { | ||
roomId: this.roomId, | ||
sockets: this.sockets.map(socket => socket.toJSON()), | ||
properties: this.properties, | ||
}; | ||
} | ||
static initByData(data, storage) { | ||
@@ -62,3 +73,10 @@ if (typeof data === 'string') { | ||
if ('properties' in data) { | ||
room.properties = new Map(data['properties']); | ||
if (typeof data['properties'] === 'string') { | ||
try { | ||
data['properties'] = JSON.parse(data['properties']); | ||
} | ||
catch (error) { | ||
} | ||
} | ||
room.properties = data['properties'] || {}; | ||
} | ||
@@ -65,0 +83,0 @@ return room; |
@@ -11,2 +11,3 @@ import { Room } from "../Room"; | ||
constructor(options?: MongoDBStorageConfig); | ||
private getCollection; | ||
close(roomId: string): Promise<void>; | ||
@@ -13,0 +14,0 @@ getRoom(roomId: string): Promise<Room>; |
@@ -15,9 +15,17 @@ "use strict"; | ||
this.db = new faas_server_sdk_1.Cloud().database(); | ||
this.collection = options.collection || '_faas_websocket_rooms'; | ||
this.collection = options.collection || 'faas_websocket_data'; | ||
} | ||
async getCollection() { | ||
if ((await this.db.getCollection(this.collection)) === null) { | ||
await this.db.createCollection(this.collection); | ||
} | ||
return this.db.collection(this.collection); | ||
} | ||
async close(roomId) { | ||
await this.db.collection(this.collection).where({ roomId: roomId }).remove(); | ||
await (await this.getCollection()).where({ roomId: roomId }).remove(); | ||
} | ||
async getRoom(roomId) { | ||
const res = await this.db.collection(this.collection).where({ roomId: roomId }).get(); | ||
const res = await (await this.getCollection()).where({ roomId: roomId }).get(); | ||
if (!res || res.length === 0) | ||
return null; | ||
return Room_1.Room.initByData(res[0], this); | ||
@@ -28,7 +36,7 @@ } | ||
if (!room) { | ||
await this.db.collection(this.collection).add({ data: { roomId: roomId, sockets: [ws] } }); | ||
await (await this.getCollection()).add({ data: { roomId: roomId, sockets: [ws.toJSON()] } }); | ||
} | ||
else { | ||
room.sockets.push(ws); | ||
await this.db.collection(this.collection).update({ data: room }); | ||
room.join(ws); | ||
await (await this.getCollection()).where({ roomId: roomId }).update({ data: room.toJSON() }); | ||
} | ||
@@ -40,4 +48,9 @@ } | ||
return; | ||
room.sockets = room.sockets.filter(socket => socket !== ws); | ||
await this.db.collection(this.collection).update({ data: room }); | ||
room.leave(ws); | ||
if (!room.sockets.length) { | ||
await this.close(roomId); | ||
} | ||
else { | ||
await (await this.getCollection()).where({ roomId: roomId }).update({ data: room.toJSON() }); | ||
} | ||
} | ||
@@ -47,3 +60,3 @@ async rooms(namespace) { | ||
const regex = new RegExp(`^${namespace}`); | ||
const res = await this.db.collection(this.collection).where({ | ||
const res = await (await this.getCollection()).where({ | ||
roomId: { $regex: regex } | ||
@@ -54,3 +67,3 @@ }).get(); | ||
else { | ||
const res = await this.db.collection(this.collection).get(); | ||
const res = await (await this.getCollection()).get(); | ||
return new Set(res); | ||
@@ -62,3 +75,3 @@ } | ||
const regex = new RegExp(`^${namespace}`); | ||
await this.db.collection(this.collection).where({ | ||
await (await this.getCollection()).where({ | ||
roomId: { $regex: regex } | ||
@@ -68,3 +81,3 @@ }).remove(); | ||
else { | ||
await this.db.collection(this.collection).remove(); | ||
await (await this.getCollection()).remove(); | ||
} | ||
@@ -78,3 +91,3 @@ } | ||
room.setProperty(key, value); | ||
await this.db.collection(this.collection).where({ roomId: roomId }).update({ data: room }); | ||
await (await this.getCollection()).where({ roomId: roomId }).update({ data: room.toJSON() }); | ||
} | ||
@@ -81,0 +94,0 @@ async getRoomProperty(roomId, key) { |
@@ -17,2 +17,4 @@ "use strict"; | ||
const res = await this.client.hgetall(roomId); | ||
if (!res) | ||
return null; | ||
return Room_1.Room.initByData(res, this); | ||
@@ -35,3 +37,8 @@ } | ||
room.leave(ws); | ||
await this.client.hmset(roomId, room); | ||
if (!room.sockets.length) { | ||
await this.close(roomId); | ||
} | ||
else { | ||
await this.client.hmset(roomId, room); | ||
} | ||
} | ||
@@ -38,0 +45,0 @@ async rooms(namespace) { |
@@ -14,3 +14,3 @@ import WS from '@alipay/faas-server-sdk/lib/Websocket'; | ||
private _storage; | ||
properties: Map<string, any>; | ||
properties: Record<string, any>; | ||
payload: any; | ||
@@ -24,2 +24,6 @@ constructor(options: WebSocketOptions); | ||
toString(): string; | ||
toJSON(): { | ||
connectionId: string; | ||
properties: Record<string, any>; | ||
}; | ||
static initByData(data: any): FaasWebSocket; | ||
@@ -26,0 +30,0 @@ join(roomId: string): this; |
@@ -15,3 +15,3 @@ "use strict"; | ||
_storage; | ||
properties = new Map(); | ||
properties = {}; | ||
payload; | ||
@@ -23,4 +23,5 @@ constructor(options) { | ||
this._storage = this.initStorage(options.storageOptions); | ||
this.properties = {}; | ||
} | ||
initStorage(options) { | ||
initStorage(options = {}) { | ||
if (options.type === Websocket_1.WSStorageType.REDIS) { | ||
@@ -41,6 +42,6 @@ return new RedisStorage_1.default(options.config); | ||
setProperty(key, value) { | ||
this.properties.set(key, value); | ||
this.properties[key] = value; | ||
} | ||
getProperty(key) { | ||
this.properties.get(key); | ||
this.properties[key]; | ||
} | ||
@@ -51,2 +52,8 @@ toString() { | ||
} | ||
toJSON() { | ||
return { | ||
connectionId: this.connectionId, | ||
properties: this.properties, | ||
}; | ||
} | ||
static initByData(data) { | ||
@@ -69,3 +76,10 @@ if (typeof data === 'string') { | ||
if ('properties' in data) { | ||
ws.properties = new Map(data['properties']); | ||
if (typeof data['properties'] === 'string') { | ||
try { | ||
data['properties'] = JSON.parse(data['properties']); | ||
} | ||
catch (error) { | ||
} | ||
} | ||
ws.properties = data['properties'] || {}; | ||
} | ||
@@ -72,0 +86,0 @@ return ws; |
@@ -23,4 +23,4 @@ import { MongoDBStorageConfig } from "../models/websocket/Storage/MongoDBStorage"; | ||
export interface StorageOptions { | ||
type: WSStorageType; | ||
config: MongoDBStorageConfig | RedisStorageConfig; | ||
type?: WSStorageType; | ||
config?: MongoDBStorageConfig | RedisStorageConfig; | ||
} |
{ | ||
"name": "@alipay/faas-biz-server-sdk", | ||
"version": "1.1.1-alpha.4", | ||
"version": "1.1.1-alpha.5", | ||
"description": "支付宝云开发业务 SDK(函数端)", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
158454
3607