-->

How to replace auto generated easyrtc id with your

2019-08-05 19:31发布

问题:

I am developing one application using easyrtc tool with wavemaker tool.For a new user easy rtc provides automatically created easyrtc id. In the chat window the random id are shown..i want to replace these ids with applications username..

I have find one solution where we have to set easyrtc.setUsername("") in client js file before calling easyrtc.connect function.. But this not solves the problem...

any help would be appriciated

回答1:

Their is no easy way to solve this. However, it is possible using a mixture of server-side and client-side events to pass/receive user metadata when connected/disconnected. Here is a simple way to achieve this:

  1. When a client connects to the server send user metadata via sendServerMessage on the connected event listener via client-side library. The server then receives the message from the client and stores the metadata about the user with that particular easyrtcid in a central location (ex. redis). The message sent to the server can be a json object with user metadata in a structured format. See details on connecting and sending a message to the server here: easyRTC Client-Side Documentation

  2. When a client disconnects from the server remove their information from the data store using the onDisconnect event on the server side. This event provides a connectionObj which includes the easyrtcid of the user who disconnected. Use this identifier to remove the user from the datastore. You could also call generateRoomList() on the connectionObj to remove the user by easyrtcid and room from your datastore. You can read about the connection object here: connectionObj easyRTC documentation

Here is some example code of how to do this:

// Client-Side Javascript Code (Step 1)
easyrtc.connect('easyrtc.appname', function(easyrtcid){

   // When we are connected we tell the server who we are by sending a message
   // with our user metadata. This way we can store it so other users can
   // access it.
   easyrtc.sendServerMessage('newConnection', {name: 'John Smith'},
     function(type, data){

       // Message Was Successfully Sent to Server and a response was received
       // with a the data available in the (data) variable.

     }, function(code, message) {

       // Something went wrong with sending the message... To be safe you 
       // could disconnect the client so you don't end up with an orphaned
       // user with no metadata.

     }
}, function(code, message) {
  // Unable to connect! Notify the user something went wrong...
}

Here is how things would work on the server-side (node.js)

// Server-Side Javascript Code (Step 2)
easyrtc.events.on('disconnect', function(connectionObj, next){
  connectionObj.generateRoomList(function(err, rooms){
      for (room in rooms) {
        // Remove the client from any data storage by room if needed
        // Use "room" for room identifier and connectionObj.getEasyrtcid() to 
        // get the easyrtcid for the disconnected user.
      }
  });

  // Send all other message types to the default handler. DO NOT SKIP THIS!
  // If this is not in place then no other handlers will be called for the 
  // event. The client-side occupancy changed event depends on this.
  easyrtc.events.emitDefault("disconnect", connectionObj, next);

});

Redis is a great way to keep track of the users connected if using rooms. You can use an hash style object with the first key being the room and each sub key/value being the users easyrtcid with a JSON hash of the metadata stored as it's value. It would have to be serialized to a string FYI and de-serialized on the lookup but this is simple using Javascript using the JSON.stringify and JSON.parse methods.

To detect occupancy changes in your application you could add a event listener to the easyrtc.setRoomOccupantListener method on the client-side and then when this event is fired send another message to the server to get all the users connected to it from the datastore.You would have to listen for a separate message on the server-side and return the users in the store deserialized back to the client. However, depending on your application this may or may not be needed.



回答2:

Now, you can do it easyer, use this function:

easyrtc.idToName(easyrtcid)


标签: easyrtc