feat(wip): 事件緩存以備離綫重連重發
This commit is contained in:
@@ -32,6 +32,10 @@ export default class ApiManager {
|
|||||||
static addEventListener(name: string, func: EventCallbackFunction) {
|
static addEventListener(name: string, func: EventCallbackFunction) {
|
||||||
this.event_listeners[name] = func
|
this.event_listeners[name] = func
|
||||||
}
|
}
|
||||||
|
static clients: { [key: string]: string[] } = {}
|
||||||
|
static checkUserIsOnline(userId: string, deviceId: string) {
|
||||||
|
return this.clients[userId].includes(deviceId)
|
||||||
|
}
|
||||||
static initEvents() {
|
static initEvents() {
|
||||||
const io = this.socketIoServer
|
const io = this.socketIoServer
|
||||||
|
|
||||||
@@ -42,10 +46,22 @@ export default class ApiManager {
|
|||||||
|
|
||||||
const deviceId = socket.handshake.auth.device_id as string
|
const deviceId = socket.handshake.auth.device_id as string
|
||||||
|
|
||||||
|
const clientInfo = {
|
||||||
|
userId: '',
|
||||||
|
deviceId,
|
||||||
|
ip
|
||||||
|
}
|
||||||
|
|
||||||
socket.on('disconnect', (_reason) => {
|
socket.on('disconnect', (_reason) => {
|
||||||
|
if (clientInfo.userId == '')
|
||||||
console.log(chalk.yellow('[斷]') + ` ${ip} disconnected`)
|
console.log(chalk.yellow('[斷]') + ` ${ip} disconnected`)
|
||||||
|
else {
|
||||||
|
console.log(chalk.green('[斷]') + ` ${ip} disconnected`)
|
||||||
|
const ls = this.clients[clientInfo.userId]
|
||||||
|
ls.splice(ls.indexOf(deviceId))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
console.log(chalk.green('[連]') + ` ${ip} connected`)
|
console.log(chalk.yellow('[連]') + ` ${ip} connected`)
|
||||||
|
|
||||||
socket.on("The_White_Silk", (name: string, args: { [key: string]: unknown }, callback_: (ret: ApiCallbackMessage) => void) => {
|
socket.on("The_White_Silk", (name: string, args: { [key: string]: unknown }, callback_: (ret: ApiCallbackMessage) => void) => {
|
||||||
function callback(ret: ApiCallbackMessage) {
|
function callback(ret: ApiCallbackMessage) {
|
||||||
@@ -59,9 +75,7 @@ export default class ApiManager {
|
|||||||
})
|
})
|
||||||
console.log(chalk.red('[收]') + ` ${ip} -> ${chalk.yellow(name)} <args: ${JSON.stringify(args)}>`)
|
console.log(chalk.red('[收]') + ` ${ip} -> ${chalk.yellow(name)} <args: ${JSON.stringify(args)}>`)
|
||||||
|
|
||||||
return callback(this.event_listeners[name]?.(args, {
|
return callback(this.event_listeners[name]?.(args, clientInfo) || {
|
||||||
deviceId
|
|
||||||
}) || {
|
|
||||||
code: 501,
|
code: 501,
|
||||||
msg: "Not implmented",
|
msg: "Not implmented",
|
||||||
})
|
})
|
||||||
|
|||||||
4
server/api/EventBean.ts
Normal file
4
server/api/EventBean.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
export default class EventBean {
|
||||||
|
declare event_name: string
|
||||||
|
declare data: unknown
|
||||||
|
}
|
||||||
53
server/api/EventStorer.ts
Normal file
53
server/api/EventStorer.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import { DatabaseSync } from "node:sqlite"
|
||||||
|
import path from 'node:path'
|
||||||
|
|
||||||
|
import config from "../config.ts"
|
||||||
|
import User from "../data/User.ts";
|
||||||
|
import EventBean from "./EventBean.ts";
|
||||||
|
|
||||||
|
export default class EventStorer {
|
||||||
|
static database: DatabaseSync = this.init()
|
||||||
|
|
||||||
|
private static init(): DatabaseSync {
|
||||||
|
const db: DatabaseSync = new DatabaseSync(path.join(config.data_path, 'Events.db'))
|
||||||
|
return db
|
||||||
|
}
|
||||||
|
|
||||||
|
static getInstanceForUser(user: User) {
|
||||||
|
return new EventStorer(user)
|
||||||
|
}
|
||||||
|
|
||||||
|
declare user: User
|
||||||
|
constructor(user: User) {
|
||||||
|
this.user = user
|
||||||
|
|
||||||
|
EventStorer.database.exec(`
|
||||||
|
CREATE TABLE IF NOT EXISTS ${this.getTableName()} (
|
||||||
|
/* 序号 */ count INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
/* 事件 */ event_name TEXT NOT NULL,
|
||||||
|
/* 數據 */ data TEXT NOT NULL,
|
||||||
|
);
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
protected getTableName() {
|
||||||
|
return `events_${this.user.bean.id}`
|
||||||
|
}
|
||||||
|
addEvent(eventName: string, data: unknown) {
|
||||||
|
EventStorer.database.prepare(`INSERT INTO ${this.getTableName()} (
|
||||||
|
event_name,
|
||||||
|
data
|
||||||
|
) VALUES (?, ?);`).run(
|
||||||
|
eventName,
|
||||||
|
JSON.stringify(data)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
getEvents() {
|
||||||
|
return EventStorer.database.prepare(`SELECT * FROM ${this.getTableName()};`).all().map((v: any) => ({
|
||||||
|
...v,
|
||||||
|
data: JSON.parse(v.data)
|
||||||
|
})) as unknown as EventBean[]
|
||||||
|
}
|
||||||
|
clearEvents() {
|
||||||
|
EventStorer.database.prepare(`DELETE FROM ${this.getTableName()};`).run()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user