如何使用套接字。io结合表达。JS(使用快速应用程序生成器)

时间:2022-08-22 15:26:28

I'm trying to use Socket.io combined with Express.JS (using Express application generator).
I've found some aswers how to do this (Using socket.io in Express 4 and express-generator's /bin/www).
My problem is that i cannot make use of the sockets inside the routes folder. I can use them in the app.js and bin/www.js files. When i call the route index.js it just keeps loading the webpage for a long time without giving any errors.

bin/www.js

我想用插座。io结合表达。JS(使用快速应用程序生成器)。我找到了一些解决方法(使用插座)。io in Express 4 and Express -generator’s /bin/www)。我的问题是我不能使用路由文件夹中的套接字。我可以在app.js和bin/www.js文件中使用它们。当我调用路由索引时。它只是持续加载网页很长一段时间,没有任何错误。bin / www.js

...
/**
 * Create HTTP server.
 */

var server = http.createServer(app);

var io     = app.io
io.attach( server );
...

app.js

app.js

...
// Express
var app = express();

// Socket.io
var io = socket_io();
app.io = io;
var routes = require('./routes/index')(io);
...

routes/index.js

路线/ index.js

module.exports = function(io) {
    var app = require('express');
    var router = app.Router();

    io.on('connection', function(socket) {
        console.log('User connected');
    });

    return router;
}

1 个解决方案

#1


2  

Here is a simple example on how to use Socket.io with Express that I made available on GitHub here:

下面是一个如何使用Socket的简单示例。io with Express, I made available on GitHub here:

The backend code is this:

后端代码是:

var path = require('path');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', (req, res) => {
  console.error('express connection');
  res.sendFile(path.join(__dirname, 'si.html'));
});
io.on('connection', s => {
  console.error('socket.io connection');
  for (var t = 0; t < 3; t++)
    setTimeout(() => s.emit('message', 'message from server'), 1000*t);
});
http.listen(3002, () => console.error('listening on http://localhost:3002/'));
console.error('socket.io example');

See https://github.com/rsp/node-websocket-vs-socket.io/blob/master/si.js

参见https://github.com/rsp/node-websocket-vs-socket.io/blob/master/si.js

As you can see here, I am creating the express app with:

正如你在这里看到的,我正在创建express app:

var app = require('express')();

Then I create an http server with that app with:

然后我用这个app创建一个http服务器:

var http = require('http').Server(app);

And finally I use that http server to create the Socket.io instance:

最后,我使用http服务器创建套接字。io实例:

var io = require('socket.io')(http);

After running:

在运行:

http.listen(3002, () => console.error('listening on http://localhost:3002/'));

it all works together.

将这些有机的组合在一起。

You can see the entire example on GitHub with both backend and frontend code that works. It currently uses Express 4.14.0 and socket.io 1.4.8.

您可以看到GitHub上的整个示例,其中包含后端代码和前端代码。它目前使用的是Express 4.14.0和套接字。io 1.4.8。

#1


2  

Here is a simple example on how to use Socket.io with Express that I made available on GitHub here:

下面是一个如何使用Socket的简单示例。io with Express, I made available on GitHub here:

The backend code is this:

后端代码是:

var path = require('path');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', (req, res) => {
  console.error('express connection');
  res.sendFile(path.join(__dirname, 'si.html'));
});
io.on('connection', s => {
  console.error('socket.io connection');
  for (var t = 0; t < 3; t++)
    setTimeout(() => s.emit('message', 'message from server'), 1000*t);
});
http.listen(3002, () => console.error('listening on http://localhost:3002/'));
console.error('socket.io example');

See https://github.com/rsp/node-websocket-vs-socket.io/blob/master/si.js

参见https://github.com/rsp/node-websocket-vs-socket.io/blob/master/si.js

As you can see here, I am creating the express app with:

正如你在这里看到的,我正在创建express app:

var app = require('express')();

Then I create an http server with that app with:

然后我用这个app创建一个http服务器:

var http = require('http').Server(app);

And finally I use that http server to create the Socket.io instance:

最后,我使用http服务器创建套接字。io实例:

var io = require('socket.io')(http);

After running:

在运行:

http.listen(3002, () => console.error('listening on http://localhost:3002/'));

it all works together.

将这些有机的组合在一起。

You can see the entire example on GitHub with both backend and frontend code that works. It currently uses Express 4.14.0 and socket.io 1.4.8.

您可以看到GitHub上的整个示例,其中包含后端代码和前端代码。它目前使用的是Express 4.14.0和套接字。io 1.4.8。