Using socket.io in Express 4 and express-generator's /bin/www

问题:

So here is the deal: I'm trying to use socket.io in an express project.所以这就是交易:我正在尝试在一个快速项目中使用 socket.io。 After Express Js 4 was lauched, i've updated my express-generator and now the app initial functions goes into ./bin/www file, including those vars (www file contents: http://jsfiddle.net/avMa5/ ) Express Js 4 发布后,我更新了我的 express-generator,现在应用程序初始功能进入./bin/www文件,包括那些变量(www 文件内容: http : //jsfiddle.net/avMa5/

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

(check it by npm install -g express-generator and then express myApp (通过npm install -g express-generator ,然后express myApp

that being said, let's remember how socket.io docs ask us to fire it:话虽如此,让我们记住 socket.io 文档如何要求我们触发它:

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

Ok but i can't do it inside app.js, like recommended.好的,但我不能在 app.js 中做到这一点,就像推荐的那样。 This should be done in ./bin/www in order to work.这应该在 ./bin/www 中完成才能工作。 in ./bin/www this is what i can do to get it working:在 ./bin/www 这是我可以做的让它工作:

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

Ok this works, but i can't use the io var anywhere else, and i really don't want to put my socket.io functions on www file.好的,这可行,但我不能在其他任何地方使用 io var,而且我真的不想将 socket.io 函数放在www文件中。

I guess this is just basic syntax, but I can't get this to work, not even using module.exports = server or server.exports = server nor module.exports.io = app(io) on www file我想这只是基本语法,但我无法module.exports = server工作,甚至不能在 www 文件上使用module.exports = serverserver.exports = servermodule.exports.io = app(io)

So the question is: how can i use socket.io having this /bin/www file as starting point of my app?所以问题是:我如何使用 socket.io 将此 /bin/www 文件作为我的应用程序的起点?


解决方案:

参考: https://stackoom.com/en/question/1fGBL
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章