Node.js变量被重置 - 未定义

时间:2012-08-10 01:31:32

标签: javascript node.js

我有一个由函数设置的全局变量(openTokSessionID)。再次运行该函数时,应检查是否设置了openTokSessionID。如果是,则应使用当前值。但是,它抱怨openTokSessionID未定义。我的服务器代码如下。谢谢你的帮助。

var http = require('http').createServer(handler), 
io   = require('socket.io').listen(http).set('log level', 1),
opentok = require('opentok'),
key = '',    // Replace with your API key  
secret = '';  // Replace with your API secret  
var ot = new opentok.OpenTokSDK(key,secret);
ot.setEnvironment("staging.tokbox.com");
//ot.setEnvironment("api.opentok.com"); //only for production
http.listen(8080);
console.log('Chatserver listening on port 8080');

var nicknames = {};
var log = {};
var connectedUsersObject={};
var privateSessionObject={};
var data;
var openTokSessionID='';


function handler(req, res) {
  res.writeHead(200);
  res.end();
}

////////////////////////////////////////////SOCKET.IO FUNCTIONS///////////////////////////////////////////////////////
io.sockets.on('connection', function (socket) {
    socket.on('private message', function(data) {
        console.log('OpenTok Session ID is: ....' + openTokSessionID);
        if(data.messageType=='openTokDemoRequest'){ //if new session, create unique session ID, add to current chat object
            console.log(data.messageType + ' sender: ' + data.from);

            var location = '127.0.0.1'; // use an IP or 'localhost' 
            console.log('message type is: "openTokDemoRequest". openTokSessionID is: ' + openTokSessionID);
            var messageRecipient=data.from;
            if(openTokSessionID==''){
                console.log('The session ID is: ' + openTokSessionID + '++++++++');openTokSessionID= ot.create_session(location, function(openTokSessionID){

                    var data= {'to':messageRecipient, 'message':{'token': ot.generate_token({'session_id':openTokSessionID, 'role': "publisher"}), 'sessionID': openTokSessionID, 'apikey':key}, 'from': 'openTok', 'messageType':'demoTokenInfo', 'privateSessionID':''};
                    console.log('NEW session id is: ' + openTokSessionID + '. token is: ' + data.message.token);
                //  privateMessageSend(data);
                //  sendToUser(data);
                    connectedUsersObject[messageRecipient].emit('private message', data);
                    console.log ('message recipient is: ' + messageRecipient);
                }); 
            }
            else{
                console.log('OpenTok Session ID is: ////' + openTokSessionID);
                var data= {'to':messageRecipient, 'message':{'token': ot.generate_token({'session_id':openTokSessionID, 'role': "publisher"}), 'sessionID': openTokSessionID, 'apikey':key}, 'from': 'openTok', 'messageType':'demoTokenInfo', 'privateSessionID':''};
                console.log('session id is: ' + openTokSessionID + '. token is: ' + data.message.token);
                connectedUsersObject[messageRecipient].emit('private message', data);
                console.log ('message recipient is: ' + messageRecipient);
            }   

        }
    });

1 个答案:

答案 0 :(得分:1)

在这里,您尝试使用异步调用的返回值,因此您将变量设置为undefined

openTokSessionID= ot.create_session(location, function(openTokSessionID){
...

因为你给你的回调参数和你的变量同名,所以它在回调中掩盖你的变量,所以它第一次看起来很好,但你的实际变量被破坏了。

您需要将其更改为:

ot.create_session(location, function(sessionID){
  openTokSessionID = sessionID;
...