添加多房间支持
This commit is contained in:
parent
be192c8585
commit
c810083b74
@ -36,17 +36,14 @@ class APIMsgHandler extends EventEmitter {
|
|||||||
|
|
||||||
const serverEventEmitter = new APIMsgHandler()
|
const serverEventEmitter = new APIMsgHandler()
|
||||||
|
|
||||||
serverEventEmitter.on("SEND", (_socket: WebSocketClient, data: string) => {
|
serverEventEmitter.on("SEND", (_socket: WebSocketClient, dataJson: string) => {
|
||||||
sendDanmaku({
|
const data: {
|
||||||
msg: data,
|
msg: string
|
||||||
})
|
roomId: number
|
||||||
})
|
} = JSON.parse(dataJson)
|
||||||
|
sendDanmaku(data.roomId, {
|
||||||
serverEventEmitter.on("ROOMID", (socket: WebSocket) => {
|
msg: data.msg
|
||||||
socket.send(JSON.stringify({
|
})
|
||||||
cmd: "ROOMID",
|
|
||||||
data: config.room_id,
|
|
||||||
}))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
server.on("connection", (client: WebSocketClient) => {
|
server.on("connection", (client: WebSocketClient) => {
|
||||||
|
@ -21,7 +21,7 @@ interface api_config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface ConfigStruct {
|
interface ConfigStruct {
|
||||||
room_id: number
|
room_id: Array<number>
|
||||||
verify: Credential
|
verify: Credential
|
||||||
danmakus: DanmakuTemplate
|
danmakus: DanmakuTemplate
|
||||||
cold_down_time: number
|
cold_down_time: number
|
||||||
@ -34,6 +34,6 @@ interface ConfigStruct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const decoder = new TextDecoder('utf-8')
|
const decoder = new TextDecoder('utf-8')
|
||||||
const config: ConfigStruct = JSON.parse(decoder.decode(Deno.readFileSync(`config/${Deno.args[0]}`)))
|
const config: ConfigStruct = JSON.parse(decoder.decode(Deno.readFileSync(Deno.args[0])))
|
||||||
|
|
||||||
export default config
|
export default config
|
||||||
|
@ -12,13 +12,13 @@ let logFile = Deno.openSync(`./log/${getTimeString()}-${config.room_id}.log`, {
|
|||||||
const thanksColdDownSet = new Set<string>()
|
const thanksColdDownSet = new Set<string>()
|
||||||
|
|
||||||
|
|
||||||
export function receiveGift(data: any) {
|
export function receiveGift(roomId: number, data: any) {
|
||||||
if(thanksColdDownSet.has(data.uname)){
|
if(thanksColdDownSet.has(data.uname)){
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} ${data.uname} 投喂了${data.super_gift_num}个 ${data.giftName} 价值${data.price / 1000 * data.super_gift_num}元\n`))
|
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} ${data.uname} 投喂了${data.super_gift_num}个 ${data.giftName} 价值${data.price / 1000 * data.super_gift_num}元\n`))
|
||||||
if (config.free_gift_action || data.super_gift_num > 0) {
|
if (config.free_gift_action || data.super_gift_num > 0) {
|
||||||
sendDanmaku({
|
sendDanmaku(roomId, {
|
||||||
msg: FormatString(config.danmakus.gift, { name: data.uname, gift: data.giftName })
|
msg: FormatString(config.danmakus.gift, { name: data.uname, gift: data.giftName })
|
||||||
})
|
})
|
||||||
thanksColdDownSet.add(data.uname)
|
thanksColdDownSet.add(data.uname)
|
||||||
@ -26,46 +26,46 @@ export function receiveGift(data: any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function onTotalGift(data: any) {
|
export function onTotalGift(roomId: number, data: any) {
|
||||||
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} ${data.uname}投喂了${data.total_num}个${data.gift_name}\n`))
|
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} ${data.uname}投喂了${data.total_num}个${data.gift_name}\n`))
|
||||||
sendDanmaku({
|
sendDanmaku(roomId, {
|
||||||
msg: FormatString(config.danmakus.gift_total, { name: data.uname, gift: data.gift_name, count: data.total_num })
|
msg: FormatString(config.danmakus.gift_total, { name: data.uname, gift: data.gift_name, count: data.total_num })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function receiveDanmaku(data: any) {
|
export function receiveDanmaku(roomId:number, data: any) {
|
||||||
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} ${data[2][1]}:${data[2][0]} ${data[1]}\n`))
|
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} ${roomId} ${data[2][1]}:${data[2][0]} ${data[1]}\n`))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function onLiveStart() {
|
export function onLiveStart(roomId: number) {
|
||||||
if(skipCount != 1){
|
if(skipCount != 1){
|
||||||
skipCount ++
|
skipCount ++
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
skipCount = 0
|
skipCount = 0
|
||||||
logFile.close()
|
logFile.close()
|
||||||
sendDanmaku({ msg: config.danmakus.live_start })
|
sendDanmaku(roomId, { msg: config.danmakus.live_start })
|
||||||
logFile = Deno.openSync(`./log/${getTimeString()}-${config.room_id}.log`, {
|
logFile = Deno.openSync(`./log/${getTimeString()}-${config.room_id}.log`, {
|
||||||
create: true
|
create: true
|
||||||
})
|
})
|
||||||
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} 直播开始\n`))
|
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} 直播开始\n`))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function onLiveEnd() {
|
export function onLiveEnd(roomId: number) {
|
||||||
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} 直播结束\n`))
|
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} 直播结束\n`))
|
||||||
sendDanmaku({ msg: config.danmakus.live_end })
|
sendDanmaku(roomId, { msg: config.danmakus.live_end })
|
||||||
}
|
}
|
||||||
|
|
||||||
export function onGraud(data: any) {
|
export function onGraud(roomId: number, data: any) {
|
||||||
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} ${data.username}:${data.uid} 购买了 ${data.gift_name}\n`))
|
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} ${data.username}:${data.uid} 购买了 ${data.gift_name}\n`))
|
||||||
sendDanmaku({
|
sendDanmaku(roomId, {
|
||||||
msg: FormatString(config.danmakus.guard, { type: data.gift_name, name: data.username })
|
msg: FormatString(config.danmakus.guard, { type: data.gift_name, name: data.username })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function onSuperChat(data: any) {
|
export function onSuperChat(roomId: number, data: any) {
|
||||||
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} ${data.user_info.uname}发送了SC 价格${data.price}\n`))
|
logFile.writeSync(Encoding.UTF8.getBytes(`${getTimeString()} ${data.user_info.uname}发送了SC 价格${data.price}\n`))
|
||||||
sendDanmaku({
|
sendDanmaku(roomId, {
|
||||||
msg: FormatString(config.danmakus.sc, { name: data.user_info.uname })
|
msg: FormatString(config.danmakus.sc, { name: data.user_info.uname })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -127,9 +127,9 @@ export class DanmakuReceiver extends EventEmitter {
|
|||||||
const packetData = resultRaw.slice(offset + 16, offset + length)
|
const packetData = resultRaw.slice(offset + 16, offset + length)
|
||||||
const data = JSON.parse(decoder.decode(packetData))
|
const data = JSON.parse(decoder.decode(packetData))
|
||||||
const cmd = data.cmd.split(":")[0]
|
const cmd = data.cmd.split(":")[0]
|
||||||
this.emit(cmd, data.info || data.data)
|
this.emit(cmd, this.roomId, data.info || data.data)
|
||||||
apiServer.clients.forEach((client: WebSocketClient) => {
|
apiServer.clients.forEach((client: WebSocketClient) => {
|
||||||
client.send(JSON.stringify({ cmd, data: data.info || data.data }))
|
client.send(JSON.stringify({ cmd, room: this.roomId, data: data.info || data.data }))
|
||||||
})
|
})
|
||||||
offset += length
|
offset += length
|
||||||
}
|
}
|
||||||
|
@ -2,5 +2,5 @@ import * as events from 'https://deno.land/x/events@v1.0.0/mod.ts'
|
|||||||
|
|
||||||
const EventEmitter = events.default
|
const EventEmitter = events.default
|
||||||
export { EventEmitter }
|
export { EventEmitter }
|
||||||
export * as brotli from 'https://deno.land/x/brotli@v0.1.4/mod.ts'
|
export * as brotli from 'https://deno.land/x/brotli@0.1.7/mod.ts'
|
||||||
export { WebSocketServer, type WebSocketClient } from 'https://deno.land/x/websocket@v0.1.4/mod.ts'
|
export { WebSocketServer, type WebSocketClient } from 'https://deno.land/x/websocket@v0.1.4/mod.ts'
|
||||||
|
52
src/Main.ts
52
src/Main.ts
@ -4,31 +4,35 @@ import { onGraud, onLiveEnd, onLiveStart, onSuperChat, onTotalGift, receiveDanma
|
|||||||
import { printLog } from './utils/mod.ts'
|
import { printLog } from './utils/mod.ts'
|
||||||
import { launchAllPlugins } from './Plugins.ts'
|
import { launchAllPlugins } from './Plugins.ts'
|
||||||
|
|
||||||
const danmakuReceiver = new DanmakuReceiver(config.room_id)
|
const roomReceiverMap: Map<number, DanmakuReceiver> = new Map()
|
||||||
danmakuReceiver.on('connected', () => {
|
for(const room of config.room_id) {
|
||||||
printLog('主程序', '连接成功')
|
const danmakuReceiver = new DanmakuReceiver(room)
|
||||||
})
|
danmakuReceiver.on('connected', () => {
|
||||||
|
printLog('主程序', '连接成功')
|
||||||
|
})
|
||||||
|
|
||||||
if (!config.disable_gift_action) {
|
if (!config.disable_gift_action) {
|
||||||
danmakuReceiver.on('COMBO_SEND', onTotalGift)
|
danmakuReceiver.on('COMBO_SEND', onTotalGift)
|
||||||
danmakuReceiver.on('SEND_GIFT', receiveGift)
|
danmakuReceiver.on('SEND_GIFT', receiveGift)
|
||||||
}
|
}
|
||||||
if (!config.disable_super_chat_action) {
|
if (!config.disable_super_chat_action) {
|
||||||
danmakuReceiver.on('GUARD_BUY', onGraud)
|
danmakuReceiver.on('GUARD_BUY', onGraud)
|
||||||
}
|
}
|
||||||
if (!config.disable_super_chat_action) {
|
if (!config.disable_super_chat_action) {
|
||||||
danmakuReceiver.on('SUPER_CHAT_MESSAGE', onSuperChat)
|
danmakuReceiver.on('SUPER_CHAT_MESSAGE', onSuperChat)
|
||||||
}
|
}
|
||||||
globalThis.onunload = () => {
|
globalThis.onunload = () => {
|
||||||
printLog('主程序', '退出')
|
printLog('主程序', '退出')
|
||||||
}
|
}
|
||||||
danmakuReceiver.on('closed', () => {
|
danmakuReceiver.on('closed', () => {
|
||||||
printLog('主程序', '掉线了')
|
printLog('主程序', '掉线了')
|
||||||
|
danmakuReceiver.connect()
|
||||||
|
})
|
||||||
|
danmakuReceiver.on('LIVE', onLiveStart)
|
||||||
|
danmakuReceiver.on('PREPARING', onLiveEnd)
|
||||||
|
danmakuReceiver.on('DANMU_MSG', receiveDanmaku)
|
||||||
danmakuReceiver.connect()
|
danmakuReceiver.connect()
|
||||||
})
|
roomReceiverMap.set(room, danmakuReceiver)
|
||||||
danmakuReceiver.on('LIVE', onLiveStart)
|
}
|
||||||
danmakuReceiver.on('PREPARING', onLiveEnd)
|
|
||||||
danmakuReceiver.on('DANMU_MSG', receiveDanmaku)
|
|
||||||
|
|
||||||
launchAllPlugins()
|
launchAllPlugins()
|
||||||
danmakuReceiver.connect()
|
|
||||||
|
@ -15,13 +15,13 @@ export interface DanmakuStruct {
|
|||||||
csrf_token?: string
|
csrf_token?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function sendDanmaku(danmaku: DanmakuStruct) {
|
export function sendDanmaku(roomId: number, danmaku: DanmakuStruct) {
|
||||||
if (danmaku.msg.length > 19) {
|
if (danmaku.msg.length > 19) {
|
||||||
sendDanmaku({
|
sendDanmaku(roomId, {
|
||||||
msg: danmaku.msg.slice(0, 15),
|
msg: danmaku.msg.slice(0, 15),
|
||||||
})
|
})
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
sendDanmaku({
|
sendDanmaku(roomId, {
|
||||||
msg: danmaku.msg.slice(15, danmaku.msg.length),
|
msg: danmaku.msg.slice(15, danmaku.msg.length),
|
||||||
})
|
})
|
||||||
}, 2000)
|
}, 2000)
|
||||||
@ -40,7 +40,7 @@ export function sendDanmaku(danmaku: DanmakuStruct) {
|
|||||||
if (!danmaku.fontsize) {
|
if (!danmaku.fontsize) {
|
||||||
danmaku.fontsize = 24
|
danmaku.fontsize = 24
|
||||||
}
|
}
|
||||||
danmaku.roomid = config.room_id as number
|
danmaku.roomid = roomId
|
||||||
danmaku.csrf = danmaku.csrf_token = config.verify.csrf
|
danmaku.csrf = danmaku.csrf_token = config.verify.csrf
|
||||||
const data = new FormData()
|
const data = new FormData()
|
||||||
for (const k in danmaku) {
|
for (const k in danmaku) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user