express.js 4 和带有快速路由器的套接字 [英] express.js 4 and sockets with express router

查看:35
本文介绍了express.js 4 和带有快速路由器的套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 express.js 4 创建一个非常简单的节点 API,但我需要一些我添加了 socket.io 的实时"事件.我对两者都比较陌生,所以我可能会遗漏一些基本的东西,但我找不到关于这方面的好的文档/tuts.

I'm trying to create a really simple node API using express.js 4 but I need a few 'realtime' events for which I added socket.io. I'm fairly new to both so I'm likely missing something basic but I can't find good docs/tuts on this.

在 express 应用程序(使用 express 生成器创建)中,我根据我阅读的简单示例和项目文档获得了类似的内容.这工作正常,从客户端应用程序,我可以发送/接收套接字事件:

In the express app (created with the express generator) I have something like this based on simple examples and project docs that I read. This works OK and from client apps, I can send/receive the socket events:

var express = require('express');
var path = require('path');
var logger = require('morgan');
var api = require('./routes/api');
var app = express();
var io = require('socket.io').listen(app.listen(3000));

app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/api', api);

io.sockets.on('connection', function (socket) {
    console.log('client connect');
    socket.on('echo', function (data) {
        io.sockets.emit('message', data);
    });
});


// error handlers omitted

module.exports = app;

但我想使用我的 API 路由中的套接字(在我上面需要"的 ./routes/api.js 文件中).例如,有人可能使用 API 来 PUT/POST 资源,我希望该广播到连接的 socket.io 客户端.

but I want to use the sockets from my API routes (in the ./routes/api.js file that I 'require' above). For example, someone might use the API to PUT/POST a resource and I want that broadcast to connected socket.io clients.

我看不到如何使用 'io' 变量或组织当前在快速路由内的 io.sockets.on('connection' ... 函数中的代码.这是 ./routes/api.js 文件:

I cannot see how to use the 'io' variable or organise the code currently in the io.sockets.on('connection' ... function inside express routes. Here's the ./routes/api.js file:

var express = require('express');
var router = express.Router();
var io = ???;

router.put('/foo', function(req, res) {
    /* 
      do stuff to update the foo resource 
      ...
     */

    // now broadcast the updated foo..
    io.sockets.emit('update', foo); // how?
});

module.exports = router;

推荐答案

我对你的文件做了一些修改,你可以看看它是否有效吗?

I've modified your files a little bit, may you check if it works?

您可以将您定义的 io 传递给您的路线,如下所示;

You can pass the io you've defined to your routes like below;

require('./routes/api')(app,io); 

我没有测试 Socket.IO 部分,但没有语法错误并且路由也可以工作.

I didn't test the Socket.IO parts but there is no syntax error and routes also working.

server.js 文件:

server.js file:

var express = require('express');
var app = express();
var path = require('path');
var logger = require('morgan');

var io = require('socket.io').listen(app.listen(3000));
 
app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
 
io.sockets.on('connection', function (socket) {
    console.log('client connect');
    socket.on('echo', function (data) {
    io.sockets.emit('message', data);
 });
});

require('./routes/api')(app,io); 
 
console.log("Server listening at port 3000");

api.js:

module.exports = function(app,io) {
app.put('/foo', function(req, res) {
 
    /* 
 
      do stuff to update the foo resource 
 
      ...
 
     */
 
 
    // now broadcast the updated foo..
    
    console.log("PUT OK!");
 
    io.sockets.emit('update'); // how?
    res.json({result: "update sent over IO"});
 
});
}

这篇关于express.js 4 和带有快速路由器的套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆