Skip to content

Commit 60f7752

Browse files
committed
feat: check duplicate msg (#14)
1 parent 679cc9d commit 60f7752

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

src/listener/index.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
SUPER_CHAT_MESSAGE, type SuperChatHandler,
1313
WATCHED_CHANGE, type WatchedChangeHandler,
1414
} from '../parser'
15-
import type { Message } from '../types/app'
1615
import type { KeepLiveTCP, KeepLiveWS, Message as WSMessage } from 'tiny-bilibili-ws'
16+
import { normalizeDanmu, checkIsDuplicateDanmuMsg } from '../utils/message'
1717

1818
export type MsgHandler = Partial<
1919
{
@@ -40,19 +40,6 @@ export type MsgHandler = Partial<
4040
& WatchedChangeHandler
4141
>
4242

43-
const normalizeDanmu = <T>(msgType: string, body: T): Message<T> => {
44-
const timestamp = Date.now()
45-
const randomText = Math.floor(Math.random() * 10000).toString()
46-
// @ts-ignore
47-
const id = `${timestamp}:${msgType}:${body.user?.uid}:${randomText}`
48-
return {
49-
id,
50-
timestamp,
51-
type: msgType,
52-
body,
53-
}
54-
}
55-
5643
export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, handler?: MsgHandler) => {
5744
if (!handler) return
5845

@@ -99,14 +86,14 @@ export const listenAll = (instance: KeepLiveTCP | KeepLiveWS, roomId: number, ha
9986

10087
// DANMU_MSG
10188
if (handler[DANMU_MSG.handlerName] || handler[DANMU_MSG_402220.handlerName]) {
102-
instance.on(DANMU_MSG.eventName, (data: WSMessage<any>) => {
89+
const msgCallback = handler[DANMU_MSG.handlerName]!
90+
const handleDanmuMsg = (data: WSMessage<any>) => {
10391
const parsedData = DANMU_MSG.parser(data.data, roomId)
104-
handler[DANMU_MSG.handlerName]?.(normalizeDanmu(DANMU_MSG.eventName, parsedData))
105-
})
106-
instance.on(DANMU_MSG_402220.eventName, (data: WSMessage<any>) => {
107-
const parsedData = DANMU_MSG_402220.parser(data.data, roomId)
108-
handler[DANMU_MSG_402220.handlerName]?.(normalizeDanmu(DANMU_MSG_402220.eventName, parsedData))
109-
})
92+
if (checkIsDuplicateDanmuMsg(parsedData)) return
93+
msgCallback(normalizeDanmu(DANMU_MSG.eventName, parsedData))
94+
}
95+
instance.on(DANMU_MSG.eventName, handleDanmuMsg)
96+
instance.on(DANMU_MSG_402220.eventName, handleDanmuMsg)
11097
}
11198

11299
// GUARD_BUY

src/utils/message.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { Message } from '../types/app'
2+
import type { DanmuMsg } from '../parser'
3+
4+
type QueueItem = [number, string]
5+
class MsgQueue {
6+
private items: QueueItem[] = []
7+
push = (item: QueueItem) => {
8+
this.items.push(item)
9+
this.debug()
10+
if (this.items.length > 10) {
11+
this.items.shift()
12+
}
13+
}
14+
has = ( [timestamp, content]: QueueItem ) => {
15+
return this.items.some(i => {
16+
const [queueTimestamp, queueContent] = i
17+
return Math.abs(timestamp - queueTimestamp) < 1200 && content === queueContent
18+
})
19+
}
20+
debug = () => console.log(this.items)
21+
}
22+
23+
const msgQueue = new MsgQueue()
24+
25+
export const normalizeDanmu = <T>(msgType: string, body: T): Message<T> => {
26+
const timestamp = Date.now()
27+
const randomText = Math.floor(Math.random() * 10000).toString()
28+
// @ts-ignore
29+
const id = `${timestamp}:${msgType}:${body.user?.uid}:${randomText}`
30+
return {
31+
id,
32+
timestamp,
33+
type: msgType,
34+
body,
35+
}
36+
}
37+
38+
export const checkIsDuplicateDanmuMsg = (msg: DanmuMsg) => {
39+
const msgIdentifier = `${msg.user.uid}:${msg.content}`
40+
const queueItem: QueueItem = [msg.timestamp, msgIdentifier]
41+
if (msgQueue.has(queueItem)) {
42+
return true
43+
}
44+
msgQueue.push(queueItem)
45+
return false
46+
}

0 commit comments

Comments
 (0)