-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
62 lines (58 loc) · 1.85 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
export * from './model/mod.ts'
import { Logger } from './deps.ts'
import { WebSocketClientConfig, WebSocketServerConfig, WebSocketClient, WebSocketServer } from './obc/mod.ts'
import { Resps, Events, Actions } from './model/mod.ts'
import { Awaitable } from './utils.ts'
export { Logger }
export interface AppConfig {
basic: {
onebotVersion: string
impl: string
platform: string
}
connect: (WebSocketClientConfig | WebSocketServerConfig)[]
connectedHandler?: () => Awaitable<Events[]>
actionHandler: (data: Actions, isMsgpack: boolean) => Awaitable<Resps>
}
export class App {
private controller?: AbortController
private obcs: Array<WebSocketClient | WebSocketServer> = []
public info: AppConfig['basic']
public connectedHandler?: AppConfig['connectedHandler']
public actionHandler?: AppConfig['actionHandler']
constructor(private config: AppConfig) {
this.info = config.basic
this.connectedHandler = config.connectedHandler
this.actionHandler = config.actionHandler
}
start() {
if (this.obcs.length > 0) return
this.controller = new AbortController()
for (const item of this.config.connect) {
let obc
switch (item.protocol) {
case 'ws':
obc = new WebSocketServer(this, item)
break
case 'ws-reverse':
obc = new WebSocketClient(this, item)
break
}
if (obc) {
obc.start(this.controller.signal)
this.obcs.push(obc)
}
}
}
shutdown() {
if (this.controller) {
this.controller.abort()
}
this.obcs = []
}
send(event: Events) {
for (const obc of this.obcs) {
obc.send(event)
}
}
}