Skip to content

Commit

Permalink
extra options
Browse files Browse the repository at this point in the history
  • Loading branch information
simon300000 committed Jul 3, 2023
1 parent 1c70a53 commit b8bc1e2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ live.on('live', () => {

(Keep)LiveWS 和 (Keep)LiveTCP 的大部分API基本上一样, 区别在本文末尾有注明

### new LiveWS(roomid [, { address, protover, key, authBody }])
### new LiveWS(roomid [, { address, protover, key, authBody, uid, buvid }])

### new LiveTCP(roomid [, { host, port, protover, key, authBody }])
### new LiveTCP(roomid [, { host, port, protover, key, authBody, uid, buvid }])

- `roomid` 房间号

Expand All @@ -73,13 +73,17 @@ live.on('live', () => {

默认 `2`

* 1 (见 #17)
* 1 (见 [#17](https://github.com/simon300000/bilibili-live-ws/issues/17))
* 2 (zlib.inflate)
* 3 (brotliDecompress)

- `key` 可选, WS握手的Token
- `uid` 可选, WS握手的 uid [#397](https://github.com/simon300000/bilibili-live-ws/issues/397)

- `authBody` 可选, 可以和 <https://open-live.bilibili.com/document/> 配合使用, 会覆盖掉 `protover` `roomid` `key`
- `key` 可选, WS握手的 Token [#397](https://github.com/simon300000/bilibili-live-ws/issues/397)

- `buvid` 可选, WS握手的 Token [#397](https://github.com/simon300000/bilibili-live-ws/issues/397)

- `authBody` 可选, 可以和 <https://open-live.bilibili.com/document/> 配合使用, 会覆盖掉 `protover` `roomid` `key` `uid` `buvid`

#### live.on('open')

Expand Down
4 changes: 3 additions & 1 deletion src/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export const makeDecoder = ({ inflateAsync, brotliDecompressAsync }: Inflates) =
return decoder
}

export const encoder = (type: string, { Buffer }: Inflates, body: any = '') => {
type EncodeType = 'heartbeat' | 'join'

export const encoder = (type: EncodeType, { Buffer }: Inflates, body: any = '') => {
const blank = Buffer.alloc(16)
if (typeof body !== 'string') {
body = JSON.stringify(body)
Expand Down
12 changes: 9 additions & 3 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EventEmitter } from 'events'

import { encoder, makeDecoder, Inflates } from './buffer'

export type LiveOptions = { protover?: 1 | 2 | 3, key?: string, authBody?: any }
export type LiveOptions = { protover?: 1 | 2 | 3, key?: string, authBody?: any, uid?: number, buvid?: string }

export const relayEvent = Symbol('relay')

Expand All @@ -26,7 +26,7 @@ export class Live extends NiceEventEmitter {
send: (data: Buffer) => void
close: () => void

constructor(inflates: Inflates, roomid: number, { send, close, protover = 2, key, authBody }: { send: (data: Buffer) => void, close: () => void } & LiveOptions) {
constructor(inflates: Inflates, roomid: number, { send, close, protover = 2, key, authBody, uid = 0, buvid }: { send: (data: Buffer) => void, close: () => void } & LiveOptions) {
if (typeof roomid !== 'number' || Number.isNaN(roomid)) {
throw new Error(`roomid ${roomid} must be Number not NaN`)
}
Expand Down Expand Up @@ -75,12 +75,18 @@ export class Live extends NiceEventEmitter {

this.on('open', () => {
if (authBody) {
if (typeof authBody === 'object') {
authBody = encoder('join', inflates, 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 }
const hi: { uid: number, roomid: number, protover: number, platform: string, type: number, key?: string, buvid?: string } = { uid: uid, roomid, protover, platform: 'web', type: 2 }
if (key) {
hi.key = key
}
if (buvid) {
hi.buvid = buvid
}
const buf = encoder('join', inflates, hi)
this.send(buf)
}
Expand Down

0 comments on commit b8bc1e2

Please sign in to comment.