我正在使用easyrtc工具和tool.For开发一个应用程序,一个新用户easyrtc提供了自动创建的easyrtc id。在聊天窗口中显示了随机id .我想用应用程序用户名.替换这些id。
我找到了一个解决方案,在调用easyrtc.setUsername函数之前,我们必须在客户端js文件中设置easyrtc.connect (“”)。但这不能解决问题..。
任何帮助都会成为学徒
发布于 2015-08-04 06:43:50
他们不是解决这个问题的简单方法。但是,在连接/断开连接时,可以使用服务器端和客户端事件的混合传递/接收用户元数据。以下是实现这一目标的简单方法:
下面是一些如何执行此操作的示例代码:
// 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)的工作方式
// 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并在查找时反序列化,但是使用使用JSON.stringify和JSON.parse方法的Javascript很简单。
要检测应用程序中的占用率变化,您可以在客户端向easyrtc.setRoomOccupantListener方法添加一个事件侦听器,然后当该事件被触发时,向服务器发送另一条消息,以使从datastore.You连接到它的所有用户都必须侦听服务器端的单独消息,并将存储中反序列化的用户返回给客户端。但是,这可能需要也可能不需要,这取决于您的应用程序。
发布于 2017-01-09 13:58:34
现在,您可以轻松地使用以下功能:
easyrtc.idToName(easyrtcid)https://stackoverflow.com/questions/30299728
复制相似问题