Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从NodeJS中的模型访问套接字控制器_Node.js_Socket.io - Fatal编程技术网

Node.js 从NodeJS中的模型访问套接字控制器

Node.js 从NodeJS中的模型访问套接字控制器,node.js,socket.io,Node.js,Socket.io,所以我正在做一个学校项目,我们有一个代码模板。 我有一个mysql数据库正在运行,每当某个表被插入时,我希望任何在网站上查看该表的人都能使用socket.io更新该表。 但是我不能从模型访问socketcontroller,也不能从routecontroller(我在这里调用模型)访问socketcontroller。我知道最好的做法可能是将插入的数据返回控制器(通过回调),然后在控制器中使用io发出消息 在index.js中 var httpServer = http.Server(app);

所以我正在做一个学校项目,我们有一个代码模板。 我有一个mysql数据库正在运行,每当某个表被插入时,我希望任何在网站上查看该表的人都能使用socket.io更新该表。 但是我不能从模型访问socketcontroller,也不能从routecontroller(我在这里调用模型)访问socketcontroller。我知道最好的做法可能是将插入的数据返回控制器(通过回调),然后在控制器中使用io发出消息

在index.js中

var httpServer = http.Server(app);
var io = require('socket.io').listen(httpServer);
io.use(sharedsession(session));

var router = require('./controller.js');
app.use('/API', router);

var socketController = require('./socketController.js');
io.on('connection', function (socket) {
    socketController(socket, io);
});
var model = require('./model.js');
在socketController.js中

module.exports = function (socket, io) {
    socket.on('join', function (req) {
        console.log(req);
        var security = req.security;
        socket.join(security);
    });

    socket.on('update', function (req) {
        //this is the function i want to "call" from the model/routecontroller
    });
}
router.post('/orders', function(req, res) {
    //var socket = io(); <--- not defined
    model.matchOrder(req.body, function(result) {
        res.json({order: req.body});
    })
});
在controller.js中

module.exports = function (socket, io) {
    socket.on('join', function (req) {
        console.log(req);
        var security = req.security;
        socket.join(security);
    });

    socket.on('update', function (req) {
        //this is the function i want to "call" from the model/routecontroller
    });
}
router.post('/orders', function(req, res) {
    //var socket = io(); <--- not defined
    model.matchOrder(req.body, function(result) {
        res.json({order: req.body});
    })
});
router.post('/orders',函数(req,res){
//var socket=io();