-
Notifications
You must be signed in to change notification settings - Fork 32
/
common.ts
191 lines (160 loc) · 4.59 KB
/
common.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { EventEmitter } from 'events'
import { encoder, makeDecoder, Inflates } from './buffer'
export type LiveOptions = { protover?: 1 | 2 | 3, key?: string, authBody?: any }
export const relayEvent = Symbol('relay')
class NiceEventEmitter extends EventEmitter {
emit(eventName: string | symbol, ...params: any[]) {
super.emit(eventName, ...params)
super.emit(relayEvent, eventName, ...params)
return true
}
}
export class Live extends NiceEventEmitter {
roomid: number
online: number
live: boolean
closed: boolean
timeout: ReturnType<typeof setTimeout>
inflates: Inflates
send: (data: Buffer) => void
close: () => void
constructor(inflates: Inflates, roomid: number, { send, close, protover = 2, key, authBody }: { send: (data: Buffer) => void, close: () => void } & LiveOptions) {
if (typeof roomid !== 'number' || Number.isNaN(roomid)) {
throw new Error(`roomid ${roomid} must be Number not NaN`)
}
super()
this.inflates = inflates
this.roomid = roomid
this.online = 0
this.live = false
this.closed = false
this.timeout = setTimeout(() => { }, 0)
this.send = send
this.close = () => {
this.closed = true
close()
}
this.on('message', async buffer => {
const packs = await makeDecoder(inflates)(buffer)
packs.forEach(({ type, data }) => {
if (type === 'welcome') {
this.live = true
this.emit('live')
this.send(encoder('heartbeat', inflates))
}
if (type === 'heartbeat') {
this.online = data
clearTimeout(this.timeout)
this.timeout = setTimeout(() => this.heartbeat(), 1000 * 30)
this.emit('heartbeat', this.online)
}
if (type === 'message') {
this.emit('msg', data)
const cmd = data.cmd || (data.msg && data.msg.cmd)
if (cmd) {
if (cmd.includes('DANMU_MSG')) {
this.emit('DANMU_MSG', data)
} else {
this.emit(cmd, data)
}
}
}
})
})
this.on('open', () => {
if (authBody) {
this.send(authBody)
} else {
const hi: { uid: number, roomid: number, protover: number, platform: string, clientver: string, type: number, key?: string } = { uid: 0, roomid, protover, platform: 'web', clientver: '2.0.11', type: 2 }
if (key) {
hi.key = key
}
const buf = encoder('join', inflates, hi)
this.send(buf)
}
})
this.on('close', () => {
clearTimeout(this.timeout)
})
this.on('_error', error => {
this.close()
this.emit('error', error)
})
}
heartbeat() {
this.send(encoder('heartbeat', this.inflates))
}
getOnline() {
this.heartbeat()
return new Promise<number>(resolve => this.once('heartbeat', resolve))
}
}
export class KeepLive<Base extends typeof Live> extends EventEmitter {
params: ConstructorParameters<Base>
closed: boolean
interval: number
timeout: number
connection: InstanceType<Base>
Base: Base
constructor(Base: Base, ...params: ConstructorParameters<Base>) {
super()
this.params = params
this.closed = false
this.interval = 100
this.timeout = 45 * 1000
this.connection = new (Base as any)(...this.params)
this.Base = Base
this.connect(false)
}
connect(reconnect = true) {
if (reconnect) {
this.connection.close()
this.connection = new (this.Base as any)(...this.params)
}
const connection = this.connection
let timeout = setTimeout(() => {
connection.close()
connection.emit('timeout')
}, this.timeout)
connection.on(relayEvent, (eventName: string, ...params: any[]) => {
if (eventName !== 'error') {
this.emit(eventName, ...params)
}
})
connection.on('error', (e: any) => this.emit('e', e))
connection.on('close', () => {
if (!this.closed) {
setTimeout(() => this.connect(), this.interval)
}
})
connection.on('heartbeat', () => {
clearTimeout(timeout)
timeout = setTimeout(() => {
connection.close()
connection.emit('timeout')
}, this.timeout)
})
connection.on('close', () => {
clearTimeout(timeout)
})
}
get online() {
return this.connection.online
}
get roomid() {
return this.connection.roomid
}
close() {
this.closed = true
this.connection.close()
}
heartbeat() {
return this.connection.heartbeat()
}
getOnline() {
return this.connection.getOnline()
}
send(data: Buffer) {
return this.connection.send(data)
}
}