Skip to content

Commit

Permalink
docs: update
Browse files Browse the repository at this point in the history
  • Loading branch information
starknt committed Aug 31, 2024
1 parent 5a51cda commit 832fde2
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 47 deletions.
65 changes: 49 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,73 @@ pnpm install tiny-bilibili-ws

## 简单使用

### 在 Nodejs 中使用

```typescript
// In Nodejs environment
import { KeepLiveTCP, getLongRoomId, toMessageData } from 'tiny-bilibili-ws'
import { KeepLiveWS, toMessageData } from 'tiny-bilibili-ws'

const room = 652581
const live = new KeepLiveWS(room, {
// 触发风控后,你会无法获取弹幕发送者的用户名,详情可见:https://github.com/ddiu8081/blive-message-listener/issues/29, 传入以下参数即可解除
headers: {
Cookie: 'xxx', // 从 Bilibili 网页版获取
},
uid: 0, // 你的 Cookie 对应的 UID
})

live.runWhenConnected(() => {
console.log(`正在监听 ${room}`) // 连接成功后才会触发
})

live.on('heartbeat', (online) => {
console.log('人气值: ', online)
})

live.on('DANMU_MSG', (danmu) => {
console.log(toMessageData(danmu))
})

live.on('error', (e) => {
console.error('错误: ', e)
})

live.on('close', () => {
console.log(`退出监听 ${room}`)
})
```

// or browser environment
### 在浏览器中使用

```typescript
import { KeepLiveWS } from 'tiny-bilibili-ws/browser'

const { data } = await getLongRoomId(652581)
const room = data.room_id
const tcp = new KeepLiveTCP(room)
// 因为存在跨域问题, Bilibili API 不能在浏览器中访问,现在需要手动传入 key 和 url
// 例子可以参考 https://github.com/starknt/tiny-bilibili-ws/blob/master/playground/src/App.vue#L13
const room = 652581
const live = new KeepLiveWS(room, {
url: 'xxx',
key: 'xxx',
})

tcp.runWhenConnected(() => {
console.log(`正在监听 ${room}`)
live.runWhenConnected(() => {
console.log(`正在监听 ${room}`) // 连接成功后才会触发
})

tcp.on('heartbeat', (online) => {
live.on('heartbeat', (online) => {
console.log('人气值: ', online)
})

tcp.on('DANMU_MSG', (danmu) => {
live.on('DANMU_MSG', (danmu) => {
console.log(toMessageData(danmu))
})

tcp.on('error', (e) => {
live.on('error', (e) => {
console.error('错误: ', e)
})

tcp.on('close', () => {
live.on('close', () => {
console.log(`退出监听 ${room}`)
})

// 因为存在跨域问题, getLongRoomId 这个 API 不能在浏览器中运行

new KeepLiveWS(652581) // Now Long Room id: 4350043
```

## Credits
Expand Down
70 changes: 39 additions & 31 deletions docs/guide/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Guide
description: Getting started with Tiny-Bilibili-WS
title: 食用指南
description: Tiny-Bilibili-WS 的开始教程
---

## 安装
Expand All @@ -14,29 +14,34 @@ description: Getting started with Tiny-Bilibili-WS
### 在 Nodejs 中使用

```typescript
import { KeepLiveTCP, toMessageData, getLongRoomId } from 'tiny-bilibili-ws'
import { KeepLiveWS, toMessageData } from 'tiny-bilibili-ws'

const { data } = await getLongRoomId(652581)
const room = data.room_id
const tcp = new KeepLiveTCP(room)
const room = 652581
const live = new KeepLiveWS(room, {
// 触发风控后,你会无法获取弹幕发送者的用户名,详情可见:https://github.com/ddiu8081/blive-message-listener/issues/29, 传入以下参数即可解除
headers: {
Cookie: 'xxx', // 从 Bilibili 网页版获取
},
uid: 0, // 你的 Cookie 对应的 UID
})

tcp.runWhenConnected(() => {
console.log(`正在监听 ${room}`)
live.runWhenConnected(() => {
console.log(`正在监听 ${room}`) // 连接成功后才会触发
})

tcp.on('heartbeat', (online) => {
live.on('heartbeat', (online) => {
console.log('人气值: ', online)
})

tcp.on('DANMU_MSG', (danmu) => {
live.on('DANMU_MSG', (danmu) => {
console.log(toMessageData(danmu))
})

tcp.on('error', (e) => {
live.on('error', (e) => {
console.error('错误: ', e)
})

tcp.on('close', () => {
live.on('close', () => {
console.log(`退出监听 ${room}`)
})
```
Expand All @@ -46,60 +51,63 @@ tcp.on('close', () => {
```typescript
import { KeepLiveWS } from 'tiny-bilibili-ws/browser'

// getLongRoomId API 在浏览器无法使用 #2

// 因为存在跨域问题, Bilibili API 不能在浏览器中访问,现在需要手动传入 key 和 url
// 例子可以参考 https://github.com/starknt/tiny-bilibili-ws/blob/master/playground/src/App.vue#L13
const room = 652581
const ws = new KeepLiveWS(room)
const live = new KeepLiveWS(room, {
url: 'xxx',
key: 'xxx',
})

ws.runWhenConnected(() => {
console.log(`正在监听 ${room}`)
live.runWhenConnected(() => {
console.log(`正在监听 ${room}`) // 连接成功后才会触发
})

ws.on('heartbeat', (online) => {
live.on('heartbeat', (online) => {
console.log('人气值: ', online)
})

ws.on('DANMU_MSG', (danmu) => {
live.on('DANMU_MSG', (danmu) => {
console.log(toMessageData(danmu))
})

ws.on('error', (e) => {
live.on('error', (e) => {
console.error('错误: ', e)
})

ws.on('close', () => {
live.on('close', () => {
console.log(`退出监听 ${room}`)
})
```

## 高级用法

```typescript
import { KeepLiveTCP, KeepLiveWS, getLongRoomId, serialize, deserialize, WS_OP, WS_BODY_PROTOCOL_VERSION, Message } from "tiny-bilibili-ws"
import { KeepLiveTCP, KeepLiveWS, Message, WS_BODY_PROTOCOL_VERSION, WS_OP, deserialize, serialize } from 'tiny-bilibili-ws'

// 扩展内置的 BILIBILI CMD 类型

const res = await getLongRoomId(652581)
const room = 652581

const live = new KeepLiveTCP<{
A_BILIBILI_CMD: Uint8Array
B_BILIBILI_CMD: void
C_BILIBILI_CMD: [Message<any>, number] /* 当前最多支持十个参数 */
}>(res.data.room_id)
A_BILIBILI_CMD: Uint8Array
B_BILIBILI_CMD: void
C_BILIBILI_CMD: [Message<any>, number] /* 当前最多支持十个参数 */
}>(room)

live.on('A_BILIBILI_CMD', (arg1 /* Uint8Array */) => {
console.log(arg1) // Uint8Array[]
console.log(arg1) // Uint8Array[]
})

live.on('B_BILIBILI_CMD', () => {
console.log('B_BILIBILI_CMD') // B_BILIBILI_CMD
console.log('B_BILIBILI_CMD') // B_BILIBILI_CMD
})

live.on('C_BILIBILI_CMD', (arg1 /* Message<any> */, arg2 /* number */) => {
console.log(arg1, arg2) // Message 2
console.log(arg1, arg2) // Message 2
})

live.emit('A_BILIBILI_CMD', serialize(WS_OP.MESSAGE, "test"))
live.emit('A_BILIBILI_CMD', serialize(WS_OP.MESSAGE, 'test'))
live.emit('B_BILIBILI_CMD')
live.emit('C_BILIBILI_CMD', { meta: { op: WS_OP.MESSAGE, ver: WS_BODY_PROTOCOL_VERSION.NORMAL, packetLength: 17, headerLength: 16, sequence: 1 }, data: 1 }, 2)
```

0 comments on commit 832fde2

Please sign in to comment.