I want to display a message when my user is offline and disable all editing features, then reenable them when the user comes back online. Right now I'm using navigator.onLine which doesn't actually detect connectivity very well in some cases.

What's the best way to determine whether the Drive Realtime API is connected?

有帮助吗?

解决方案

We've had success with using the Save State Change event on the realtime document to determine the offline/online status of the connection. This means that you wont know you're offline until you try to make a change to the doc, but you could add a heartbeat doc change if knowing the status is critical.

See https://developers.google.com/drive/realtime/reference/gapi.drive.realtime.DocumentSaveStateChangedEvent for the event info.

其他提示

One addition to Grant's answer which may be important to consider, is that 'isSaving' can NOT be relied on to determine that a new realtime document or an update has been fully saved to the servers.

To make sure that a disconnection or page closure is safe, you also have to check that document.saveDelay has dropped to zero.

This isn't currently documented by Google, but can be verified by looking at network traffic when uploading, for instance, an in-memory model:

var saving = false;
var ev = gapi.drive.realtime.EventType.DOCUMENT_SAVE_STATE_CHANGED;

document.addEventListener(ev, function(e) {
    saving = e.isSaving;
});

document.saveAs(fileId);

var h = window.setInterval(function() {
    if (!saving && !document.saveDelay) {
        window.clearInterval(h);
        console.log("Good to go");
    }
}, 1000);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top