How to send an alert message to a special online user with firebase

前端 未结 2 735
醉梦人生
醉梦人生 2020-12-09 06:46

I\'m trying to make a \"FourConnect\"-game with javascript. I want, that there is a list from all online users. This list I\'ve made with the example on the firebase site. N

相关标签:
2条回答
  • 2020-12-09 07:33

    From what I understand. You want a chat window in the game so that users logged to communicate among themselves, okay?

    Well, in its structure, you simply add something like:

    -Games
        -rooms
         -charOfRoomSpecific
             - users
                
                 + InmydEpSe5oZcLZUhfU
    
                  -InrLM6uxAsoOayOgFce
                         name: "Barbara"
                         status "away"
             
           - messages
               + InmyBlaBlae5oZcLPKSu
               -InmyBlaBlae5oZcLPKSu2
                    user: "Barbara"
                    say: "Hello man, will gamer!"
    
    
         + charOfRoomSpecific2
         + charOfRoomSpecific3
         ...
    

    So, for your users in the room can read the message simply:

    FirebaseRef.child ("rooms/charOfYourRoomSpecific/messages");
    

    And everyone who has access to this room will see in real time their conversations.

    0 讨论(0)
  • 2020-12-09 07:42

    In order to send a message to another user, you need that user to be monitoring a known location in your Firebase. Then when you want to send them a message, you simply modify that location in some way and they'll get a callback. Here's some pseudo code:

    var root = new Firebase(...);
    
    //On initialization start listening for messages
    root.child("users/inbound-messages").on("child_added", 
      function(newMessageSnapshot) {
        displaySomethingToTheUser(newMessageSnapshot.val());
        newMessageSnapshot.ref().remove();
      }
    );
    
    //Send a message to another user
    root.child(otherUserId).child("inbound-messages").push("Hi other user!");
    
    0 讨论(0)
提交回复
热议问题