Skip to content

Commit

Permalink
feat!: 用户关注/分享直播间消息
Browse files Browse the repository at this point in the history
  • Loading branch information
ddiu8081 committed Oct 11, 2022
1 parent 001af59 commit 855ea84
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
19 changes: 11 additions & 8 deletions README.md
Expand Up @@ -175,7 +175,7 @@ export type Handler = {
| onWatchedChange | 累计看过人数变化 |
| onLikedChange | 累计点赞人数变化 |
| onRankCountChange | 高能用户人数变化 |
| onNewComer | 观众进入直播间 |
| onUserAction | 用户进入、关注、分享直播间 |
| onRoomInfoChange | 直播间信息修改 |
##### handler.onLiveStart
Expand Down Expand Up @@ -290,25 +290,28 @@ export interface RankCountChangeMsg {
}
```

##### handler.onNewComer
##### handler.onUserAction

观众进入直播间
用户进入、关注、分享直播间

- 包括普通用户进入与舰长进入
- 舰长进入直播间时,有几率会触发两次
- 舰长进入直播间时,uname 超长可能会省略号截断

```ts
export type Handler = {
/** 观众进入直播间 */
onNewComer: (msg: Message<NewComerMsg>) => void
/** 用户进入、关注、分享直播间 */
onUserAction: (msg: Message<UserActionMsg>) => void
}

type msgType = 'INTERACT_WORD' | 'ENTRY_EFFECT'

export interface NewComerMsg {
type UserActionType = 'enter' | 'follow' | 'share' | 'unknown'

export interface UserActionMsg {
user: User
/** 入场时间,毫秒时间戳 */
/** 事件类型 */
type: UserActionType
/** 事件时间,毫秒时间戳 */
timestamp: number
}
```
Expand Down
4 changes: 2 additions & 2 deletions src/listener/index.ts
Expand Up @@ -4,7 +4,7 @@ import {
PREPARING, type LiveStopMsgHandler,
DANMU_MSG, type DanmuMsgHandler,
GUARD_BUY, type GuardBuyHandler,
INTERACT_WORD, ENTRY_EFFECT, type NewComerMsgHandler,
INTERACT_WORD, ENTRY_EFFECT, type UserActionMsgHandler,
LIKE_INFO_V3_UPDATE, type LikedChangeMsgHandler,
ONLINE_RANK_COUNT, type RankCountChangeMsgHandler,
ROOM_CHANGE, type RoomInfoChangeHandler,
Expand All @@ -31,7 +31,7 @@ export type MsgHandler = Partial<
& LiveStopMsgHandler
& DanmuMsgHandler
& GuardBuyHandler
& NewComerMsgHandler
& UserActionMsgHandler
& LikedChangeMsgHandler
& RankCountChangeMsgHandler
& RoomInfoChangeHandler
Expand Down
32 changes: 23 additions & 9 deletions src/parser/INTERACT_WORD_ENTRY_EFFECT.ts
@@ -1,14 +1,26 @@
import { intToColorHex } from '../utils/color'
import type { Message, User } from '../types/app'

export interface NewComerMsg {
type UserActionType = 'enter' | 'follow' | 'share' | 'unknown'

export interface UserActionMsg {
user: User
/** 入场时间,毫秒时间戳 */
/** 事件类型 */
type: UserActionType
/** 事件时间,毫秒时间戳 */
timestamp: number
}

const parserNormal = (data: any, roomId: number): NewComerMsg => {
const parserNormal = (data: any, roomId: number): UserActionMsg => {
const rawData = data.data
let eventType: UserActionType = 'unknown'
if (rawData.msg_type === 1) {
eventType = 'enter'
} else if (rawData.msg_type === 2) {
eventType = 'follow'
} else if (rawData.msg_type === 3) {
eventType = 'share'
}
return {
user: {
uid: rawData.uid,
Expand All @@ -32,11 +44,12 @@ const parserNormal = (data: any, roomId: number): NewComerMsg => {
room_admin: false,
}
},
type: eventType,
timestamp: Math.ceil(rawData.trigger_time / 1000000),
}
}

const parserGuard = (data: any, roomId: number): NewComerMsg => {
const parserGuard = (data: any, roomId: number): UserActionMsg => {
const rawData = data.data
const uname = /<%(.*)%>/.exec(rawData.copy_writing)?.[1] || ''
return {
Expand All @@ -49,11 +62,12 @@ const parserGuard = (data: any, roomId: number): NewComerMsg => {
room_admin: false,
}
},
type: 'enter',
timestamp: Math.ceil(rawData.trigger_time / 1000000),
}
}

const parser = (data: any, roomId: number): NewComerMsg => {
const parser = (data: any, roomId: number): UserActionMsg => {
const msgType = data.cmd
if (msgType === 'ENTRY_EFFECT') {
return parserGuard(data, roomId)
Expand All @@ -66,16 +80,16 @@ const parser = (data: any, roomId: number): NewComerMsg => {
export const INTERACT_WORD = {
parser,
eventName: 'INTERACT_WORD' as const,
handlerName: 'onNewComer' as const,
handlerName: 'onUserAction' as const,
}

export const ENTRY_EFFECT = {
parser,
eventName: 'ENTRY_EFFECT' as const,
handlerName: 'onNewComer' as const,
handlerName: 'onUserAction' as const,
}

export type Handler = {
/** 观众进入直播间 */
onNewComer: (msg: Message<NewComerMsg>) => void
/** 用户进入、关注、分享直播间 */
onUserAction: (msg: Message<UserActionMsg>) => void
}
2 changes: 1 addition & 1 deletion src/parser/index.ts
Expand Up @@ -3,7 +3,7 @@ export { LIVE, Handler as LiveStartMsgHandler, LiveStartMsg } from './LIVE'
export { PREPARING, Handler as LiveStopMsgHandler, LiveEndMsg } 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'
export { INTERACT_WORD, ENTRY_EFFECT, Handler as UserActionMsgHandler, UserActionMsg } from './INTERACT_WORD_ENTRY_EFFECT'
export { LIKE_INFO_V3_UPDATE, Handler as LikedChangeMsgHandler, LikedChangeMsg } from './LIKE_INFO_V3_UPDATE'
export { ONLINE_RANK_COUNT, Handler as RankCountChangeMsgHandler, RankCountChangeMsg } from './ONLINE_RANK_COUNT'
export { ROOM_CHANGE, Handler as RoomInfoChangeHandler, RoomInfoChangeMsg } from './ROOM_CHANGE'
Expand Down
2 changes: 1 addition & 1 deletion src/types/message.ts
Expand Up @@ -2,7 +2,7 @@ export type {
AttentionChangeMsg,
DanmuMsg,
GuardBuyMsg,
NewComerMsg,
UserActionMsg,
GiftMsg,
SuperChatMsg,
WatchedChangeMsg,
Expand Down

0 comments on commit 855ea84

Please sign in to comment.