Skip to content

Commit f47fc53

Browse files
committed
feat: support custom ws
1 parent 3191ad6 commit f47fc53

File tree

13 files changed

+5318
-3685
lines changed

13 files changed

+5318
-3685
lines changed

Diff for: .vscode/settings.json

+48-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,57 @@
11
{
2-
"editor.codeActionsOnSave": {
3-
"source.fixAll.eslint": "explicit"
4-
},
52
"cSpell.words": [
63
"Bilibili",
74
"vitepress"
85
],
96
"yaml.schemas": {
107
"https://json.schemastore.org/github-workflow.json": "/.github/workflows/deploy.yml"
118
},
12-
"eslint.experimental.useFlatConfig": true
9+
10+
// Disable the default formatter, use eslint instead
11+
"prettier.enable": false,
12+
"editor.formatOnSave": false,
13+
14+
// Auto fix
15+
"editor.codeActionsOnSave": {
16+
"source.fixAll.eslint": "explicit",
17+
"source.organizeImports": "never"
18+
},
19+
20+
// Silent the stylistic rules in you IDE, but still auto fix them
21+
"eslint.rules.customizations": [
22+
{ "rule": "style/*", "severity": "off" },
23+
{ "rule": "format/*", "severity": "off" },
24+
{ "rule": "*-indent", "severity": "off" },
25+
{ "rule": "*-spacing", "severity": "off" },
26+
{ "rule": "*-spaces", "severity": "off" },
27+
{ "rule": "*-order", "severity": "off" },
28+
{ "rule": "*-dangle", "severity": "off" },
29+
{ "rule": "*-newline", "severity": "off" },
30+
{ "rule": "*quotes", "severity": "off" },
31+
{ "rule": "*semi", "severity": "off" }
32+
],
33+
34+
// Enable eslint for all supported languages
35+
"eslint.validate": [
36+
"javascript",
37+
"javascriptreact",
38+
"typescript",
39+
"typescriptreact",
40+
"vue",
41+
"html",
42+
"markdown",
43+
"json",
44+
"jsonc",
45+
"yaml",
46+
"toml",
47+
"xml",
48+
"gql",
49+
"graphql",
50+
"astro",
51+
"css",
52+
"less",
53+
"scss",
54+
"pcss",
55+
"postcss"
56+
]
1357
}

Diff for: package.json

+13-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "tiny-bilibili-ws",
33
"type": "module",
44
"version": "0.1.10",
5-
"packageManager": "pnpm@8.14.0",
5+
"packageManager": "pnpm@9.4.0",
66
"description": "Bilibili Live WebSocket/TCP API",
77
"author": "starknt",
88
"license": "MIT",
@@ -39,6 +39,7 @@
3939
"dist"
4040
],
4141
"scripts": {
42+
"stub": "unbuild --stub",
4243
"build": "unbuild",
4344
"prepublishOnly": "pnpm run build",
4445
"release": "pnpm publish",
@@ -56,23 +57,23 @@
5657
},
5758
"dependencies": {
5859
"pako": "^2.1.0",
59-
"ws": "^8.16.0"
60+
"ws": "^8.18.0"
6061
},
6162
"devDependencies": {
62-
"@antfu/eslint-config": "^2.6.1",
63-
"@types/node": "^20.10.6",
63+
"@antfu/eslint-config": "^2.21.3",
64+
"@types/node": "^20.14.10",
6465
"@types/pako": "^2.0.3",
6566
"@types/ws": "^8.5.10",
6667
"brotli-size": "^4.0.0",
67-
"bumpp": "^9.2.1",
68-
"dotenv": "^16.3.1",
69-
"eslint": "^8.56.0",
68+
"bumpp": "^9.4.1",
69+
"dotenv": "^16.4.5",
70+
"eslint": "^9.6.0",
7071
"eventemitter3": "5.0.1",
71-
"pnpm": "^8.14.0",
72-
"terser": "^5.26.0",
73-
"tsx": "^4.7.0",
74-
"typescript": "^5.3.3",
72+
"pnpm": "^9.4.0",
73+
"terser": "^5.31.1",
74+
"tsx": "^4.16.2",
75+
"typescript": "^5.5.3",
7576
"unbuild": "^2.0.0",
76-
"vitest": "^1.1.3"
77+
"vitest": "^1.6.0"
7778
}
7879
}

Diff for: playground/node/test.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import path from 'node:path'
2+
import process from 'node:process'
23
import * as dotenv from 'dotenv'
3-
import { KeepLiveTCP, getLongRoomId } from 'tiny-bilibili-ws'
4+
import { KeepLiveWS } from 'tiny-bilibili-ws'
45

56
dotenv.config({ path: path.resolve(process.cwd(), '.env.local') })
67

7-
const res = await getLongRoomId(process.env.VITE_ROOM as any)
8-
9-
const live = new KeepLiveTCP(res.data.room_id)
8+
const live = new KeepLiveWS(process.env.VITE_ROOM as any, {
9+
// uid: 32140469,
10+
// buvid: '1517DD0A-0A72-7039-2EE2-DC76CFC2408709872infoc',
11+
// key: 'zfYifs1uI1RrPpyGY07Zdlbcd2Y_riAWqn-ISq4oI9phE1Vg8ZqmqBQQyGBpcER6DU5vfQqgqKISu0SP_P3zS0gmrqDM7EDMfGR44PlnaSlJoIB3-hu0kx_P_XTaJLLDM3YQ91XthTADaY7GnQLgpfvN5tFJWATFCLZB_YjKQX_O8TrGHlyUDYg=',
12+
})
1013

1114
live.on('open', () => console.log('连接成功'))
12-
1315
live.on('close', () => console.log('断开连接'))
1416

1517
live.on('SEND_GIFT', (message) => { // 有礼物, 但不会被触发

Diff for: playground/src/App.vue

+16-18
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,30 @@ import { onMounted, ref } from 'vue'
44
55
const danmu = ref<any[]>([])
66
7-
onMounted(() => {
8-
fetch(`https://api.live.bilibili.com/room/v1/Room/mobileRoomInit?id=${import.meta.env.VITE_ROOM}`)
9-
.then(w => w.json())
10-
.then((data) => {
11-
console.log(data)
12-
})
7+
function randomElement<T>(list: T[]): T {
8+
return list[Math.floor(Math.random() * list.length)]
9+
}
1310
14-
const live = new KeepLiveWS(import.meta.env.VITE_ROOM)
11+
onMounted(async () => {
12+
const room = await fetch(`/api/room/v1/Room/mobileRoomInit?id=${import.meta.env.VITE_ROOM}`)
13+
.then(res => res.json())
14+
const conf = await fetch(`/api/room/v1/Danmu/getConf?room_id=${room.data.room_id}&platform=pc&player=web`)
15+
.then(res => res.json())
1516
16-
live.runWhenConnected(() => {
17-
console.log(`正在监听 ${import.meta.env.VITE_ROOM}`)
18-
})
17+
const host = randomElement(conf.data.host_server_list)
1918
20-
live.on('WELCOME_GUARD', (message) => { // 有弹幕会被触发
21-
console.log(message)
19+
const live = new KeepLiveWS(room.data.room_id, {
20+
url: `wss://${host.host}:${host.wss_port}/sub`,
21+
key: conf.data.token,
2222
})
2323
24-
live.on('WELCOME', (message) => { // 有弹幕会被触发
25-
console.log(message)
24+
live.runWhenConnected(() => {
25+
console.log(`正在监听 ${import.meta.env.VITE_ROOM}`)
2626
})
2727
28-
live.on('SUPER_CHAT_MESSAGE', (message) => { // 有弹幕会被触发
29-
console.log(message)
30-
})
28+
live.on('DANMU_MSG', async (m) => {
29+
console.log('Online: ', await live.getOnline())
3130
32-
live.on('DANMU_MSG', (m) => {
3331
danmu.value.push(m)
3432
})
3533
})

Diff for: playground/vite.config.ts

+9
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,13 @@ import vue from '@vitejs/plugin-vue'
44
// https://vitejs.dev/config/
55
export default defineConfig({
66
plugins: [vue()],
7+
server: {
8+
proxy: {
9+
'/api': {
10+
target: 'https://api.live.bilibili.com',
11+
changeOrigin: true,
12+
rewrite: path => path.replace(/^\/api/, ''),
13+
},
14+
},
15+
},
716
})

0 commit comments

Comments
 (0)