@laplace.live/ws 7.0.0 → 7.0.2
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 +15 -82
- package/dist/browser.d.ts +37 -2
- package/dist/browser.js +439 -361
- package/dist/index.d.ts +128 -2
- package/dist/index.js +225 -124
- package/dist/ws-D63GKJdI.d.ts +250 -0
- package/package.json +1 -1
- package/dist/ws-DVInVzNN.d.ts +0 -67
package/README.md
CHANGED
|
@@ -1,103 +1,36 @@
|
|
|
1
1
|
# @laplace.live/ws
|
|
2
2
|
|
|
3
|
-
Bilibili Live WebSocket/TCP API. Browser support via `@laplace.live/ws/browser`
|
|
3
|
+
Bilibili Live WebSocket/TCP API. Browser support via `@laplace.live/ws/browser`
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
This project is based on [bilibili-live-ws](https://github.com/simon300000/bilibili-live-ws) and has the exact same API as the original project, with the following differences:
|
|
6
|
+
|
|
7
|
+
- Pure ESM
|
|
8
|
+
- Uses web-standard `EventTarget`/`Event` instead of Node.js `EventEmitter` polyfills
|
|
9
|
+
- Uses native `WebSocket` API directly, removing `isomorphic-ws` and `ws` dependencies
|
|
10
|
+
- No `Buffer` polyfills
|
|
11
|
+
- Typed `on<T>()`/`off<T>()` convenience methods
|
|
12
|
+
- Conditional `exports` field with `./server`, `./client`, `./browser` sub-path exports
|
|
6
13
|
|
|
7
14
|
## Install
|
|
8
15
|
|
|
9
16
|
```bash
|
|
10
|
-
|
|
17
|
+
bun add @laplace.live/ws
|
|
11
18
|
```
|
|
12
19
|
|
|
13
20
|
## Usage
|
|
14
21
|
|
|
15
|
-
```
|
|
22
|
+
```typescript
|
|
23
|
+
// Or specifically use @laplace.live/ws/server or @laplace.live/ws/client
|
|
16
24
|
import { LiveWS, LiveTCP, KeepLiveWS, KeepLiveTCP } from "@laplace.live/ws";
|
|
17
25
|
|
|
18
|
-
const live = new LiveWS(
|
|
26
|
+
const live = new LiveWS(25034104);
|
|
19
27
|
|
|
20
|
-
live.on("open", () => console.log("Connection established"));
|
|
28
|
+
live.on<T>("open", () => console.log("Connection established"));
|
|
21
29
|
live.on("live", () => {
|
|
22
|
-
live.on("heartbeat", console.log);
|
|
30
|
+
live.on<T>("heartbeat", console.log);
|
|
23
31
|
});
|
|
24
32
|
```
|
|
25
33
|
|
|
26
|
-
## API
|
|
27
|
-
|
|
28
|
-
### `new LiveWS(roomid [, options])` / `new LiveTCP(roomid [, options])`
|
|
29
|
-
|
|
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`. | |
|
|
40
|
-
|
|
41
|
-
### Events
|
|
42
|
-
|
|
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) |
|
|
53
|
-
|
|
54
|
-
### Methods
|
|
55
|
-
|
|
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 |
|
|
62
|
-
|
|
63
|
-
### `KeepLiveWS` / `KeepLiveTCP`
|
|
64
|
-
|
|
65
|
-
Same API as `LiveWS` / `LiveTCP` with automatic reconnection:
|
|
66
|
-
|
|
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
|
|
71
|
-
|
|
72
|
-
### `getConf(roomid)`
|
|
73
|
-
|
|
74
|
-
Resolve CDN host, WebSocket address, and auth key for a room.
|
|
75
|
-
|
|
76
|
-
```typescript
|
|
77
|
-
import { getConf } from "@laplace.live/ws";
|
|
78
|
-
|
|
79
|
-
const { key, host, address } = await getConf(roomid);
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
### `getRoomid(short)`
|
|
83
|
-
|
|
84
|
-
Resolve a short room ID to a full room ID.
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
import { getRoomid } from "@laplace.live/ws";
|
|
88
|
-
|
|
89
|
-
const roomid = await getRoomid(255); // 48743
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### Internals
|
|
93
|
-
|
|
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 |
|
|
100
|
-
|
|
101
34
|
## License
|
|
102
35
|
|
|
103
36
|
MIT
|
package/dist/browser.d.ts
CHANGED
|
@@ -1,9 +1,44 @@
|
|
|
1
|
-
import { K as KeepLive, b as LiveWSBase, W as WSOptions } from './ws-
|
|
2
|
-
export {
|
|
1
|
+
import { K as KeepLive, b as LiveWSBase, W as WSOptions } from './ws-D63GKJdI.js';
|
|
2
|
+
export { c as LaplaceRawEvent, a as LiveOptions } from './ws-D63GKJdI.js';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* WebSocket client for a Bilibili live room (browser).
|
|
6
|
+
*
|
|
7
|
+
* Uses `pako` for zlib inflate and a bundled JS decoder for brotli
|
|
8
|
+
* decompression, requiring no Node.js built-ins.
|
|
9
|
+
*
|
|
10
|
+
* @param roomid - Numeric Bilibili live room ID.
|
|
11
|
+
* @param opts - Optional WebSocket address and authentication options.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { LiveWS } from '@laplace.live/ws/browser'
|
|
16
|
+
*
|
|
17
|
+
* const live = new LiveWS(12345, { key: '...', address: 'wss://...' })
|
|
18
|
+
* live.on('DANMU_MSG', (e) => console.log(e.data))
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
4
21
|
declare class LiveWS extends LiveWSBase {
|
|
5
22
|
constructor(roomid: number, opts?: WSOptions);
|
|
6
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Auto-reconnecting WebSocket client for a Bilibili live room (browser).
|
|
26
|
+
*
|
|
27
|
+
* Wraps the browser {@link LiveWS} with automatic reconnection on
|
|
28
|
+
* disconnect or heartbeat timeout. All events from the underlying
|
|
29
|
+
* connection are forwarded to this instance.
|
|
30
|
+
*
|
|
31
|
+
* @param roomid - Numeric Bilibili live room ID.
|
|
32
|
+
* @param opts - Optional WebSocket address and authentication options.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { KeepLiveWS } from '@laplace.live/ws/browser'
|
|
37
|
+
*
|
|
38
|
+
* const keep = new KeepLiveWS(12345, { key: '...' })
|
|
39
|
+
* keep.on('DANMU_MSG', (e) => console.log(e.data))
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
7
42
|
declare class KeepLiveWS extends KeepLive<typeof LiveWSBase> {
|
|
8
43
|
constructor(roomid: number, opts?: WSOptions);
|
|
9
44
|
}
|