Reputation: 105
I have a page that upon receiving a websocket
, it does something via javascript
. I want this page to reload after some time only if a websocket
wasn't received between the last time and the time I want to.
<script>
if ("WebSocket" in window) {
var ws = new WebSocket('ws://192.168.33.1:5000');
var variableSpan = document.getElementById('variable');
ws.onmessage = function (event) {
var message = JSON.parse(event.data);
console.log(message);
jQuery('#variable').empty();
$(function () {
$.get(message.variable, function (data) {
$("#variable").append(data);
});
});
setTimeout(function () {
window.location.reload(); // you can pass true to reload function to ignore the client cache and reload from the server
}, 10000);
}
}
</script>
I tried something like this, but it doesn't seem to work. I would appreciate some help :)
Upvotes: 4
Views: 2187
Reputation: 41
Reload after checking webSocket State
function onWebSocketClosed(){
if (webSocket.readyState != WebSocket.OPEN) {
try{
console.error("webSocket is not open: " + webSocket.readyState +"Reloading Page");
window.location.reload()
}
catch (error) {
console.log('onWebSocketClosed : {0}' , error)
}
}
}
Upvotes: 1