我正在使用带有 OpenTok 的 node.js 服务器。我正在两个用户之间创建私人聊天,并希望创建一个会话和两个令牌。问题是 generate_token 函数只创建一个令牌,然后将其发送给两个用户。在这段代码中,我想将单个令牌发送给每个用户。
我的代码如下:
var sessionId;
var sourceUser=data.from;
var targetUser=data.to;
if(data.messageType=='accepted'){
var location = '127.0.0.1'; // use an IP or 'localhost'
ot.create_session(location, function(result){
sessionId = result;
});
console.log('chat has been accepted by ' + data.from + '. I can start the OpenTok session now with ' +data.to + '.');
var data= {'to':sourceUser, 'message':{'token': ot.generate_token({session_id:sessionId, 'connection_data': Math.floor(Math.random()*999999),
'role': "publisher"}), 'sessionID': sessionId, 'apikey':key}, 'from': targetUser, 'messageType':'tokenInfo'}
connectedUsersObject[sourceUser].emit('private message', tstamp(), socket.nickname, data);
console.log(sourceUser + '\'s token is: ' + data.message.token + '. The sessionID is: ' + data.message.sessionID + '. The API key is: ' + data.message.apikey);
console.log('connection data is: ' + data.message.connection_data);
var data= {'to':targetUser, 'message':{'token': ot.generate_token({session_id:sessionId, 'connection_data': Math.floor(Math.random()*999999),
'role': "publisher"}), 'sessionID': sessionId, 'apikey':key}, 'from': sourceUser, 'messageType':'tokenInfo'}
connectedUsersObject[targetUser].emit('private message', tstamp(), socket.nickname, data);
console.log(targetUser + '\'s token is: ' + data.message.token + '. The sessionID is: ' + data.message.sessionID + '. The API key is: ' + data.message.apikey);
console.log('connection data is: ' + data.message.connection_data);
整个服务器代码位于: http: //pastebin.com/uvWfKSWp