Add backup servers

This commit is contained in:
the1812 2020-03-12 22:04:48 +08:00
parent 743982d5d0
commit c123a324cd
8 changed files with 36 additions and 15 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -127,7 +127,7 @@
"image-viewer.min.html": "763742E79923A7918F281AEBB3CEE76FE2A0AE94CACE325AE4BEE1AED451DAEA",
"index.min.html": "94B83D9EBB9005C1286A7E0759A7683932F8DADA20D07E5E9D8FF867B04D4B95",
"keymap.min.js": "97F968FE6B8719E4A22FC8A23DD6369548BDABE7574BE1539217DD52CA297884",
"live-socket.min.js": "FB799DA10059CC7EF934F6C678EE91729A0FB00D5D1EC24D7FBFC75FD8FA12FB",
"live-socket.min.js": "8C59B0458D14FBA9233847F18D82F3DAA558A0DA432B63409050C1A27D871C51",
"magic-grid.min.js": "30BA27115FAC84B018A5A8C64031939DF1E324AE199F2F3675925ECF427CBFFC",
"mdi.min.js": "8A22F2F37F88F74FC07CE2FECA7CE135182058BE24409BAF3DEF0D5845B0BE1A",
"medal-helper.min.css": "6AF13675048FEBFB7E56ABE7462346BB53D22DE21DC31A34211872FCF6FA2A3B",

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -138,6 +138,8 @@ export class LiveSocket extends EventTarget {
webSocket: WebSocket
retryInterval = 200
autoRetry = true
servers: string[] = ['broadcastlv.chat.bilibili.com']
selectedServer = ''
private liveTime = new LiveTimeExtractor()
private bufferHelper = new SocketBufferHelper()
private stopRequested = false
@ -147,26 +149,45 @@ export class LiveSocket extends EventTarget {
window.addEventListener('unload', () => this.stop())
}
heartBeat() {
this.webSocket.send(this.bufferHelper.encode('', 'heartBeat'))
if (this.webSocket.readyState === WebSocket.OPEN) {
this.webSocket.send(this.bufferHelper.encode('', 'heartBeat'))
} else {
this.stop()
this.restart()
}
}
restart() {
this.dispatchEvent(new CustomEvent('restart'))
if (!this.stopRequested && this.autoRetry) {
console.log(`Live Socket: unexpected disconnect, retry in ${this.retryInterval}ms`)
const index = this.servers.indexOf(this.selectedServer)
if (index < this.servers.length - 1) { // 尝试下一个服务器
this.selectedServer = this.servers[index + 1]
} else { // 所有服务器用尽, 从头再来
[this.selectedServer] = this.servers
}
console.log(`Live Socket: server changed to`, this.selectedServer)
setTimeout(() => this.start(), this.retryInterval)
}
}
async start() {
const roomConfig = await Ajax.getJson(`https://api.live.bilibili.com/room/v1/Danmu/getConf?room_id=${this.roomID}&platform=pc&player=web`)
const hostServers: any[] = _.get(roomConfig, 'data.host_server_list', [])
let server = 'broadcastlv.chat.bilibili.com'
if (hostServers.length > 0) {
server = hostServers[0].host
const hostServers: { host: string }[] = _.get(roomConfig, 'data.host_server_list', [])
// let server = 'broadcastlv.chat.bilibili.com'
// if (hostServers.length > 0) {
// server = hostServers[0].host
// }
this.servers = [...new Set([...this.servers, ...hostServers.map(it => it.host)])]
if (this.selectedServer === '') { // 首次启动
[this.selectedServer] = this.servers
console.log('Initial server:', this.selectedServer)
}
if (this.webSocket && (this.webSocket.readyState === WebSocket.CONNECTING || this.webSocket.readyState === WebSocket.OPEN)) {
this.stop()
}
this.webSocket = new WebSocket(`wss://${server}/sub`)
this.webSocket = new WebSocket(`wss://${this.selectedServer}/sub`)
this.stopRequested = false
this.dispatchEvent(new CustomEvent('start', {
detail: this.webSocket
}))
@ -252,7 +273,7 @@ export class LiveSocket extends EventTarget {
if (this.heartBeatTimer) {
clearInterval(this.heartBeatTimer)
}
if (this.webSocket) {
if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) {
this.webSocket.close()
}
}