text Express 4和Socket.io:将socket.io传递给路由。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text Express 4和Socket.io:将socket.io传递给路由。相关的知识,希望对你有一定的参考价值。

///https://stackoverflow.com/questions/29334800/express-js-4-and-sockets-with-express-router
///USE THIS



var app = express();
app.io = require('socket.io')();

var routes = require('./routes/index')(app.io);

app.use('/', routes);
//Normal code here

//then at the bottom:

module.exports = function (io) {
    //Socket.IO
    io.on('connection', function (socket) {
        console.log('User has connected to Index');
        //ON Events
        socket.on('admin', function () {
            console.log('Successful Socket Test');
        });

        //End ON Events
    });
    return router;
};
/**
 * Create HTTP server
 */

var server = http.createServer(app);
app.io.attach(server);
/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);


/**
 * Normalize a port into a number, string, or false.
 */

以上是关于text Express 4和Socket.io:将socket.io传递给路由。的主要内容,如果未能解决你的问题,请参考以下文章

Socket.io 和 Express 4

socket.io 和 express 4 个会话

Express 4.0 中的 Heroku socket.io 示例错误

Socket.io 1.0 + express 4.2 = 无套接字连接

找不到Node.js /socket.io/socket.io.js express 4.0

如何在 Express 4 路由中使用 socket.io 向连接的套接字发出事件?