[英]Javascript Firebase real time database onDisconnect
So when i'm closing my browser i would like to add所以当我关闭浏览器时,我想添加
const timestamp = Date.now();
const username = "MyUser";
const message = "I have logged out";
db.ref("messages/222222222222222" + timestamp).set({
username,
message,
});
I have tried:我努力了:
const presenceRef = firebase.database().ref("disconnectmessage");
presenceRef.onDisconnect().set("I disconnected!");
But i can't add my code in to it:但我无法将我的代码添加到其中:
db.ref("messages/222222222222222" + timestamp).set({
username,
message,
});
How can i add my own event listener to onDisconnect?如何将我自己的事件侦听器添加到 onDisconnect?
This not working for me for some reason:由于某种原因,这对我不起作用:
var connectedRef = firebase.database().ref(".info/connected");
connectedRef.on("value", (snap) => {
if (snap.val() === true) {
console.log("connected");
} else {
//My code here is not working
// When i'm closing the broswer it doesn't add it to
// messages
}
});
You should be able to just do this:你应该能够做到这一点:
db.ref("messages/222222222222222" + timestamp).onDisconnect().set({
username,
message,
});
Your last snippet will not work, because by the time the .info/connected
gets set to false, the connection to the server has been lost already so you can't write to the remote database anymore您的最后一个片段将不起作用,因为当
.info/connected
设置为 false 时,与服务器的连接已经丢失,因此您无法再写入远程数据库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.