如何在1v1聊天中链接用户ID?

时间:2018-06-30 02:03:16

标签: javascript firebase-realtime-database

对于1v1聊天,我正在尝试链接我的用户ID。我已经与数据库建立了全局聊天,如下图所示。
enter image description here

此代码允许我在firebase数据库上通过userid搜索,然后拉起两个用户将进行私人聊天的私人消息html文件。

            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';
                } 
            });
        });

我倾向于一对一的演讲 var pmChat = database.ref('chat/' + userId);

我将创建一个新引用,然后添加第一个用户ID,然后添加第二个用户ID甚至显示名。

如何获取要搜索的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);
            if (searchId.value === snapshot.val().userId) {
                var currentUser = database.ref('chat').child(userId).child(searchId.value);
                var otherUser = database.ref('chat').child(searchId.value).child(userId);

                messageForm.addEventListener("submit", function(e) {
                    e.preventDefault();

                    var user = auth.currentUser;
                    var userId = user.uid;
                    if (user.emailVerified) {
                        // Get the message the user entered
                        var message = messageInput.value;

                        var myPassword = "11111";
                        var myString = CryptoJS.AES.encrypt(message, myPassword);

                        // Decrypt the after, user enters the key
                        var decrypt = document.getElementById('decrypt')

                        // Event listener takes input
                        // Allows user to plug in the key
                        // function will decrypt the message
                        decrypt.addEventListener('click', function(e) {
                            e.preventDefault();
                            // Allows user to input there encryption password 
                            var pass = document.getElementById('password').value;

                            if (pass === myPassword) {
                                var decrypted = CryptoJS.AES.decrypt(myString, myPassword);
                                document.getElementById("demo3").innerHTML = decrypted.toString(CryptoJS.enc.Utf8);
                            }
                        });

                        // Create a new message and add it to the list.  
                        if (userId) {

                            currentUser.push({
                                    displayName: displayName,
                                    userId: userId,
                                    pic: userPic,
                                    text: myString.toString(),
                                    timestamp: new Date().getTime(), // unix timestamp in milliseconds

                                })
                                .then(function() {
                                    messageStuff.value = "";

                                })
                                .catch(function(error) {
                                    windows.alert("Your message was not sent!");
                                    messageStuff;
                                });
                        } else {
                            otherUser.push({
                                    displayName: displayName,
                                    userId: userId,
                                    pic: userPic,
                                    text: myString.toString(),
                                    timestamp: new Date().getTime(), // unix timestamp in milliseconds

                                })
                                .then(function() {
                                    messageStuff.value = "";

                                })
                                .catch(function(error) {
                                    windows.alert("Your message was not sent!");
                                    messageStuff;
                                });

我觉得我没有抓住另一个用户ID,因为它只是文本框的字符串值。

var currentUser = database.ref('chat').child(userId).child(searchId.value);
 var otherUser = database.ref('chat').child(searchId.value).child(userId);

这就是我在Firebase数据库中创建新引用的方式。

0 个答案:

没有答案