diff --git a/README.md b/README.md index bc90b70..4afd09f 100644 --- a/README.md +++ b/README.md @@ -43,9 +43,9 @@ live.on('live', () => { (Keep)LiveWS 和 (Keep)LiveTCP 的大部分API基本上一样, 区别在本文末尾有注明 -### new LiveWS(roomid [, { address }]) +### new LiveWS(roomid [, { address, protover }]) -### new LiveTCP(roomid [, { host, port }]) +### new LiveTCP(roomid [, { host, port, protover }]) - `roomid` 房间号 @@ -53,15 +53,19 @@ live.on('live', () => { - `address` 可选, WebSocket连接的地址 - 默认 `wss://broadcastlv.chat.bilibili.com/sub` + 默认 `'wss://broadcastlv.chat.bilibili.com/sub'` - `host` 可选, TCP连接的地址 - 默认 `broadcastlv.chat.bilibili.com` + 默认 `'broadcastlv.chat.bilibili.com'` - `port` 可选, TCP连接的端口 默认 `2243` + +- `protover` 可选, 见 #17 + + 默认 `2` #### live.on('open') @@ -192,9 +196,9 @@ WebSocket/TCP收到的Raw Buffer(不推荐直接使用) KeepLiveWS 和 KeepLiveTCP 有断线重新连接的功能 -#### new KeepLiveWS(roomid [, { address }]) +#### new KeepLiveWS(roomid [, { address, protover }]) -#### new KeepLiveTCP(roomid [, { host, port }]) +#### new KeepLiveTCP(roomid [, { host, port, protover }]) 所有上方的API都是一样的, 只不过会有以下微小的区别 diff --git a/src/index.ts b/src/index.ts index 67b30ed..42357a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,7 +44,7 @@ class Live extends NiceEventEmitter { send: (data: Buffer) => void close: () => void - constructor(roomid: number, { send, close }: { send: (data: Buffer) => void, close: () => void }) { + constructor(roomid: number, { send, close, protover }: { send: (data: Buffer) => void, close: () => void, protover: 1 | 2 }) { if (typeof roomid !== 'number' || Number.isNaN(roomid)) { throw new Error(`roomid ${roomid} must be Number not NaN`) } @@ -88,7 +88,7 @@ class Live extends NiceEventEmitter { }) this.on('open', () => { - const buf = encoder('join', { uid: 0, roomid, protover: 2, platform: 'web', clientver: '1.8.5', type: 2 }) + const buf = encoder('join', { uid: 0, roomid, protover, platform: 'web', clientver: '1.8.5', type: 2 }) this.send(buf) }) @@ -115,7 +115,7 @@ class Live extends NiceEventEmitter { export class LiveWS extends Live { ws: WebSocket - constructor(roomid: number, { address = 'wss://broadcastlv.chat.bilibili.com/sub' } = {}) { + constructor(roomid: number, { address = 'wss://broadcastlv.chat.bilibili.com/sub', protover = 2 }: { address?: string, protover?: 1 | 2 } = {}) { const ws = new WebSocket(address) const send = (data: Buffer) => { if (ws.readyState === 1) { @@ -124,7 +124,7 @@ export class LiveWS extends Live { } const close = () => this.ws.close() - super(roomid, { send, close }) + super(roomid, { send, close, protover }) ws.on('open', (...params) => this.emit('open', ...params)) ws.on('message', data => this.emit('message', data as Buffer)) @@ -139,14 +139,14 @@ export class LiveTCP extends Live { socket: Socket buffer: Buffer - constructor(roomid: number, { host = 'broadcastlv.chat.bilibili.com', port = 2243 } = {}) { + constructor(roomid: number, { host = 'broadcastlv.chat.bilibili.com', port = 2243, protover = 2 }: { host?: string, port?: number, protover?: 1 | 2 } = {}) { const socket = net.connect(port, host) const send = (data: Buffer) => { socket.write(data) } const close = () => this.socket.end() - super(roomid, { send, close }) + super(roomid, { send, close, protover }) this.buffer = Buffer.alloc(0) diff --git a/test/test.ts b/test/test.ts index 0ffecd7..0842279 100644 --- a/test/test.ts +++ b/test/test.ts @@ -4,7 +4,6 @@ import { assert } from 'chai' import { LiveWS, LiveTCP, KeepLiveWS, KeepLiveTCP } from '..' - Object.entries({ LiveWS, LiveTCP, KeepLiveWS, KeepLiveTCP }) .forEach(([name, Live]) => { describe(name, function() { @@ -82,6 +81,12 @@ Object.entries({ LiveWS, LiveTCP, KeepLiveWS, KeepLiveTCP }) } }) context('options', function() { + it('protover: 1', async function() { + const live = new Live(12235923, { protover: 1 }) + const [online] = await once(live, 'heartbeat') + live.close() + return assert.isAbove(online, 0) + }) if (name.includes('WS')) { it('address', async function() { const live = new Live(12235923, { address: 'wss://broadcastlv.chat.bilibili.com:2245/sub' })