如何在easyrtc应用程序中用您的应用程序用户名替换自动生成的easyrtc id [英] How to replace auto generated easyrtc id with your applications username in easyrtc application

查看:137
本文介绍了如何在easyrtc应用程序中用您的应用程序用户名替换自动生成的easyrtc id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带wavemaker工具的easyrtc工具开发一个应用程序.对于新用户,easy rtc提供了自动创建的easyrtc id. 在聊天窗口中显示随机ID.我想用应用程序用户名替换这些ID.

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..

我找到了一种解决方案,在调用easyrtc.connect函数之前,我们必须在客户端js文件中设置easyrtc.setUsername("). 但这不能解决问题...

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...

任何帮助都将得到

推荐答案

解决这个问题并不容易.但是,可以在连接/断开连接时使用服务器端事件和客户端事件的混合来传递/接收用户元数据.这是实现此目的的简单方法:

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. 当客户端连接到服务器时,通过客户端库通过连接的事件侦听器上的sendServerMessage发送用户元数据.然后,服务器从客户端接收消息,并在中央位置(例如redis)中存储具有该特定easyrtcid的有关用户的元数据.发送到服务器的消息可以是具有用户元数据的json对象,具有结构化格式的用户元数据.在此处查看有关连接和向服务器发送消息的详细信息: easyRTC客户端文档

当客户端与服务器断开连接时,使用服务器端的onDisconnect事件从数据存储中删除其信息.此事件提供一个connectionObj,其中包括断开连接的用户的easyrtcid.使用此标识符可以从数据存储中删除用户.您也可以在connectionObj上调用generateRoomList(),通过easyrtcid和room从数据存储中删除用户.您可以在此处阅读有关连接对象的信息: connectionObj easyRTC文档

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...
}

这是服务器端(node.js)上的工作方式

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是跟踪使用房间的用户连接的好方法.您可以使用哈希样式对象,第一个键是房间,每个子键/值是用户easyrtcid,并使用元数据的JSON哈希作为值存储.它必须被序列化为字符串FYI并在查找时反序列化,但这使用Javascript使用JSON.stringify和JSON.parse方法很简单.

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.

要检测应用程序中的占用率变化,您可以在客户端的easyrtc.setRoomOccupantListener方法中添加事件侦听器,然后在触发该事件时向服务器发送另一条消息,以使所有用户从服务器连接到该服务器.数据存储区.您将不得不在服务器端侦听单独的消息,然后将存储区中的用户反序列化返回给客户端.但是,根据您的应用程序,可能需要也可能不需要.

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.

这篇关于如何在easyrtc应用程序中用您的应用程序用户名替换自动生成的easyrtc id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆