Skip to content

Commit

Permalink
feat: support custom ws
Browse files Browse the repository at this point in the history
  • Loading branch information
starknt committed Jul 7, 2024
1 parent 3191ad6 commit f47fc53
Show file tree
Hide file tree
Showing 13 changed files with 5,318 additions and 3,685 deletions.
52 changes: 48 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,57 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"cSpell.words": [
"Bilibili",
"vitepress"
],
"yaml.schemas": {
"https://json.schemastore.org/github-workflow.json": "/.github/workflows/deploy.yml"
},
"eslint.experimental.useFlatConfig": true

// Disable the default formatter, use eslint instead
"prettier.enable": false,
"editor.formatOnSave": false,

// Auto fix
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
"source.organizeImports": "never"
},

// Silent the stylistic rules in you IDE, but still auto fix them
"eslint.rules.customizations": [
{ "rule": "style/*", "severity": "off" },
{ "rule": "format/*", "severity": "off" },
{ "rule": "*-indent", "severity": "off" },
{ "rule": "*-spacing", "severity": "off" },
{ "rule": "*-spaces", "severity": "off" },
{ "rule": "*-order", "severity": "off" },
{ "rule": "*-dangle", "severity": "off" },
{ "rule": "*-newline", "severity": "off" },
{ "rule": "*quotes", "severity": "off" },
{ "rule": "*semi", "severity": "off" }
],

// Enable eslint for all supported languages
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact",
"vue",
"html",
"markdown",
"json",
"jsonc",
"yaml",
"toml",
"xml",
"gql",
"graphql",
"astro",
"css",
"less",
"scss",
"pcss",
"postcss"
]
}
25 changes: 13 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "tiny-bilibili-ws",
"type": "module",
"version": "0.1.10",
"packageManager": "pnpm@8.14.0",
"packageManager": "pnpm@9.4.0",
"description": "Bilibili Live WebSocket/TCP API",
"author": "starknt",
"license": "MIT",
Expand Down Expand Up @@ -39,6 +39,7 @@
"dist"
],
"scripts": {
"stub": "unbuild --stub",
"build": "unbuild",
"prepublishOnly": "pnpm run build",
"release": "pnpm publish",
Expand All @@ -56,23 +57,23 @@
},
"dependencies": {
"pako": "^2.1.0",
"ws": "^8.16.0"
"ws": "^8.18.0"
},
"devDependencies": {
"@antfu/eslint-config": "^2.6.1",
"@types/node": "^20.10.6",
"@antfu/eslint-config": "^2.21.3",
"@types/node": "^20.14.10",
"@types/pako": "^2.0.3",
"@types/ws": "^8.5.10",
"brotli-size": "^4.0.0",
"bumpp": "^9.2.1",
"dotenv": "^16.3.1",
"eslint": "^8.56.0",
"bumpp": "^9.4.1",
"dotenv": "^16.4.5",
"eslint": "^9.6.0",
"eventemitter3": "5.0.1",
"pnpm": "^8.14.0",
"terser": "^5.26.0",
"tsx": "^4.7.0",
"typescript": "^5.3.3",
"pnpm": "^9.4.0",
"terser": "^5.31.1",
"tsx": "^4.16.2",
"typescript": "^5.5.3",
"unbuild": "^2.0.0",
"vitest": "^1.1.3"
"vitest": "^1.6.0"
}
}
12 changes: 7 additions & 5 deletions playground/node/test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import path from 'node:path'
import process from 'node:process'
import * as dotenv from 'dotenv'
import { KeepLiveTCP, getLongRoomId } from 'tiny-bilibili-ws'
import { KeepLiveWS } from 'tiny-bilibili-ws'

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

const res = await getLongRoomId(process.env.VITE_ROOM as any)

const live = new KeepLiveTCP(res.data.room_id)
const live = new KeepLiveWS(process.env.VITE_ROOM as any, {
// uid: 32140469,
// buvid: '1517DD0A-0A72-7039-2EE2-DC76CFC2408709872infoc',
// key: 'zfYifs1uI1RrPpyGY07Zdlbcd2Y_riAWqn-ISq4oI9phE1Vg8ZqmqBQQyGBpcER6DU5vfQqgqKISu0SP_P3zS0gmrqDM7EDMfGR44PlnaSlJoIB3-hu0kx_P_XTaJLLDM3YQ91XthTADaY7GnQLgpfvN5tFJWATFCLZB_YjKQX_O8TrGHlyUDYg=',
})

live.on('open', () => console.log('连接成功'))

live.on('close', () => console.log('断开连接'))

live.on('SEND_GIFT', (message) => { // 有礼物, 但不会被触发
Expand Down
34 changes: 16 additions & 18 deletions playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,30 @@ import { onMounted, ref } from 'vue'
const danmu = ref<any[]>([])
onMounted(() => {
fetch(`https://api.live.bilibili.com/room/v1/Room/mobileRoomInit?id=${import.meta.env.VITE_ROOM}`)
.then(w => w.json())
.then((data) => {
console.log(data)
})
function randomElement<T>(list: T[]): T {
return list[Math.floor(Math.random() * list.length)]
}
const live = new KeepLiveWS(import.meta.env.VITE_ROOM)
onMounted(async () => {
const room = await fetch(`/api/room/v1/Room/mobileRoomInit?id=${import.meta.env.VITE_ROOM}`)
.then(res => res.json())
const conf = await fetch(`/api/room/v1/Danmu/getConf?room_id=${room.data.room_id}&platform=pc&player=web`)
.then(res => res.json())
live.runWhenConnected(() => {
console.log(`正在监听 ${import.meta.env.VITE_ROOM}`)
})
const host = randomElement(conf.data.host_server_list)
live.on('WELCOME_GUARD', (message) => { // 有弹幕会被触发
console.log(message)
const live = new KeepLiveWS(room.data.room_id, {
url: `wss://${host.host}:${host.wss_port}/sub`,
key: conf.data.token,
})
live.on('WELCOME', (message) => { // 有弹幕会被触发
console.log(message)
live.runWhenConnected(() => {
console.log(`正在监听 ${import.meta.env.VITE_ROOM}`)
})
live.on('SUPER_CHAT_MESSAGE', (message) => { // 有弹幕会被触发
console.log(message)
})
live.on('DANMU_MSG', async (m) => {
console.log('Online: ', await live.getOnline())
live.on('DANMU_MSG', (m) => {
danmu.value.push(m)
})
})
Expand Down
9 changes: 9 additions & 0 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
server: {
proxy: {
'/api': {
target: 'https://api.live.bilibili.com',
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, ''),
},
},
},
})
Loading

0 comments on commit f47fc53

Please sign in to comment.