Created
July 25, 2022 10:47
-
-
Save Bonemind/8b60e295caf4243b4234cfae6ca9ebca to your computer and use it in GitHub Desktop.
F1 telemetry signalr websocket connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm i axios signalr | |
const axios = require('axios'); | |
const ws = require('ws'); | |
async function negotiate() { | |
const hub = encodeURIComponent(JSON.stringify([{name:"Streaming"}])); | |
const url = `https://livetiming.formula1.com/signalr/negotiate?connectionData=${hub}&clientProtocol=1.5` | |
const resp = await axios.get(url); | |
return resp; | |
} | |
async function connectwss(token, cookie) { | |
const hub = encodeURIComponent(JSON.stringify([{name:"Streaming"}])); | |
const encodedToken = encodeURIComponent(token); | |
const url = `wss://livetiming.formula1.com/signalr/connect?clientProtocol=1.5&transport=webSockets&connectionToken=${encodedToken}&connectionData=${hub}` | |
const p = new Promise((res, rej) => { | |
const sock = new ws.WebSocket(url, {headers: { | |
'User-Agent': 'BestHTTP', | |
'Accept-Encoding': 'gzip,identity', | |
'Cookie': cookie | |
}}); | |
sock.on('open', ev => { | |
res(sock); | |
}); | |
sock.on('message', (data) => { | |
console.log('received %s', data); | |
}); | |
}); | |
return p | |
} | |
async function main() { | |
try { | |
const resp = await negotiate(); | |
console.log(resp.data); | |
console.log(resp.headers); | |
const sock = await connectwss(resp.data['ConnectionToken'], resp.headers['set-cookie']); | |
sock.send(JSON.stringify( | |
{ | |
"H": "Streaming", | |
"M": "Subscribe", | |
"A": [["TimingData", "Heartbeat"]], | |
"I": 1 | |
} | |
)); | |
} catch(e) { | |
console.error(e); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment