“信息 - socket.io开始”失踪

时间:2015-05-25 09:58:35

标签: node.js socket.io

我正在尝试为我的应用添加套接字,通常如果一切顺利,我应该会看到类似的内容:

info  - socket.io started
Listening { address: '0.0.0.0', family: 'IPv4', port: 443 }

然而,我所看到的只是:

Listening { address: '0.0.0.0', family: 'IPv4', port: 443 }

这只能意味着套接字无法正常工作。这是我的套接字代码:

var sockets = require('socket.io').listen(server).of('/elyes');

sockets.use(function (socket, next) {
  // Read cookies from handshake headers
  var handshakeData = socket.request;
  var cookies = cookie.parse(handshakeData.headers.cookie);
  // We're now able to retrieve session ID
  var sessionID;
  if (cookies['connect.sid']) {
    sessionID = connect.utils.parseSignedCookie(cookies['connect.sid'], sessionSecret);
  }
  // No session? Refuse connection
  if (!sessionID) {
    callback('No session', false);
  } else {
    // Store session ID in handshake data, we'll use it later to associate
    // session with open sockets
    handshakeData.sessionID = sessionID;
    // On récupère la session utilisateur, et on en extrait son username
    sessionStore.get(sessionID, function (err, session) {
      if (!err && session && session.username) {
        // On stocke ce username dans les données de l'authentification, pour réutilisation directe plus tard
        handshakeData.username = session.username;
        handshakeData['pwdHash']=session.pwdHash;
        // OK, on accepte la connexion
        callback(null, true);
      } else {
        // Session incomplète, ou non trouvée
        callback(err || 'User not authenticated', false);
      }
    });
  }
});

var connections = {};
sockets.on('connection', function (socket) { // New client

    //========================================================= 
    socket.handshake['socket']=socket;
    socket.handshake['sockets']=sockets;
    connectedClients[socket.handshake.pwdHash]=socket.handshake
    //console.log(connectedClients);
    //=========================================================

    broadcastToClients=sockets;
    if (connectedBoxs[socket.handshake.pwdHash]!=undefined) {
        //console.log(sockets['checkConnectivityInterval']);
        if (connectedClients[socket.handshake.pwdHash].checkConnectivityInterval!=undefined) 
            clearInterval(connectedClients[socket.handshake.pwdHash].checkConnectivityInterval);
            if (connectedBoxs[socket.handshake.pwdHash].stream!=undefined) 
            connectedClients[socket.handshake.pwdHash]['checkConnectivityInterval']=setInterval(function(){connectedBoxs[socket.handshake.pwdHash].stream.push('PresenceCheck\n')
                                                                                                            }, 2000); 
        //console.log(sockets['checkConnectivityInterval']);
    }
    else{
        sockets.emit('isBoxOnline', false);
    }

    //numClient++;
    //connectedClients[numClient]=socket;
  //socket.emit('message', Date.now());
  var sessionID = socket.handshake.sessionID; // Store session ID from handshake
  //console.log(socket.handshake);
  // this is required if we want to access this data when user leaves, as handshake is
  // not available in "disconnect" event.
  var username = socket.handshake.username; // Same here, to allow event "bye" with username
  if ('undefined' == typeof connectedClients[sessionID]) {
    connectedClients[sessionID] = { "length": 0 };
    // First connection
    sockets.emit('join', username, Date.now());
  }
  // Add connection to pool
  connectedClients[sessionID][socket.id] = socket;
  connectedClients[sessionID].length ++;


  // When user leaves
    socket.on('disconnect', function () {
       // Is this socket associated to user session ?
       var userConnections = connectedClients[sessionID];
       if (userConnections.length && userConnections[socket.id]) {
         // Forget this socket
         userConnections.length --;
         delete userConnections[socket.id];
       }
       if (userConnections.length == 0) {
        console.log("================================");
         // No more active sockets for this user: say bye
           if (connectedBoxs[socket.handshake.pwdHash]!=undefined) {
if (connectedClients[socket.handshake.pwdHash].checkConnectivityInterval!=undefined) 
            clearInterval(connectedClients[socket.handshake.pwdHash].checkConnectivityInterval);  
           }
         delete connectedClients[socket.handshake.pwdHash];
         sockets.emit('bye', username, Date.now());
       }
     });

    socket.on('isBoxOnline', function(message){
        if (connectedBoxs[socket.handshake.pwdHash]!=undefined) {
            socket.emit('isBoxOnline', true);
        }
        else{
            socket.emit('isBoxOnline', false);
        }
    });

    socket.on('getConnectedDevices', function(message){
        if (connectedBoxs[socket.handshake.pwdHash]!=undefined) {
            connectedBoxs[socket.handshake.pwdHash].stream.push('ConnectedDevices\n');
            console.log(['Here GetBoxStatus Function !'].join("\n").green);
        }    
    });

    socket.on('Mount', function(message){
        if (connectedBoxs[socket.handshake.pwdHash]!=undefined) {
            connectedBoxs[socket.handshake.pwdHash].stream.push('Mount'+message+'\n');
        }    
    });

    socket.on('Umount', function(message){
        if (connectedBoxs[socket.handshake.pwdHash]!=undefined) {
            connectedBoxs[socket.handshake.pwdHash].stream.push('Umount'+message+'\n');
        }
    });

    socket.on('RqForTree', function(message){
        if (connectedBoxs[socket.handshake.pwdHash]!=undefined) {
            connectedBoxs[socket.handshake.pwdHash].stream.push('RqForTree'+message+'\n');
        }
    });

    socket.on('ls', function(message){
        if (connectedBoxs[socket.handshake.pwdHash]!=undefined) {
            connectedBoxs[socket.handshake.pwdHash].stream.push('ls'+message+'\n');
        }
    });

    socket.on('Download', function(message){
        if (connectedBoxs[socket.handshake.pwdHash]!=undefined) {
            connectedBoxs[socket.handshake.pwdHash].stream.push('Download'+message+'\n');
        }
    });

    socket.on('getBoxStatus', function (message) {
     if (connectedBoxs[socket.handshake.pwdHash]!=undefined) {
        connectedBoxs[socket.handshake.pwdHash].stream.push(['Here GetBoxStatus Function !\n'].join("\n").green);
        console.log(['Here GetBoxStatus Function !'].join("\n").green);
     }
    });

    socket.on('message1', function (message) {
        if (connectedBoxs[socket.handshake.pwdHash]!=undefined) {
            connectedBoxs[socket.handshake.pwdHash].stream.push("message1\n");
            //console.log("//==============================================");
            //console.log(connectedBoxs[socket.handshake.pwdHash].stream);
            //console.log("//==============================================");
        }
    });

  // New message from client = "write" event
  socket.on('write', function (message) {
    sockets.emit('message', username, message, Date.now());
  });
});

if (!module.parent) {
  server.listen(port, function () {
    console.log('Listening', this.address());
  })
}

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

事实证明这只是一个版本问题:

信息消息,如" info - socket.io已启动"不会出现在1.0版本中,只有0. *版本。因此,如果您在package.json中拥有1.0 socket.io版本时正在运行0. * socket.io代码,那么您需要做的是:

  • 最快的方法是在package.json中安装相同的版本:

    npm install socket.io@insert.your.version.here

  • 或者保留最新的socket.io版本并相应地编辑代码。比如:

    io.set(' authorization',function(handshakeData,callback){ 变为:

    io.use(function(socket,next){   var handshakeData = socket.request;

我推荐这篇关于如何迁移到1.0的文章: socket.io/docs/migrating-from-0-9 /