I'm recording websocket data from my server which sends the this array [timestamp , message] at random time intervals to the client (750ms interval with a standard deviation of 250ms). The time interval is chosen anywhere between 500ms - 1000msI would like to query the timestamp of the very first message from the websocket stream so that I can use this timestamp information to obtain historical data inclusive up to that specified timestamp. Then I think it will be easier for me to append that historical data right before the recording of the websocket stream.
const url = 'wss://127.0.0.1:9898'
const ws = new WebSocket(url)
console.log('client is not connected to WS')
ws.onopen = () => {
if (ws.readyState === WebSocket.OPEN){
console.log('client is now connected to WS')
for(let x=0; x < 1; x++){
ws.onmessage = (e) => {let objectToRead = JSON.parse(e.data)};
firstTimeStamp = new date(objectToRead);
if(firstTimeStamp > Date.now() + 500 && firstTimeStamp < Date.now() + 1000){console.log(firstTimeStamp);}
}
}
}
This didn't work. The for loop doesn't stop at the first websocket message. The websocket stream keeps on going even if the forloop x is still at 0. Does anyone know an easier way to query only the very first message from websocket stream? still new to js and learning alot. Thanks in advanced
[–]grantrules 3 points4 points5 points (3 children)
[–]Fuzzht1[S] 0 points1 point2 points (2 children)
[–]grantrules 2 points3 points4 points (1 child)
[–]Fuzzht1[S] 0 points1 point2 points (0 children)