我正在开发一款游戏,将两个玩家与Firebase实时数据连接起来。 但我想要一种方法,如果失去与互联网的连接,可以删除玩家数据。
这可以通过 OnDisconnect 方法进行更新。
弗兰克的这个原始答案最初描述了有关OnT断开连接方法的更多详细信息。
在此处阅读有关界面的更多信息。
您可以使用此 Firebase 示例设置"已连接状态",该示例使用实时数据库和云功能以及 OnDisconnect 来显示用户的状态。从示例中提取:
var userStatusDatabaseRef = firebase.database().ref('/status/' + uid);
firebase.database().ref('.info/connected').on('value', function(snapshot) {
// If we're not currently connected, don't do anything.
if (snapshot.val() == false) {
return;
};
// If we are currently connected, then use the 'onDisconnect()'
// method to add a set which will only trigger once this
// client has disconnected by closing the app,
// losing internet, or any other means.
userStatusDatabaseRef.onDisconnect().set(isOfflineForDatabase).then(function() {
// The promise returned from .onDisconnect().set() will
// resolve as soon as the server acknowledges the onDisconnect()
// request, NOT once we've actually disconnected:
// https://firebase.google.com/docs/reference/js/firebase.database.OnDisconnect
// We can now safely set ourselves as 'online' knowing that the
// server will mark us as offline once we lose connection.
userStatusDatabaseRef.set(isOnlineForDatabase);
});
});