- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要从服务器代码、路由器/ Controller 上的任何位置发出来自服务器的套接字。我检查了一些线程和谷歌,但没有按预期工作。
app.js
var app = require('express').createServer();
var io = require('socket.io')(app);
require(./router.js)(app)
require(./socket.js)(io)
app.listen(80);
router.js
module.exports = function (app) {
app.use('/test', require(./controller.js));
return app;
};
套接字.js
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
controller.js
var express = require('express');
var router = express.Router();
router.get('/about', function(req, res) {
// I need to emit here
});
module.exports = router;
这不是我使用的提取代码。这是我正在使用的结构以及我需要调用的地方。
最佳答案
您将需要 io
对象来发出事件。
流程 1:将 io 作为全局传递(快速修复)
app.js
var app = require('express').createServer();
var io = require('socket.io')(app);
global.io = io; //added
require(./socket.js)(io)
app.listen(80)
controller.js
router.get('/about', function(req, res) {
// I need to emit here
global.io.emit('news', { hello: 'world' });
});
注意事项:至于传递 io 对象。您有几个选择,可能是也可能不是最好的。
Probably, the cleanest way is to pass is as arguments to the controllers, while requiring them in routes.
流程 2:将 io 作为参数传递
app.js
var app = require('express').createServer();
var io = require('socket.io')(app);
require(./router.js)(app,io);
require(./socket.js)(io);
app.listen(80);
router.js
/*
* Contains the routing logic
*/
module.exports = function (app,io) {
//passing while creating the instance of controller for the first time.
var controller = require("./controller")(io);
app.get('/test/about',controller.myAction);
};
controller.js
module.exports = function(io){
var that={};
/*
* Private local variable
* made const so that
* one does not alter it by mistake
* later on.
*/
const _io = io;
that.myAction = function(req,res){
_io.emit('news', { hello: 'world' });
res.send('Done');
}
// everything attached to that will be exposed
// more like making public member functions and properties.
return that;
}
关于node.js - Socket.IO + Express 框架 |从 Express Controller 和路由器发射/发射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36904048/
我正在尝试使用 DynamicMethod 并尝试使用 IL 来创建一些对象。我想创建以下非常基本的对象: new Queue(new List{100}); 我已经使用 ILDASM 查看生成此代码
这是我stackoverflow的第一个问题!我有一个显示 mpl 图 Canvas 的 PyQT gui。我已将主轴周围的边距设置为 0,因此绘图将完全填满图形 Canvas 和包含它的小部件。问题
我的环境是 Windows 7,安装了 scala 2.11.4(运行良好),Java 1.8 我已经尝试过 spark-1.2.0-bin-hadoop2.4 和 spark-1.2.1-bin-h
对于我的生活,我无法弄清楚为什么我不能发送或捕获一些数据。 toggleNavigation() 触发,但我不确定 .emit() 是否真的在工作。 最终我想折叠和展开导航,但现在我只想了解如何将数据
我试图在 VUE 3 中传递一个 emit prop,每次传递它时我仍然得到 false,并且 prop 无法切换。 Accordion .vue
我有一个 View 模型,它采用初始 ViewState对象并具有可公开访问的 state可以收集的变量。 class MyViewModel(initialState: ViewState) : V
现在在玩 RxJava,偶然发现了以下问题: 我有 2 个不同的流: 带有项目的流 Stream(只有 1 个项目),它发出第一个流的转换信息。 所以基本上我有项目流,我希望所有这些项目与第二个流中的
我有一个 API 登录服务,它使用 http 服务来执行登录逻辑(LoginApiService、login-api.service.ts): login(data: LoginCredentials
我们有微服务架构,我们通过网络进行服务间调用。我们在顶层服务中使用 RxJava,这会导致向底层服务创建大量并行请求。因此,我收到“没有到主机的路由错误”或“连接错误”。为此,我想减慢 RxJava
Vue.component('rating-edit', { template:` {{rating.remark}} Sav
我最近购买了 Dream Cheeky Thunder 导弹发射器,我希望通过我的树莓派来控制它。 使用来自报复的代码(https://raw.githubusercontent.com/codeda
我制作了这段代码来记录发送到我的机器人的 DM: client.on('messageCreate', async message => { if (message.author.bot) r
我需要从服务器代码、路由器/ Controller 上的任何位置发出来自服务器的套接字。我检查了一些线程和谷歌,但没有按预期工作。 app.js var app = require('express'
我是一名优秀的程序员,十分优秀!