Skip to content

Commit

Permalink
feat: 开播/关播消息
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiu8081 committed Oct 1, 2022
1 parent 0d6032f commit 79d397f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.md
Expand Up @@ -152,6 +152,44 @@ export type Handler = {
}
```
### handler.onLiveStart
直播开始消息
```ts
export type Handler = {
/** 直播开始消息 */
onLiveStart: (msg: Message<LiveStartMsg>) => void
}

type msgType = 'LIVE'

export interface LiveStartMsg {
/** 开播平台 */
live_platform: string
/** 房间号 */
room_id: number
}
```

### handler.onLiveEnd

直播结束消息

```ts
export type Handler = {
/** 直播结束消息 */
onLiveEnd: (msg: Message<LiveEndMsg>) => void
}

type msgType = 'PREPARING'

export interface LiveEndMsg {
/** 房间号 */
room_id: number
}
```

### handler.onIncomeDanmu

收到普通弹幕消息
Expand Down
20 changes: 20 additions & 0 deletions src/listener/index.ts
@@ -1,5 +1,7 @@
import {
HEARTBEAT, type AttentionChangeMsgHandler,
LIVE, type LiveStartMsgHandler,
PREPARING, type LiveStopMsgHandler,
DANMU_MSG, type DanmuMsgHandler,
GUARD_BUY, type GuardBuyHandler,
INTERACT_WORD, ENTRY_EFFECT, type NewComerMsgHandler,
Expand All @@ -22,6 +24,8 @@ export type MsgHandler = Partial<
onStartListen: () => void,
}
& AttentionChangeMsgHandler
& LiveStartMsgHandler
& LiveStopMsgHandler
& DanmuMsgHandler
& GuardBuyHandler
& NewComerMsgHandler
Expand Down Expand Up @@ -71,6 +75,22 @@ export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, ha
})
}

// LIVE
if (handler[LIVE.handlerName]) {
instance.on(LIVE.eventName, (data: WSMessage<any>) => {
const parsedData = LIVE.parser(data.data)
handler[LIVE.handlerName]?.(normalizeDanmu(LIVE.eventName, parsedData))
})
}

// PREPARING
if (handler[PREPARING.handlerName]) {
instance.on(PREPARING.eventName, (data: WSMessage<any>) => {
const parsedData = PREPARING.parser(data.data)
handler[PREPARING.handlerName]?.(normalizeDanmu(PREPARING.eventName, parsedData))
})
}

// DANMU_MSG
if (handler[DANMU_MSG.handlerName]) {
instance.on(DANMU_MSG.eventName, (data: WSMessage<any>) => {
Expand Down
26 changes: 26 additions & 0 deletions src/parser/LIVE.ts
@@ -0,0 +1,26 @@
import type { Message } from '../types/app'

export interface LiveStartMsg {
/** 开播平台 */
live_platform: string
/** 房间号 */
room_id: number
}

const parser = (data: any): LiveStartMsg => {
return {
live_platform: data.live_platform,
room_id: data.roomid,
}
}

export const LIVE = {
parser,
eventName: 'LIVE' as const,
handlerName: 'onLiveStart' as const,
}

export type Handler = {
/** 直播开始消息 */
onLiveStart: (msg: Message<LiveStartMsg>) => void
}
23 changes: 23 additions & 0 deletions src/parser/PREPARING.ts
@@ -0,0 +1,23 @@
import type { Message } from '../types/app'

export interface LiveEndMsg {
/** 房间号 */
room_id: number
}

const parser = (data: any): LiveEndMsg => {
return {
room_id: parseInt(data.roomid),
}
}

export const PREPARING = {
parser,
eventName: 'PREPARING' as const,
handlerName: 'onLiveEnd' as const,
}

export type Handler = {
/** 直播结束消息 */
onLiveEnd: (msg: Message<LiveEndMsg>) => void
}
2 changes: 2 additions & 0 deletions src/parser/index.ts
@@ -1,4 +1,6 @@
export { HEARTBEAT, Handler as AttentionChangeMsgHandler, AttentionChangeMsg } from './HEARTBEAT'
export { LIVE, Handler as LiveStartMsgHandler, LiveStartMsg } from './LIVE'
export { PREPARING, Handler as LiveStopMsgHandler, LiveStopMsg } from './PREPARING'
export { DANMU_MSG, Handler as DanmuMsgHandler, DanmuMsg } from './DANMU_MSG'
export { GUARD_BUY, Handler as GuardBuyHandler, GuardBuyMsg } from './GUARD_BUY'
export { INTERACT_WORD, ENTRY_EFFECT, Handler as NewComerMsgHandler, NewComerMsg } from './INTERACT_WORD_ENTRY_EFFECT'
Expand Down

0 comments on commit 79d397f

Please sign in to comment.