Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firebase一对一聊天_Javascript_Firebase_Firebase Realtime Database_Firebase Authentication - Fatal编程技术网

Javascript Firebase一对一聊天

Javascript Firebase一对一聊天,javascript,firebase,firebase-realtime-database,firebase-authentication,Javascript,Firebase,Firebase Realtime Database,Firebase Authentication,我正在尝试在全球firebase聊天中创建一对一聊天。我想知道我该怎么做?我现在正在做的是,我创建了另一个HTML文件,这是私人聊天的地方,我正在按用户id进行搜索 用这段代码 oneOnone.addEventListener('click', function(e){ // Attach an asynchronous callback to read the data at our posts reference re

我正在尝试在全球firebase聊天中创建一对一聊天。我想知道我该怎么做?我现在正在做的是,我创建了另一个HTML文件,这是私人聊天的地方,我正在按用户id进行搜索 用这段代码

            oneOnone.addEventListener('click', function(e){
            // Attach an asynchronous callback to read the data at our posts reference
            ref.on("value", function (snapshot) {}, function (errorObject) {
                console.log("The read failed: " + errorObject.code);
            });
            ref.orderByChild("userId").on("child_added", function (snapshot) {
               console.log(snapshot.val().userId);
               console.log(searchId);
                if(searchId.value === snapshot.val().userId){
                    window.location.href = 'privatemessage.html';
                } 
            });
        });
然后,这将导入privatemessage.html文件,所有聊天都将在该文件中进行。我一直觉得我的数据库可能不正确。

firebase上的数据库仍然像在全球聊天中一样注册用户,而不是一对一聊天。我只是完全搞不懂如何确保聊天只在两个人之间进行。如果有人可以推荐一个指南,请就1v1聊天如何工作给出一个完整的解释,我们将不胜感激。是的,我已经看过文档了,它并没有解释如何进行1v1聊天

编辑

所以我把我的数据库改成了这个

因此,我所依赖的是这样的一对一谈话
var-pmChat=database.ref('chat/'+userId)


基本上是创建一个新引用,然后链接用户id,但现在,我如何链接其他用户的用户id?

您可以将当前用户id链接到其他用户id,如下所示:

database.ref('chat').child(currentUserId).child(otherUserId).child(message);
database.ref('chat').child(otherUserId).child(currentUserId).child(message);

您需要在数据库中创建这些文件室。所以
/chattrooms/RoomName1
,然后在下面加上消息。这样,您就可以根据每个房间的参与者来确保对每个房间的访问。有关构造此数据的好方法,请参阅