2015-05-25 55 views
2

我想一個插座添加到我的應用程序,通常,如果一切順利的話,我應該看到類似這樣的東西:「信息 - 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()); 
    }) 
} 

我在做什麼錯在這裏?

回答

0

事實證明,這只是一個版本的問題:

像信息的消息「信息 - socket.io開始」不1.0版本顯示,僅在0 *版本。因此,如果你同時在你的的package.json 1.0 socket.io版本運行的0 * socket.io代碼,你需要做的是:

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

    NPM安裝[email protected]

  • 或保留了最新版本socket.io並相應修改你的代碼。像例如:

    io.set( '授權',函數(handshakeData,回調){ 變爲:

    io.use(功能(插座,下一個){ VAR handshakeData = socket.request;

我推薦這篇文章如何遷移到1.0: socket.io/docs/migrating-from-0-9/