Skip to content

Commit

Permalink
protover可选
Browse files Browse the repository at this point in the history
close #17
  • Loading branch information
simon300000 committed Feb 15, 2020
1 parent cf5fc8c commit 42093ba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,29 @@ 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` 房间号

比如 https://live.bilibili.com/14327465 中的 `14327465`

- `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')

Expand Down Expand Up @@ -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都是一样的, 只不过会有以下微小的区别

Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
}
Expand Down Expand Up @@ -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)
})

Expand All @@ -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) {
Expand All @@ -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))
Expand All @@ -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)

Expand Down
7 changes: 6 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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' })
Expand Down

0 comments on commit 42093ba

Please sign in to comment.