-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
32 lines (27 loc) · 865 Bytes
/
index.js
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
const WebSocket = require('websocket').client
let cachedData = []
const wsUri = 'wss://rtw.uol.com/sub?id=app-esporte-placar-futebol-live-worldcup'
function initializeWebSocket (wsUri) {
const websocket = new WebSocket()
websocket.on('connect', (connection) => {
connection.on('message', (message) => {
if (message.type === 'utf8') {
let jsonData = JSON.parse(message.utf8Data).subchannels
cachedData = Object.keys(jsonData).map(k => jsonData[k])
}
})
connection.on('error', (error) => {
console.log('Connection Error: ' + error.toString())
})
connection.on('close', () => {
initializeWebSocket(wsUri)
})
})
websocket.connect(wsUri)
return websocket
}
initializeWebSocket(wsUri)
module.exports = (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
return cachedData
}