簡體   English   中英

“信息-socket.io啟動”丟失

[英]“Info - socket.io started” Missing

我正在嘗試向我的應用程序添加套接字,並且通常情況下如果一切順利,我應該會看到類似的內容:

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());
  })
}

我在這里做錯了什么?

原來這只是一個版本問題:

諸如“ info-socket.io starts”之類的信息消息不會在1.0版本中顯示,而僅在0. *版本中顯示。 因此,如果您正在運行0. * socket.io代碼,同時在package.json中具有1.0 socket.io版本,則需要做的是:

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

    npm在此處安裝socket.io@insert.your.version。

  • 或保持最新的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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM