简体   繁体   中英

Refresh page for all online users

When a user clicks the save button, I want all users currently in the page to be notified, or even better (or worse in a second thought) to enforce the page to refresh.

So, I tried this in my script:

function save_match() {
    ...
    alert("Saved slot " + slot + "! :)");
    window.location.reload();
}

but the refresh, of course, happens only for the user that actually pressed the button, the other users have no idea about the save the other user just made.

Enforcing the reload of the page seems a bit unlikely to happen, so what about doing something via JavaScript that would notify the other online users for the save incident?

Possible related question .


So, what I want is this:

function save_match() {
    ...
    alert("Saved slot " + slot + "! :)");
    // do something here so that all online users are updated!!
}

You need to implement a Server Push . This technology has different working strategies like Long pooling, Heartbeat and WebSockets. Check them out.

Edit: It is indeed possible with any of the strategies mentioned.

For long pooling, you send a request to the server and wait indifinetly until the server 'pushes' back any updates if there are any. Check this post to see how to do it with jQuery.

Heartbeat is a different and more newtork-consuming tecnique in which you send a request every x seconds to see if there are any updates from the server and if so, you update your data. Check this example of how to implement it.

You can also see an example of how to implement a WebSockets javascript application in the MDN but consider the browser support for this feature before jumping on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM