@alipay/faas-biz-server-sdk
Advanced tools
Comparing version 1.1.1-alpha.6 to 1.1.1-alpha.7
@@ -13,4 +13,4 @@ import { StorageOptions } from "../../types/Websocket"; | ||
get storage(): WSBaseStorage; | ||
rooms(): Promise<Set<import("./Room").Room>>; | ||
getRoom(roomId: string): Promise<import("./Room").Room>; | ||
rooms(): Promise<Set<import("./models/Room").Room>>; | ||
getRoom(roomId: string): Promise<import("./models/Room").Room>; | ||
of(namespace: string): this; | ||
@@ -17,0 +17,0 @@ to(roomId: string): this; |
@@ -22,3 +22,3 @@ "use strict"; | ||
this._storage = this.initStorage(options); | ||
Websocket_2.default._storage = this._storage; | ||
Websocket_2.default.storage = this._storage; | ||
} | ||
@@ -122,3 +122,7 @@ initStorage(options = {}) { | ||
for (const s of room.sockets) { | ||
const socket = await this.storage.getSocket(s); | ||
const socketRes = await this.storage.getSocket(s); | ||
if (!socketRes) { | ||
continue; | ||
} | ||
const socket = Websocket_2.default.initByData(socketRes); | ||
await socket?.close(); | ||
@@ -125,0 +129,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { Room } from "./Room"; | ||
import { Room } from "./models/Room"; | ||
export default class RoomSet extends Set<Room> { | ||
@@ -3,0 +3,0 @@ constructor(array?: Room[]); |
@@ -1,2 +0,2 @@ | ||
import { Room } from "../Room"; | ||
import { Room } from "../models/Room"; | ||
import FaasWebSocket from "../Websocket"; | ||
@@ -13,5 +13,5 @@ export default abstract class WSBaseStorage { | ||
abstract setRoomProperty(roomId: string, key: string, value: any): Promise<void>; | ||
abstract saveSocket(ws: FaasWebSocket): Promise<FaasWebSocket>; | ||
abstract saveSocket(ws: FaasWebSocket): Promise<void>; | ||
abstract getSocket(connectionId: string): Promise<FaasWebSocket | null>; | ||
abstract removeSocket(connectionId: string): Promise<void>; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { Room } from "../Room"; | ||
import { Room } from "../models/Room"; | ||
import FaasWebSocket from "../Websocket"; | ||
@@ -21,5 +21,5 @@ import BaseStorage from "./BaseStorage"; | ||
setRoomProperty(roomId: string, key: string, value: any): Promise<void>; | ||
saveSocket(ws: FaasWebSocket): Promise<FaasWebSocket>; | ||
getSocket(connectionId: string): Promise<FaasWebSocket | null>; | ||
saveSocket(ws: FaasWebSocket): Promise<void>; | ||
getSocket(connectionId: string): Promise<any>; | ||
removeSocket(connectionId: string): Promise<void>; | ||
} |
@@ -7,6 +7,5 @@ "use strict"; | ||
const faas_server_sdk_1 = require("@alipay/faas-server-sdk"); | ||
const Room_1 = require("../Room"); | ||
const Websocket_1 = __importDefault(require("../Websocket")); | ||
const Room_1 = require("../models/Room"); | ||
const BaseStorage_1 = __importDefault(require("./BaseStorage")); | ||
const Websocket_2 = require("../../../types/Websocket"); | ||
const Websocket_1 = require("../../../types/Websocket"); | ||
const RoomSet_1 = __importDefault(require("../RoomSet")); | ||
@@ -23,4 +22,4 @@ class MongoDBStorage extends BaseStorage_1.default { | ||
} | ||
async getCollection(type = Websocket_2.WSDataType.room) { | ||
const collection = type === Websocket_2.WSDataType.room ? this.roomCollection : this.socketCollection; | ||
async getCollection(type = Websocket_1.WSDataType.room) { | ||
const collection = type === Websocket_1.WSDataType.room ? this.roomCollection : this.socketCollection; | ||
if ((await this.db.getCollection(collection)) === null) { | ||
@@ -98,23 +97,22 @@ await this.db.createCollection(collection); | ||
async saveSocket(ws) { | ||
const socket = await this.getSocket(ws.connectionId); | ||
if (!socket) { | ||
await (await this.getCollection(Websocket_2.WSDataType.socket)).add({ data: ws.toJSON() }); | ||
const collection = await this.getCollection(Websocket_1.WSDataType.socket); | ||
const res = await collection.where({ connectionId: ws.connectionId }).get(); | ||
if (!res || res.length === 0) { | ||
await (await this.getCollection(Websocket_1.WSDataType.socket)).add({ data: ws.toJSON() }); | ||
} | ||
else { | ||
await (await this.getCollection(Websocket_2.WSDataType.socket)).where({ connectionId: ws.connectionId }).update({ data: ws.toJSON() }); | ||
await (await this.getCollection(Websocket_1.WSDataType.socket)).where({ connectionId: ws.connectionId }).update({ data: ws.toJSON() }); | ||
} | ||
return ws; | ||
} | ||
async getSocket(connectionId) { | ||
const collection = await this.getCollection(Websocket_2.WSDataType.socket); | ||
const collection = await this.getCollection(Websocket_1.WSDataType.socket); | ||
const res = await collection.where({ connectionId }).get(); | ||
console.trace('getSocket', res); | ||
if (!res || res.length === 0) | ||
return null; | ||
return Websocket_1.default.initByData(res[0]); | ||
return res[0]; | ||
} | ||
async removeSocket(connectionId) { | ||
await (await this.getCollection(Websocket_2.WSDataType.socket)).where({ connectionId }).remove(); | ||
await (await this.getCollection(Websocket_1.WSDataType.socket)).where({ connectionId }).remove(); | ||
} | ||
} | ||
exports.default = MongoDBStorage; |
import { RedisOptions } from '@alipay/faas-server-sdk/lib/Redis'; | ||
import { Room } from "../Room"; | ||
import { Room } from "../models/Room"; | ||
import FaasWebSocket from "../Websocket"; | ||
@@ -21,5 +21,5 @@ import BaseStorage from "./BaseStorage"; | ||
setRoomProperty(roomId: string, key: string, value: any): Promise<void>; | ||
saveSocket(ws: FaasWebSocket): Promise<FaasWebSocket>; | ||
saveSocket(ws: FaasWebSocket): Promise<any>; | ||
getSocket(connectionId: string): Promise<FaasWebSocket>; | ||
removeSocket(connectionId: string): Promise<void>; | ||
} |
@@ -7,3 +7,3 @@ "use strict"; | ||
const faas_server_sdk_1 = require("@alipay/faas-server-sdk"); | ||
const Room_1 = require("../Room"); | ||
const Room_1 = require("../models/Room"); | ||
const Websocket_1 = __importDefault(require("../Websocket")); | ||
@@ -10,0 +10,0 @@ const BaseStorage_1 = __importDefault(require("./BaseStorage")); |
@@ -11,9 +11,10 @@ import WS from '@alipay/faas-server-sdk/lib/Websocket'; | ||
connectionId: string; | ||
static _storage: Storage; | ||
properties: Record<string, any>; | ||
payload: any; | ||
static storage: Storage; | ||
roomIds: string[]; | ||
constructor(options: WebSocketOptions); | ||
get storage(): Storage; | ||
setProperty(key: string, value: any): Promise<void>; | ||
static getSocket(connectionId: string): Promise<any>; | ||
getProperty(key: string): Promise<any>; | ||
toString(): string; | ||
@@ -31,4 +32,3 @@ toJSON(): { | ||
send(data: any): this; | ||
_close(): Promise<void>; | ||
close(): this; | ||
} |
@@ -11,5 +11,5 @@ "use strict"; | ||
connectionId; | ||
static _storage; | ||
properties = {}; | ||
payload; | ||
static storage; | ||
roomIds = []; | ||
@@ -22,5 +22,2 @@ constructor(options) { | ||
} | ||
get storage() { | ||
return FaasWebSocket._storage; | ||
} | ||
async setProperty(key, value) { | ||
@@ -30,2 +27,15 @@ this.properties[key] = value; | ||
} | ||
static async getSocket(connectionId) { | ||
const socketRes = await FaasWebSocket.storage.getSocket(connectionId); | ||
return socketRes; | ||
} | ||
async getProperty(key) { | ||
const dbObj = await FaasWebSocket.storage.getSocket(this.connectionId); | ||
const properties = { | ||
...dbObj.properties, | ||
...this.properties, | ||
}; | ||
this.properties = properties; | ||
return this.properties[key]; | ||
} | ||
toString() { | ||
@@ -43,6 +53,5 @@ const { connectionId, properties, roomIds } = this; | ||
async save() { | ||
await this.storage.saveSocket(this); | ||
await FaasWebSocket.storage.saveSocket(this); | ||
} | ||
static initByData(data) { | ||
console.log(data, 'caonimaya data !!!!!!'); | ||
if (typeof data === 'string') { | ||
@@ -79,11 +88,13 @@ try { | ||
join(roomId) { | ||
this.later(async () => await this.storage.join(roomId, this)); | ||
this.later(async () => await FaasWebSocket.storage.join(roomId, this)); | ||
return this; | ||
} | ||
leave(roomId) { | ||
this.later(async () => await this.storage.leave(roomId, this)); | ||
this.later(async () => await FaasWebSocket.storage.leave(roomId, this)); | ||
return this; | ||
} | ||
emit(event, ...data) { | ||
this.later(async () => await this.ws.send(this.connectionId, { event, data })); | ||
this.later(async () => { | ||
await this.ws.send(this.connectionId, { event, data }); | ||
}); | ||
return this; | ||
@@ -95,15 +106,18 @@ } | ||
} | ||
async _close() { | ||
await this.ws.close(this.connectionId); | ||
const ws = await this.storage.getSocket(this.connectionId); | ||
if (!ws) { | ||
return; | ||
} | ||
for (const roomId of ws.roomIds) { | ||
await this.storage.leave(roomId, ws); | ||
} | ||
await this.storage.removeSocket(this.connectionId); | ||
} | ||
close() { | ||
this.later(async () => await this._close()); | ||
this.later(async () => { | ||
let ws = await FaasWebSocket.getSocket(this.connectionId); | ||
console.log('_close ws', ws, this.connectionId); | ||
if (!ws) { | ||
await this.ws.close(this.connectionId); | ||
return; | ||
} | ||
ws = FaasWebSocket.initByData(ws); | ||
console.log('close ws', ws, ws.roomIds); | ||
for (const roomId of ws.roomIds) { | ||
await this.leave(roomId); | ||
} | ||
await FaasWebSocket.storage.removeSocket(this.connectionId); | ||
await this.ws.close(this.connectionId); | ||
}); | ||
return this; | ||
@@ -110,0 +124,0 @@ } |
{ | ||
"name": "@alipay/faas-biz-server-sdk", | ||
"version": "1.1.1-alpha.6", | ||
"version": "1.1.1-alpha.7", | ||
"description": "支付宝云开发业务 SDK(函数端)", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
165517
228
3786