我想要能够从我的Express路由文件向连接到Socket.io服务器的客户端发送数据。
我的app.js文件看起来像这样:
我的 test.js 文件:
我的app.js文件看起来像这样:
var express = require('express');
var app = express();
//routes
require('./routes/test')(app);
// starting http server
var httpd = require('http').createServer(app).listen(8000, function(){
console.log('HTTP server listening on port 8000');
});
var io = require('socket.io').listen(httpd, { log: false });
io.on('connection', function(socket){
socket.join("test");
socket.on('message', function(data){
.....
});
});
module.exports = app;
我的 test.js 文件:
module.exports = function(app){
app.post('/test', function(req, res){
.....
//I would like here be able to send to all clients in room "test"
});
};