This repository was archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (48 loc) · 1.32 KB
/
index.js
File metadata and controls
56 lines (48 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const axios = require('axios')
const env = require('process').env
const WebSocket = require('ws')
const config = {
host: env.HOST || '1.solariar.tech',
port: env.PORT || 32000,
isSecure: env.IS_SECURE || true
}
let counter = 0
function connect () {
const url = `${(config.isSecure ? 'wss' : 'ws')}://${config.host}:${config.port}/ws`
const ws = new WebSocket(url)
let timeout = 250
let connectTimeout
ws.onopen = () => {
console.log('Connected to ', ws.url)
timeout = 250
clearTimeout(connectTimeout)
}
ws.onclose = () => {
console.log('Disconnected. retrying...')
timeout = Math.min(30000, timeout * 2)
connectTimeout = setTimeout(() => {
if (!ws || ws.readyState === WebSocket.CLOSED) connect()
}, timeout)
}
ws.onerror = () => {
ws.close()
}
ws.onmessage = msg => {
if (msg.type === 'message') {
const data = JSON.parse(msg.data)
axios.get('https://api.live.bilibili.com/room/v1/Room/playUrl', {
params: data
})
.then(res => {
ws.send(JSON.stringify(res.data))
counter += 1
console.log('Successfully processed the request to CID ' + JSON.parse(msg.data).cid + '.')
console.log('Counter: ' + counter)
})
.catch(err => {
console.log(err)
})
}
}
}
connect()