mirror of
https://github.com/Megghy/vtsuru.live.git
synced 2025-12-06 18:36:55 +08:00
chore: format code style and update linting configuration
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { useAccount } from '@/api/account'
|
||||
import type { DataConnection } from 'peerjs'
|
||||
import Peer from 'peerjs'
|
||||
import { useVTsuruHub } from '@/store/useVTsuruHub'
|
||||
import Peer, { DataConnection } from 'peerjs'
|
||||
|
||||
export interface ComponentsEventHubModel {
|
||||
IsMaster: boolean
|
||||
@@ -34,6 +34,9 @@ export abstract class BaseRTCClient {
|
||||
|
||||
abstract type: 'master' | 'slave'
|
||||
|
||||
protected abstract connectRTC(): void
|
||||
protected abstract processData(conn: DataConnection, data: RTCData): void
|
||||
|
||||
public on(eventName: string, listener: (args: any) => void) {
|
||||
eventName = eventName.toLowerCase()
|
||||
if (!this.events[eventName]) {
|
||||
@@ -53,89 +56,35 @@ export abstract class BaseRTCClient {
|
||||
|
||||
this.send('VTsuru.RTCEvent.Off', eventName)
|
||||
}
|
||||
|
||||
public send(eventName: string, data: any) {
|
||||
this.connections.forEach((item) =>
|
||||
item.send({
|
||||
Key: eventName,
|
||||
Data: data
|
||||
})
|
||||
)
|
||||
this.connections.forEach((item) => {
|
||||
if (item && item.open) {
|
||||
item.send({
|
||||
Key: eventName,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
protected connectRTC() {
|
||||
//console.log('[Components-Event] 正在连接到 PeerJS 服务器...')
|
||||
this.peer = new Peer({
|
||||
host: 'peer.suki.club',
|
||||
port: 443,
|
||||
key: 'vtsuru',
|
||||
secure: true,
|
||||
config: {
|
||||
iceServers: [
|
||||
{ url: 'stun:turn.suki.club' },
|
||||
{
|
||||
url: 'turn:turn.suki.club',
|
||||
username: this.user,
|
||||
credential: this.pass
|
||||
}
|
||||
]
|
||||
}
|
||||
//debug: 3
|
||||
})
|
||||
|
||||
this.peer?.on('open', async (id) => {
|
||||
console.log('[Components-Event] 已连接到 PeerJS 服务器: ' + id)
|
||||
this.vhub?.send('SetRTCToken', id, this.type == 'master')
|
||||
})
|
||||
this.peer?.on('error', (err) => {
|
||||
console.error(err)
|
||||
})
|
||||
this.peer?.on('close', () => {
|
||||
console.log('[Components-Event] PeerJS 连接已关闭')
|
||||
})
|
||||
this.peer?.on('disconnected', () => {
|
||||
console.log('[Components-Event] PeerJS 连接已断开')
|
||||
this.peer?.reconnect()
|
||||
})
|
||||
}
|
||||
public processData(conn: DataConnection, data: RTCData) {
|
||||
//console.log(data)
|
||||
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))
|
||||
}
|
||||
}
|
||||
public async getAllRTC() {
|
||||
return (
|
||||
(await this.vhub.invoke<ComponentsEventHubModel[]>('GetOnlineRTC')) || []
|
||||
)
|
||||
}
|
||||
protected onConnectionClose(id: string) {
|
||||
this.connections = this.connections.filter((item) => item.peer != id)
|
||||
this.connections = this.connections.filter(item => item && item.peer != id)
|
||||
delete this.handledEvents[id]
|
||||
|
||||
console.log(
|
||||
`[Components-Event] <${this.connections.length}> ${this.type == 'master' ? 'Slave' : 'Master'} 下线: ` +
|
||||
id
|
||||
`[Components-Event] <${this.connections.length}> ${this.type == 'master' ? 'Slave' : 'Master'} 下线: ${
|
||||
id}`,
|
||||
)
|
||||
}
|
||||
|
||||
public async Init() {
|
||||
if (!this.isInited) {
|
||||
this.isInited = true
|
||||
await this.vhub.on('RTCOffline', (id: string) =>
|
||||
await this.vhub.on('RTCOffline', (...args: unknown[]) => {
|
||||
const id = args[0] as string
|
||||
this.onConnectionClose(id)
|
||||
)
|
||||
})
|
||||
this.connectRTC()
|
||||
}
|
||||
return this
|
||||
@@ -146,21 +95,28 @@ export class SlaveRTCClient extends BaseRTCClient {
|
||||
constructor(user: string, pass: string) {
|
||||
super(user, pass)
|
||||
}
|
||||
|
||||
type: 'slave' = 'slave' as const
|
||||
|
||||
protected async getAllRTC(): Promise<ComponentsEventHubModel[]> {
|
||||
return await this.vhub.invoke<ComponentsEventHubModel[]>('GetAllRTC') || []
|
||||
}
|
||||
|
||||
public async connectToAllMaster() {
|
||||
const masters = (await this.getAllRTC()).filter(
|
||||
(item) =>
|
||||
item.IsMaster &&
|
||||
item.Token != this.peer!.id &&
|
||||
!this.connections.some((conn) => conn.peer == item.Token)
|
||||
(item: ComponentsEventHubModel) =>
|
||||
item.IsMaster
|
||||
&& item.Token != this.peer!.id
|
||||
&& !this.connections.some(conn => conn.peer == item.Token),
|
||||
)
|
||||
masters.forEach((item) => {
|
||||
masters.forEach((item: ComponentsEventHubModel) => {
|
||||
this.connectToMaster(item.Token)
|
||||
//console.log('[Components-Event] 正在连接到现有 Master: ' + item.Token)
|
||||
// console.log('[Components-Event] 正在连接到现有 Master: ' + item.Token)
|
||||
})
|
||||
}
|
||||
|
||||
public connectToMaster(id: string) {
|
||||
if (this.connections.some((conn) => conn.peer == id)) return
|
||||
if (this.connections.some(conn => conn.peer == id)) return
|
||||
const c = this.peer?.connect(id)
|
||||
c?.on('open', () => {
|
||||
this.connections.push(c)
|
||||
@@ -168,17 +124,53 @@ export class SlaveRTCClient extends BaseRTCClient {
|
||||
this.handledEvents[id] = []
|
||||
|
||||
console.log(
|
||||
`[Components-Event] <${this.connections.length}> ==> Master 连接已建立: ` +
|
||||
id
|
||||
`[Components-Event] <${this.connections.length}> ==> Master 连接已建立: ${
|
||||
id}`,
|
||||
)
|
||||
})
|
||||
c?.on('error', (err) => console.error(err))
|
||||
c?.on('data', (data) => this.processData(c, data as RTCData))
|
||||
c?.on('error', err => console.error(err))
|
||||
c?.on('data', data => this.processData(c, data as RTCData))
|
||||
c?.on('close', () => this.onConnectionClose(c.peer))
|
||||
}
|
||||
|
||||
protected connectRTC(): void {
|
||||
this.peer = new Peer()
|
||||
this.peer.on('open', (id) => {
|
||||
console.log('[Components-Event] Slave Peer ID:', id)
|
||||
this.vhub.send('RegisterRTC', false, id)
|
||||
})
|
||||
this.peer.on('error', err => console.error('[Components-Event] Slave Peer Error:', err))
|
||||
}
|
||||
|
||||
protected processData(conn: DataConnection, data: RTCData): void {
|
||||
if (data.Key === 'VTsuru.RTCEvent.On') {
|
||||
if (!this.handledEvents[conn.peer]) {
|
||||
this.handledEvents[conn.peer] = []
|
||||
}
|
||||
if (!this.handledEvents[conn.peer].includes(data.Data)) {
|
||||
this.handledEvents[conn.peer].push(data.Data)
|
||||
}
|
||||
} else if (data.Key === 'VTsuru.RTCEvent.Off') {
|
||||
if (this.handledEvents[conn.peer]) {
|
||||
const index = this.handledEvents[conn.peer].indexOf(data.Data)
|
||||
if (index > -1) {
|
||||
this.handledEvents[conn.peer].splice(index, 1)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const eventName = data.Key.toLowerCase()
|
||||
if (this.events[eventName]) {
|
||||
this.events[eventName].forEach(listener => listener(data.Data))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Init() {
|
||||
await super.Init()
|
||||
this.vhub?.on('MasterOnline', (data: string) => this.connectToMaster(data))
|
||||
this.vhub?.on('MasterOnline', (...args: unknown[]) => {
|
||||
const data = args[0] as string
|
||||
this.connectToMaster(data)
|
||||
})
|
||||
setTimeout(() => {
|
||||
this.connectToAllMaster()
|
||||
}, 500)
|
||||
@@ -193,26 +185,55 @@ export class MasterRTCClient extends BaseRTCClient {
|
||||
constructor(user: string, pass: string) {
|
||||
super(user, pass)
|
||||
}
|
||||
|
||||
type: 'master' = 'master' as const
|
||||
|
||||
public connectRTC() {
|
||||
super.connectRTC()
|
||||
protected connectRTC(): void {
|
||||
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
|
||||
`[Components-Event] <${this.connections.length}> Slave 上线: ${
|
||||
conn.peer}`,
|
||||
)
|
||||
})
|
||||
conn.on('data', (d) => this.processData(conn, d as RTCData))
|
||||
conn.on('error', (err) => console.error(err))
|
||||
conn.on('data', d => this.processData(conn, d as RTCData))
|
||||
conn.on('error', err => console.error(err))
|
||||
conn.on('close', () => this.onConnectionClose(conn.peer))
|
||||
})
|
||||
|
||||
this.peer = new Peer()
|
||||
this.peer.on('open', (id) => {
|
||||
console.log('[Components-Event] Master Peer ID:', id)
|
||||
this.vhub.send('RegisterRTC', true, id)
|
||||
})
|
||||
this.peer.on('error', err => console.error('[Components-Event] Master Peer Error:', err))
|
||||
}
|
||||
|
||||
public Init() {
|
||||
protected processData(conn: DataConnection, data: RTCData): void {
|
||||
if (data.Key === 'VTsuru.RTCEvent.On') {
|
||||
if (!this.handledEvents[conn.peer]) {
|
||||
this.handledEvents[conn.peer] = []
|
||||
}
|
||||
if (!this.handledEvents[conn.peer].includes(data.Data)) {
|
||||
this.handledEvents[conn.peer].push(data.Data)
|
||||
}
|
||||
} else if (data.Key === 'VTsuru.RTCEvent.Off') {
|
||||
if (this.handledEvents[conn.peer]) {
|
||||
const index = this.handledEvents[conn.peer].indexOf(data.Data)
|
||||
if (index > -1) {
|
||||
this.handledEvents[conn.peer].splice(index, 1)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (this.handledEvents[conn.peer]?.includes(data.Key.toLowerCase())) {
|
||||
conn.send(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Init() {
|
||||
return super.Init()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user