“信息 – 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()); }) } 

我在这里做错了什么?

原来这只是一个版本问题:

“info – socket.io started”等信息消息不会在1.0版本中显示,只能在0. *版本中显示。 所以,如果你在package.json中运行一个1.0 socket.io版本的socket.io代码,你需要做的是:

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

    npm安装socket.io@insert.your.version.here

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

    io.set('授权',函数(handshakeData,callback){成为:

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

我build议这篇文章如何迁移到1.0:socket.io/docs/migrating-from-0-9/