Skip to content

Commit c7ac0f7

Browse files
committed
feat: connect options (#28)
1 parent 9976d78 commit c7ac0f7

File tree

7 files changed

+60
-23
lines changed

7 files changed

+60
-23
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ instance.close()
4141

4242
> 需传入房间的长id。短id需转为长id才能接收到消息。(https://github.com/ddiu8081/blive-message-listener/issues/19)
4343
44+
## Options
45+
46+
你可以向 `startListen` 传入第三个参数 `options` 来手动配置连接选项。
47+
48+
```ts
49+
interface MessageListenerOptions {
50+
/**
51+
* tiny-bilibili-ws 连接选项
52+
*
53+
* @see https://github.com/starknt/tiny-bilibili-ws
54+
*/
55+
ws?: WSOptions | TCPOptions
56+
}
57+
```
58+
4459
## Handlers & Type Definitions
4560

4661
### Common

example/browser/src.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ const handler: MsgHandler = {
1313
logDom!.innerHTML += `${JSON.stringify(msg.body)}<br>`
1414
},
1515
raw: {
16-
'INTERACT_WORD': (msg) => {
16+
INTERACT_WORD: (msg) => {
1717
console.log(msg)
1818
logDom!.innerHTML += `${JSON.stringify(msg)}<br>`
1919
},
20-
}
20+
},
2121
}
2222

2323
startListen(652581, handler)

example/node/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const handler: MsgHandler = {
88
console.log(msg)
99
},
1010
raw: {
11-
'INTERACT_WORD': (msg) => {
11+
INTERACT_WORD: (msg) => {
1212
console.log(msg)
1313
},
14-
}
14+
},
1515
}
1616

1717
startListen(652581, handler)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
],
4141
"sideEffects": false,
4242
"dependencies": {
43-
"tiny-bilibili-ws": "^0.1.8"
43+
"tiny-bilibili-ws": "^0.1.9"
4444
},
4545
"devDependencies": {
4646
"tsup": "^6.7.0",
47-
"typescript": "^5.1.3"
47+
"typescript": "^5.1.6"
4848
}
4949
}

pnpm-lock.yaml

Lines changed: 15 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/browser.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { KeepLiveWS } from 'tiny-bilibili-ws/browser'
2+
import { listenAll, type MsgHandler } from './listener'
23
import type { MessageListener } from '.'
4+
import type { WSOptions } from 'tiny-bilibili-ws'
35

4-
import { listenAll, type MsgHandler } from './listener'
6+
interface MessageListenerWSOptions {
7+
/**
8+
* tiny-bilibili-ws 连接选项
9+
*
10+
* @see https://github.com/starknt/tiny-bilibili-ws
11+
*/
12+
ws?: WSOptions
13+
}
514

6-
export const startListen = (roomId: number, handler: MsgHandler) => {
7-
const live = new KeepLiveWS(roomId)
15+
export const startListen = (roomId: number, handler: MsgHandler, options?: MessageListenerWSOptions) => {
16+
const live = new KeepLiveWS(roomId, options?.ws)
817

918
listenAll(live, roomId, handler)
1019

src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { KeepLiveTCP } from 'tiny-bilibili-ws'
2-
32
import { listenAll, type MsgHandler } from './listener'
3+
import type { TCPOptions } from 'tiny-bilibili-ws'
44

55
export interface MessageListener {
66
/** 直播间房间号 */
@@ -11,8 +11,17 @@ export interface MessageListener {
1111
getAttention: () => Promise<number>
1212
}
1313

14-
export const startListen = (roomId: number, handler: MsgHandler) => {
15-
const live = new KeepLiveTCP(roomId)
14+
interface MessageListenerTCPOptions {
15+
/**
16+
* tiny-bilibili-ws 连接选项
17+
*
18+
* @see https://github.com/starknt/tiny-bilibili-ws
19+
*/
20+
ws?: TCPOptions
21+
}
22+
23+
export const startListen = (roomId: number, handler: MsgHandler, options?: MessageListenerTCPOptions) => {
24+
const live = new KeepLiveTCP(roomId, options?.ws)
1625

1726
listenAll(live, roomId, handler)
1827

0 commit comments

Comments
 (0)