mirror of
https://github.com/Megghy/vtsuru.live.git
synced 2025-12-06 18:36:55 +08:00
fix wrong langue value when adding songs
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { useAccount } from '@/api/account'
|
||||
import { useVTsuruHub } from '@/store/useVTsuruHub'
|
||||
import Peer, { DataConnection } from 'peerjs'
|
||||
import { Ref, ref } from 'vue'
|
||||
|
||||
export interface ComponentsEventHubModel {
|
||||
IsMaster: boolean
|
||||
@@ -27,29 +26,34 @@ abstract class BaseRTCClient {
|
||||
public peer?: Peer
|
||||
|
||||
protected connections: DataConnection[] = []
|
||||
protected handledEvents: { [key: string]: string[] } = {}
|
||||
|
||||
protected events: {
|
||||
[key: string]: ((args: unknown) => void)[]
|
||||
[key: string]: ((args: any) => void)[]
|
||||
} = {}
|
||||
|
||||
abstract type: 'master' | 'slave'
|
||||
|
||||
public on(eventName: string, listener: (args: unknown) => void) {
|
||||
public on(eventName: string, listener: (args: any) => void) {
|
||||
eventName = eventName.toLowerCase()
|
||||
if (!this.events[eventName]) {
|
||||
this.events[eventName] = []
|
||||
}
|
||||
this.events[eventName].push(listener)
|
||||
|
||||
this.send('VTsuru.RTCEvent.On', eventName)
|
||||
}
|
||||
public off(eventName: string, listener: (args: unknown) => void) {
|
||||
public off(eventName: string, listener: (args: any) => void) {
|
||||
if (this.events[eventName]) {
|
||||
const index = this.events[eventName].indexOf(listener)
|
||||
if (index > -1) {
|
||||
this.events[eventName].splice(index, 1)
|
||||
}
|
||||
}
|
||||
|
||||
this.send('VTsuru.RTCEvent.Off', eventName)
|
||||
}
|
||||
public send(eventName: string, data: unknown) {
|
||||
public send(eventName: string, data: any) {
|
||||
this.connections.forEach((item) =>
|
||||
item.send({
|
||||
Key: eventName,
|
||||
@@ -93,10 +97,18 @@ abstract class BaseRTCClient {
|
||||
this.peer?.reconnect()
|
||||
})
|
||||
}
|
||||
public processData(data: RTCData) {
|
||||
public processData(conn: DataConnection, data: RTCData) {
|
||||
//console.log(data)
|
||||
if (data.Key == 'Heartbeat') return
|
||||
if (this.events[data.Key.toLowerCase()]) {
|
||||
if (data.Key == 'Heartbeat') { // 心跳
|
||||
return
|
||||
} else if (data.Key == 'VTsuru.RTCEvent.On') { // 添加事件
|
||||
this.handledEvents[conn.peer].push(data.Data)
|
||||
} else if (data.Key == 'VTsuru.RTCEvent.Off') { // 移除事件
|
||||
const i = this.handledEvents[conn.peer].indexOf(data.Data)
|
||||
if (i > -1) {
|
||||
this.handledEvents[conn.peer].splice(i, 1)
|
||||
}
|
||||
} else if (this.events[data.Key.toLowerCase()]) {
|
||||
this.events[data.Key].forEach((item) => item(data.Data))
|
||||
}
|
||||
}
|
||||
@@ -107,6 +119,8 @@ abstract class BaseRTCClient {
|
||||
}
|
||||
protected onConnectionClose(id: string) {
|
||||
this.connections = this.connections.filter((item) => item.peer != id)
|
||||
delete this.handledEvents[id]
|
||||
|
||||
console.log(
|
||||
`[Components-Event] <${this.connections.length}> ${this.type == 'master' ? 'Slave' : 'Master'} 下线: ` +
|
||||
id
|
||||
@@ -140,17 +154,20 @@ export class SlaveRTCClient extends BaseRTCClient {
|
||||
//console.log('[Components-Event] 正在连接到现有 Master: ' + item.Token)
|
||||
})
|
||||
}
|
||||
public connectToMaster(token: string) {
|
||||
if (this.connections.some((conn) => conn.peer == token)) return
|
||||
const c = this.peer?.connect(token)
|
||||
public connectToMaster(id: string) {
|
||||
if (this.connections.some((conn) => conn.peer == id)) return
|
||||
const c = this.peer?.connect(id)
|
||||
c?.on('open', () => {
|
||||
this.connections.push(c)
|
||||
|
||||
this.handledEvents[id] = []
|
||||
|
||||
console.log(
|
||||
`[Components-Event] <${this.connections.length}> ==> Master 连接已建立: ` +
|
||||
token
|
||||
id
|
||||
)
|
||||
})
|
||||
c?.on('data', (data) => this.processData(data as RTCData))
|
||||
c?.on('data', (data) => this.processData(c, data as RTCData))
|
||||
c?.on('close', () => this.onConnectionClose(c.peer))
|
||||
}
|
||||
public Init() {
|
||||
@@ -171,21 +188,24 @@ export class MasterRTCClient extends BaseRTCClient {
|
||||
super(user, pass)
|
||||
}
|
||||
type: 'master' = 'master' as const
|
||||
|
||||
public connectRTC() {
|
||||
super.connectRTC()
|
||||
this.peer?.on('connection', (conn) => {
|
||||
conn.on('open', () => {
|
||||
this.connections.push(conn)
|
||||
this.handledEvents[conn.peer] = []
|
||||
console.log(
|
||||
`[Components-Event] <${this.connections.length}> Slave 上线: ` +
|
||||
conn.peer
|
||||
)
|
||||
})
|
||||
conn.on('data', (data) => this.processData(data as RTCData))
|
||||
conn.on('data', (d) => this.processData(conn, d as RTCData))
|
||||
conn.on('error', (err) => console.error(err))
|
||||
conn.on('close', () => this.onConnectionClose(conn.peer))
|
||||
})
|
||||
}
|
||||
|
||||
public Init() {
|
||||
return super.Init()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user