gpt4 book ai didi

javascript - 在 Express 4 和 express-generator's/bin/www 中使用 socket.io

转载 作者:IT老高 更新时间:2023-10-28 21:48:47 25 4
gpt4 key购买 nike

这就是交易:我正在尝试在一个快速项目中使用 socket.io。在 Express Js 4 发布后,我更新了我的 express-generator,现在应用程序初始函数进入 ./bin/www 文件,包括那些变量(www 文件内容:http://jsfiddle.net/avMa5/)

var server = app.listen(app.get('port'), function() {..}

(通过 npm install -g express-generator 检查它,然后 express myApp

话虽如此,让我们记住 socket.io 文档是如何要求我们触发它的:

var app = require('express').createServer();
var io = require('socket.io')(app);

好的,但我不能在 app.js 中这样做,就像推荐的那样。这应该在 ./bin/www 中完成才能正常工作。在 ./bin/www 中,这是我可以做的让它工作:

var io = require('socket.io')(server)

好的,这可行,但我不能在其他任何地方使用 io var,而且我真的不想将我的 socket.io 函数放在 www 文件中。

我猜这只是基本语法,但我无法让它工作,即使使用 module.exports = serverserver.exports = server 也不module.exports.io = app(io) 在 www 文件上

所以问题是:如何使用 socket.io 将此/bin/www 文件作为我的应用程序的起点?

最佳答案

以下是将 Socket.io 添加到新生成的 Express-Generator 应用程序的方法:

  1. 创建一个包含您的 socket.io 逻辑的文件,例如 socketapi.js :

socketapi.js:

const io = require( "socket.io" )();
const socketapi = {
io: io
};

// Add your socket.io logic here!
io.on( "connection", function( socket ) {
console.log( "A user connected" );
});
// end of socket.io logic

module.exports = socketapi;
  1. 修改你的bin/www发射器。有两个步骤:需要您的 Socket.io api 并在创建 HTTP 服务器后立即将 HTTP 服务器附加到您的 socket.io 实例:

bin/www:

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('socketexpress:server');
var http = require('http');
let socketapi = require("../socketapi"); // <== Add this line

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);
socketapi.io.attach(server); // <== Also add this line

(...)
  1. 然后你只需要在你的 index.html 中添加 Socket.io 客户端。在 </body> 之前添加以下内容结束标签:

index.html

    (...)
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
</body>
</html>
  1. 终于可以启动你的 Express 服务器了:
  • 基于 Unix:DEBUG=myapp:* npm start
  • Windows:set DEBUG=myapp:* & npm start

注 1

如果出于任何原因您需要访问 app.js 中的套接字 API ,那么您可以改为在 app.js 中导入您的套接字 api ,然后重新导出:

app.js

var express = require('express');
var socketapi = require("./socketapi"); // <== Add this line
(...)
// You can now access socket.io through the socketapi.io object
(...)
module.exports = { app, socketapi }; // <== Export your app and re-export your socket API here

然后在你的bin/www启动器,而不是在自己的行中导入您的套接字 api,只需在您的应用程序中导入它:

bin/www

var {app, socketapi} = require('../app'); // <== Import your app and socket api like this
(...)
var server = http.createServer(app);
socketapi.io.attach(server); // <== You still have to attach your HTTP server to your socket.io instance

注意 2此答案已更新为使用最新的 Express Generator(撰写本文时为 4.16)和最新的 Socket.io(撰写本文时为 3.0.5)。

关于javascript - 在 Express 4 和 express-generator's/bin/www 中使用 socket.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24609991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com