-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add deSerialize and Serialize data
- Loading branch information
Showing
13 changed files
with
2,757 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import EventEmitter from 'eventemitter3' | ||
import type { IZlib } from './buffer' | ||
import { LiveProtocolOperation, deserialize, serialize } from './buffer' | ||
import type { ISocket } from './types' | ||
|
||
export interface LiveHelloMessage { | ||
clientver: `${number}.${number}.${number}` | ||
platform: 'web' | ||
protover: 2 | ||
roomid: number | ||
uid: 0 | ||
type: 2 | ||
} | ||
|
||
export const MESSAGE_EVENT = Symbol('') | ||
export const OPEN_EVENT = Symbol('') | ||
export const ERROR_EVENT = Symbol('') | ||
export const CLOSE_EVENT = Symbol('') | ||
|
||
export type ListenerEvents = 'msg' | 'message' | 'open' | 'close' | 'error' | 'live' | ||
|
||
export class Live extends EventEmitter<ListenerEvents | symbol | string> { | ||
readonly roomId: number | ||
|
||
timeout: any | ||
|
||
constructor(roomId: number, private readonly _socket: ISocket, readonly zlib: IZlib<Buffer>) { | ||
if (typeof roomId !== 'number' || Number.isNaN(roomId)) | ||
throw new Error(`roomId ${roomId} must be Number not NaN`) | ||
|
||
super() | ||
|
||
this.on(OPEN_EVENT, () => { | ||
const helloMessage: LiveHelloMessage = { | ||
clientver: '2.0.11', | ||
platform: 'web', | ||
protover: 2, | ||
roomid: this.roomId, | ||
uid: 0, | ||
type: 2, | ||
} | ||
|
||
this._socket.write(serialize(LiveProtocolOperation.USER_AUTHENTICATION, helloMessage)) | ||
this.emit('live') | ||
}) | ||
|
||
this.roomId = roomId | ||
this.bindEvent() | ||
} | ||
|
||
private bindEvent() { | ||
this.on(MESSAGE_EVENT, async (buffer: Uint8Array) => { | ||
const packs = await deserialize(buffer, this.zlib) | ||
|
||
packs.forEach(({ type, data }) => { | ||
if (type === 'welcome') { | ||
this.emit('live') | ||
this._socket.write(serialize(LiveProtocolOperation.HEARTBEAT, {})) | ||
} | ||
if (type === 'heartbeat') | ||
this.timeout = setTimeout(() => this.heartbeat(), 1000 * 30) | ||
// this.emit('heartbeat', this.online) | ||
|
||
if (type === 'message') { | ||
console.log(data) | ||
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(ERROR_EVENT, (error: any) => { | ||
this.emit('error', error) | ||
}) | ||
|
||
this.on(CLOSE_EVENT, () => { | ||
this.emit('close') | ||
}) | ||
} | ||
|
||
heartbeat() { | ||
this._socket.write(serialize(LiveProtocolOperation.HEARTBEAT, {})) | ||
} | ||
|
||
getOnline() { | ||
this.heartbeat() | ||
// return new Promise<number>(resolve => this.once('heartbeat', resolve)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { CLOSE_EVENT, ERROR_EVENT, Live, MESSAGE_EVENT, OPEN_EVENT } from './base' | ||
import type { ISocket } from './types' | ||
import { inflates } from './inflate/browser' | ||
|
||
export class KeepLiveTCP extends Live { | ||
private socket: WebSocket | ||
i = 0 | ||
|
||
constructor(roomId: number) { | ||
const socket = new WebSocket('wss://broadcastlv.chat.bilibili.com:2245/sub') | ||
socket.binaryType = 'arraybuffer' | ||
const _socket: ISocket = { | ||
write(buffer) { | ||
socket.send(new Uint8Array(buffer)) | ||
}, | ||
end() { | ||
socket.close() | ||
}, | ||
} | ||
|
||
socket.addEventListener('open', () => this.emit(OPEN_EVENT)) | ||
socket.addEventListener('close', () => this.emit(CLOSE_EVENT)) | ||
socket.addEventListener('error', (...params: any[]) => this.emit(ERROR_EVENT, ...params)) | ||
socket.addEventListener('message', (e: MessageEvent<ArrayBuffer>) => { | ||
this.emit(MESSAGE_EVENT, new Uint8Array(e.data)) | ||
}) | ||
|
||
super(roomId, _socket, inflates as any) | ||
|
||
this.socket = socket | ||
} | ||
} | ||
|
Oops, something went wrong.