@laplace.live/ws 6.3.5 → 6.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,294 +1,103 @@
1
- # bilibili-live-ws [![npm](https://img.shields.io/npm/v/bilibili-live-ws.svg)](https://www.npmjs.com/package/bilibili-live-ws) ![Node CI](https://github.com/simon300000/bilibili-live-ws/workflows/Node%20CI/badge.svg)
1
+ # @laplace.live/ws
2
2
 
3
- Bilibili 直播 WebSocket/TCP API
3
+ Bilibili Live WebSocket/TCP API. Browser support via `@laplace.live/ws/browser` (experimental).
4
4
 
5
- LiveWS/KeepLiveWS 支持浏览器 *(实验性)*
5
+ Based on [bilibili-live-ws](https://github.com/simon300000/bilibili-live-ws).
6
6
 
7
- 应该支持 bilibili直播开放平台 <https://open-live.bilibili.com>
7
+ ## Install
8
8
 
9
- 注:如果在浏览器环境遇到问题,可以尝试手动指定引入 `bilibili-live-ws/browser`
10
-
11
- ## API
12
-
13
- ```javascript
14
- const { LiveWS, LiveTCP, KeepLiveWS, KeepLiveTCP } = require('bilibili-live-ws')
15
- const live = new LiveWS(roomid)
16
- // 或
17
- const live = new LiveTCP(roomid)
9
+ ```bash
10
+ npm install @laplace.live/ws
18
11
  ```
19
12
 
20
- 举个栗子:
13
+ ## Usage
21
14
 
22
15
  ```javascript
23
- const live = new LiveWS(14275133)
16
+ import { LiveWS, LiveTCP, KeepLiveWS, KeepLiveTCP } from "@laplace.live/ws";
24
17
 
25
- live.on('open', () => console.log('Connection is established'))
26
- // Connection is established
27
- live.on('live', () => {
28
- live.on('heartbeat', console.log)
29
- // 74185
30
- })
31
- ```
18
+ const live = new LiveWS(14275133);
32
19
 
33
- 或者用TCP (新功能):
34
-
35
- ```javascript
36
- const live = new LiveTCP(26283)
37
-
38
- live.on('open', () => console.log('Connection is established'))
39
- // Connection is established
40
- live.on('live', () => {
41
- live.on('heartbeat', console.log)
42
- // 13928
43
- })
20
+ live.on("open", () => console.log("Connection established"));
21
+ live.on("live", () => {
22
+ live.on("heartbeat", console.log);
23
+ });
44
24
  ```
45
25
 
46
- > 晚上做梦梦到一个白胡子的老爷爷和我说TCP更节约内存哦!
47
-
48
- ## Class: LiveWS / LiveTCP / KeepLiveWS / KeepLiveTCP
49
-
50
- (Keep)LiveWS 和 (Keep)LiveTCP 的大部分API基本上一样, 区别在本文末尾有注明
51
-
52
- ### new LiveWS(roomid [, { address, protover, key, authBody, uid, buvid }])
53
-
54
- ### new LiveTCP(roomid [, { host, port, protover, key, authBody, uid, buvid }])
55
-
56
- - `roomid` 房间号
57
-
58
- 比如 https://live.bilibili.com/14327465 中的 `14327465`
59
-
60
- - `address` 可选, WebSocket连接的地址
61
-
62
- 默认 `'wss://broadcastlv.chat.bilibili.com/sub'`
63
-
64
- - `host` 可选, TCP连接的地址
65
-
66
- 默认 `'broadcastlv.chat.bilibili.com'`
67
-
68
- - `port` 可选, TCP连接的端口
69
-
70
- 默认 `2243`
71
-
72
- - `protover` 可选 (1, 2, 3)
73
-
74
- 默认 `2`
75
-
76
- * 1 (见 [#17](https://github.com/simon300000/bilibili-live-ws/issues/17))
77
- * 2 (zlib.inflate)
78
- * 3 (brotliDecompress)
79
-
80
- - `uid` 可选, WS握手的 uid [#397](https://github.com/simon300000/bilibili-live-ws/issues/397)
81
-
82
- - `key` 可选, WS握手的 Token [#397](https://github.com/simon300000/bilibili-live-ws/issues/397)
83
-
84
- - `buvid` 可选, WS握手的 Token [#397](https://github.com/simon300000/bilibili-live-ws/issues/397)
85
-
86
- - `authBody` 可选, 可以和 <https://open-live.bilibili.com/document/> 配合使用, 会覆盖掉 `protover` `roomid` `key` `uid` `buvid`. 如果是 `object` 会按照握手包编码,如果是 `string`/`Buffer` 会直接发送
87
-
88
- #### live.on('open')
89
-
90
- WebSocket连接上了
91
-
92
- #### live.on('live')
93
-
94
- 成功登入房间
95
-
96
- #### live.on('heartbeat', (online) => ...)
97
-
98
- 收到服务器心跳包,会在30秒之后自动发送心跳包。
99
-
100
- - `online` 当前人气值
101
-
102
- #### live.on('msg', (data) => ...)
103
-
104
- - `data` 收到信息/弹幕/广播等
105
-
106
- data的例子: (我simon3000送了一个辣条)
107
-
108
- ```javascript
109
- {
110
- cmd: 'SEND_GIFT',
111
- data: {
112
- giftName: '辣条',
113
- num: 1,
114
- uname: 'simon3000',
115
- face: 'http://i1.hdslb.com/bfs/face/c26b9f670b10599ad105e2a7fea4b5f21c0f0bcf.jpg',
116
- guard_level: 0,
117
- rcost: 2318827,
118
- uid: 3499295,
119
- top_list: [],
120
- timestamp: 1555690631,
121
- giftId: 1,
122
- giftType: 0,
123
- action: '喂食',
124
- super: 0,
125
- super_gift_num: 0,
126
- price: 100,
127
- rnd: '1555690616',
128
- newMedal: 0,
129
- newTitle: 0,
130
- medal: [],
131
- title: '',
132
- beatId: '0',
133
- biz_source: 'live',
134
- metadata: '',
135
- remain: 6,
136
- gold: 0,
137
- silver: 0,
138
- eventScore: 0,
139
- eventNum: 0,
140
- smalltv_msg: [],
141
- specialGift: null,
142
- notice_msg: [],
143
- capsule: null,
144
- addFollow: 0,
145
- effect_block: 1,
146
- coin_type: 'silver',
147
- total_coin: 100,
148
- effect: 0,
149
- tag_image: '',
150
- user_count: 0
151
- }
152
- }
153
- ```
154
-
155
- #### live.on(cmd, (data) => ...)
156
-
157
- 用法如上,只不过只会收到特定cmd的Event。
158
-
159
- 比如 `live.on('DANMU_MSG')` 只会收到弹幕Event,一个弹幕Event的Data例子如下: (我simon3000发了个`233`)
160
-
161
- ```javascript
162
- {
163
- cmd: 'DANMU_MSG',
164
- info: [
165
- [0, 1, 25, 16777215, 1555692037, 1555690616, 0, 'c5c630b1', 0, 0, 0],
166
- '233',
167
- [3499295, 'simon3000', 0, 0, 0, 10000, 1, ''],
168
- [5, '財布', '神楽めあOfficial', 12235923, 5805790, ''],
169
- [11, 0, 6406234, '>50000'],
170
- ['title-58-1', 'title-58-1'],
171
- 0,
172
- 0,
173
- null,
174
- { ts: 1555692037, ct: '2277D56A' }
175
- ]
176
- }
177
- ```
178
-
179
-
180
-
181
- #### live.on('close')
182
-
183
- 连接关闭事件
184
-
185
- #### live.on('error', (e) => ...)
186
-
187
- 连接 error 事件,连接同时也会关闭
188
-
189
- #### live.heartbeat()
190
-
191
- 无视30秒时间,直接发送一个心跳包。
26
+ ## API
192
27
 
193
- 如果连接正常,会收到来自服务器的心跳包,也就是 `live.on('heartbeat')`,可以用于更新人气值。
28
+ ### `new LiveWS(roomid [, options])` / `new LiveTCP(roomid [, options])`
194
29
 
195
- #### live.close()
30
+ | Option | Description | Default |
31
+ | ---------- | ----------------------------------------------------------------------------------------------------------------- | ----------------------------------------- |
32
+ | `address` | WebSocket URL (LiveWS only) | `wss://broadcastlv.chat.bilibili.com/sub` |
33
+ | `host` | TCP host (LiveTCP only) | `broadcastlv.chat.bilibili.com` |
34
+ | `port` | TCP port (LiveTCP only) | `2243` |
35
+ | `protover` | Protocol version: `1`, `2` (zlib), `3` (brotli) | `2` |
36
+ | `uid` | User ID for handshake | |
37
+ | `key` | Auth token for handshake | |
38
+ | `buvid` | Browser unique ID for handshake | |
39
+ | `authBody` | Custom auth body. Overrides `protover`, `roomid`, `key`, `uid`, `buvid`. Accepts `object`, `string`, or `Buffer`. | |
196
40
 
197
- 关闭WebSocket/TCP Socket连接。
41
+ ### Events
198
42
 
199
- #### live.getOnline()
43
+ | Event | Callback | Description |
44
+ | ----------- | -------------------------- | ---------------------------------------------------------- |
45
+ | `open` | `() => void` | Connection opened |
46
+ | `live` | `() => void` | Successfully joined the room |
47
+ | `heartbeat` | `(online: number) => void` | Server heartbeat received. Auto-sends heartbeat every 30s. |
48
+ | `msg` | `(data: object) => void` | Any message (danmaku, gift, broadcast, etc.) |
49
+ | `<cmd>` | `(data: object) => void` | Specific command, e.g. `DANMU_MSG`, `SEND_GIFT` |
50
+ | `close` | `() => void` | Connection closed |
51
+ | `error` | `(error: Error) => void` | Connection error (also closes the connection) |
52
+ | `message` | `(buffer: Buffer) => void` | Raw buffer (advanced use) |
200
53
 
201
- 立即调用 `live.heartbeat()` 刷新人气数值,并且返回 Promise,resolve 人气刷新后数值
54
+ ### Methods
202
55
 
203
- #### live.on('message')
56
+ | Method | Description |
57
+ | ------------------- | ------------------------------------------------------------------- |
58
+ | `live.heartbeat()` | Send a heartbeat immediately |
59
+ | `live.close()` | Close the connection |
60
+ | `live.getOnline()` | Send heartbeat and return `Promise<number>` with current popularity |
61
+ | `live.send(buffer)` | Send raw data |
204
62
 
205
- WebSocket/TCP收到的Raw Buffer(不推荐直接使用)
63
+ ### `KeepLiveWS` / `KeepLiveTCP`
206
64
 
207
- #### live.send(buffer)
65
+ Same API as `LiveWS` / `LiveTCP` with automatic reconnection:
208
66
 
209
- 使用 WebSocket 或者 TCP 发送信息
67
+ - Reconnects automatically after disconnection (default 100ms delay, configurable via `live.interval`)
68
+ - `live.connection` exposes the underlying `LiveWS` / `LiveTCP` instance
69
+ - `live.closed` indicates whether `close()` was called manually
70
+ - `error` / `close` events do not mean the connection is permanently closed; call `live.close()` to stop reconnecting
210
71
 
211
- ### getConf(roomid)
72
+ ### `getConf(roomid)`
212
73
 
213
- 选一个cdn,Resolve host, address key, 可以直接放进(Keep)LiveWS/TCP设置
74
+ Resolve CDN host, WebSocket address, and auth key for a room.
214
75
 
215
76
  ```typescript
216
- const { getConf } = require('bilibili-live-ws')
77
+ import { getConf } from "@laplace.live/ws";
217
78
 
218
- getConf(roomid)
219
- // Return
220
- Promise<{
221
- key: string;
222
- host: string;
223
- address: string;
224
- }>
79
+ const { key, host, address } = await getConf(roomid);
225
80
  ```
226
81
 
227
- ### getRoomid(short)
82
+ ### `getRoomid(short)`
228
83
 
229
- 通过短房间号获取长房间号
84
+ Resolve a short room ID to a full room ID.
230
85
 
231
86
  ```typescript
232
- const { getRoomid } = require('bilibili-live-ws')
87
+ import { getRoomid } from "@laplace.live/ws";
233
88
 
234
- getRoomid(255)
235
- // Return Promise<number>: 48743
89
+ const roomid = await getRoomid(255); // 48743
236
90
  ```
237
91
 
238
- <hr>
239
-
240
- ### Keep和无Keep的区别
241
-
242
- KeepLiveWS 和 KeepLiveTCP 有断线重新连接的功能
243
-
244
- #### new KeepLiveWS(roomid [, { address, protover, key }])
245
-
246
- #### new KeepLiveTCP(roomid [, { host, port, protover, key }])
247
-
248
- 所有上方的API都是一样的, 只不过会有以下微小的区别
249
-
250
- * 收到error或者close事件的时候并不代表连接关闭, 必须要手动调用`live.close()`来关闭连接
251
- * 内部的连接对象(`LiveWS`/`LiveTCP`)在`live.connection`中
252
- * 内部的连接关闭了之后, 如果不是因为`live.close()`关闭, 会在100毫秒之后自动打开一个新的连接。
253
- * 这个100毫秒可以通过改变`live.interval`来设置
254
- * 内部的boolean, `live.closed`, 用于判断是否是因为`live.close()`关闭。
255
-
256
- <hr>
257
-
258
- ### LiveWS 和 LiveTCP 的区别
259
-
260
- #### LiveWS.ws
261
-
262
- LiveWS 内部 WebSocket 对象,需要时可以直接操作
263
-
264
- Doc: <https://github.com/websockets/ws/blob/master/doc/ws.md#class-websocket>
265
-
266
- #### LiveTCP.socket
267
-
268
- LiveTCP 内部 TCP Socket 对象,需要时可以直接操作
269
-
270
- Doc: <https://nodejs.org/api/net.html#net_class_net_socket>
271
-
272
- #### LiveTCP.buffer
273
-
274
- LiveTCP内部TCP流的Buffer缓冲区,有不完整的包
275
-
276
- #### LiveTCP.splitBuffer()
277
-
278
- 处理LiveTCP内部TCP流的Buffer缓冲区,把其中完整的包交给decoder处理
279
-
280
- ### BenchMark 简单对比
281
-
282
- 在连接了640个直播间后过了一分钟左右(@2.0.0)
283
-
284
- | | LiveWS (wss) | LiveWS (ws) | LiveTCP |
285
- | -------- | ------------ | ----------- | ------- |
286
- | 内存占用 | 63 MB | 26 MB | 18 MB |
287
-
288
- <hr>
92
+ ### Internals
289
93
 
290
- 参考资料: <https://github.com/lovelyyoshino/Bilibili-Live-API/blob/master/API.WebSocket.md>
94
+ | Property | Description |
95
+ | ----------------------- | --------------------------------------------------------------------------------- |
96
+ | `LiveWS.ws` | Underlying [WebSocket](https://github.com/websockets/ws) instance |
97
+ | `LiveTCP.socket` | Underlying [net.Socket](https://nodejs.org/api/net.html#class-netsocket) instance |
98
+ | `LiveTCP.buffer` | Internal TCP stream buffer |
99
+ | `LiveTCP.splitBuffer()` | Process buffered TCP data into complete packets |
291
100
 
292
- # 贡献
101
+ ## License
293
102
 
294
- 欢迎Issue和Pull Request!
103
+ MIT
package/dist/browser.d.ts CHANGED
@@ -1,19 +1,11 @@
1
- import { L as Live, I as Inflates, a as LiveOptions, K as KeepLive } from './common-BGRg5uk7.js';
2
- export { r as relayEvent } from './common-BGRg5uk7.js';
3
- import { EventEmitter } from 'events';
1
+ import { L as Live, I as Inflates, a as LiveOptions, K as KeepLive } from './common-DWwvds2J.js';
2
+ export { D as DataEvent, E as EventEvent } from './common-DWwvds2J.js';
4
3
 
5
4
  type WSOptions = LiveOptions & {
6
5
  address?: string;
7
6
  };
8
- declare class WSWrapper extends EventEmitter {
9
- ws: WebSocket;
10
- constructor(address: string, inflates: Inflates);
11
- get readyState(): number;
12
- send(data: Buffer): void;
13
- close(code?: number, reason?: string): void;
14
- }
15
7
  declare class LiveWSBase extends Live {
16
- ws: WSWrapper;
8
+ ws: WebSocket;
17
9
  constructor(inflates: Inflates, roomid: number, { address, ...options }?: WSOptions);
18
10
  }
19
11