Skip to content

Commit bb00a21

Browse files
committed
feat: add HEARTBEAT listener
1 parent 103a535 commit bb00a21

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "blive-message-listener",
3-
"version": "0.1.3",
3+
"version": "0.1.4",
44
"description": "Bilibili-live danmu listener with type",
55
"type": "module",
66
"main": "dist/index.js",

src/listener/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
HEARTBEAT, type AttentionChangeMsgHandler,
23
DANMU_MSG, type DanmuMsgHandler,
34
GUARD_BUY, type GuardBuyHandler,
45
SEND_GIFT, type GiftHandler,
@@ -9,6 +10,7 @@ import type { Message } from '../types/app'
910
import type { KeepLiveTCP } from 'bilibili-live-ws'
1011

1112
export type MsgHandler = Partial<
13+
& AttentionChangeMsgHandler
1214
& DanmuMsgHandler
1315
& GuardBuyHandler
1416
& GiftHandler
@@ -31,6 +33,14 @@ const normalizeDanmu = <T>(msgType: string, data: T): Message<T> => {
3133
export const listenAll = (instance: KeepLiveTCP, handler?: MsgHandler) => {
3234
if (!handler) return
3335

36+
// HEARTBEAT
37+
if (handler[HEARTBEAT.handlerName]) {
38+
instance.on(HEARTBEAT.eventName, (data: any) => {
39+
const parsedData = HEARTBEAT.parser(data)
40+
handler[HEARTBEAT.handlerName]?.(normalizeDanmu(HEARTBEAT.eventName, parsedData))
41+
})
42+
}
43+
3444
// DANMU_MSG
3545
if (handler[DANMU_MSG.handlerName]) {
3646
instance.on(DANMU_MSG.eventName, (data: any) => {

src/parser/HEARTBEAT.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { Message } from '../types/app'
2+
3+
export interface AttentionChangeMsg {
4+
/** 直播间热度·*/
5+
attention: number
6+
}
7+
8+
const parser = (data: any): AttentionChangeMsg => {
9+
return {
10+
attention: data,
11+
}
12+
}
13+
14+
export const HEARTBEAT = {
15+
parser,
16+
eventName: 'heartbeat' as const,
17+
handlerName: 'onAttentionChange' as const,
18+
}
19+
20+
export type Handler = {
21+
onAttentionChange: (data: Message<AttentionChangeMsg>) => void
22+
}

src/parser/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { HEARTBEAT, Handler as AttentionChangeMsgHandler, AttentionChangeMsg } from './HEARTBEAT'
12
export { DANMU_MSG, Handler as DanmuMsgHandler, DanmuMsg } from './DANMU_MSG'
23
export { GUARD_BUY, Handler as GuardBuyHandler, GuardBuyMsg } from './GUARD_BUY'
34
export { SEND_GIFT, Handler as GiftHandler, GiftMsg } from './SEND_GIFT'

src/types/message.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type {
2+
AttentionChangeMsg,
23
DanmuMsg,
34
GuardBuyMsg,
45
GiftMsg,

0 commit comments

Comments
 (0)