我正在尝试从外部.js文件调用socket.on()事件,但我无法弄清丢失的内容... 我将NodeJS和ExpressJS一起使用。以下是文件:
app.js(服务器文件)
const fs = require('fs');
const express = require('express');
const app = express();
const http = require('http').Server(app);
var io = require('socket.io')(http);
....
//Socket Io functions
const ioObj = require( './library/io.js')(app, express, io);
// This route will be used to print the type of HTTP request the particular Route is referring to
router.use(function (req, res, next) {
console.log("/" + req.method);
next();
});
....
/library/io.js(套接字文件)
module.exports = function(app, express, io){
io.on('connection', async function(socket) {
socket.on('refreshPage', function(){
console.log("page should now be refreshed !!");
socket.emit("refreshPageNow");
});
....
});
}
我想做的是从/library/io.js调用/访问refreshPage事件,这样我可以进一步发送“刷新网页”信号。 我试图做类似的事情:
io.sockets.emit("refreshPage");
和
ioObj.sockets.emit("refreshPage");
但是没用...