在Express 4和express-generator / bin / www中使用socket.io [英] Using socket.io in Express 4 and express-generator's /bin/www

查看:147
本文介绍了在Express 4和express-generator / bin / www中使用socket.io的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里是交易:我正试图在一个快速项目中使用socket.io。 Express Js 4之后,我已经更新了我的express-genrator,现在应用程序的初始功能进入 ./ bin / www 文件,包括那些vars(www文件内容: http://jsfiddle.net/avMa5/

So here is the deal: I'm trying to use socket.io in an express project. After Express Js 4 was lauched, i've updated my express-genrator and now the app initial functions goes into ./bin/www file, including those vars (www file contents: http://jsfiddle.net/avMa5/ )

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

(请检查 npm install -g express-generator 然后 express myApp

(check it by npm install -g express-generator and then express myApp

据说,让我们记住socket.io文件如何要求我们启动它:

that being said, let's remember how socket.io docs ask us to fire it:

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

好的,但我不能在app.js里面做,像推荐的那样,应该在./bin/www命令工作在./bin/www这是我可以做的,让它工作:

Ok but i can't do it inside app.js, like recommended. This should be done in ./bin/www in order to work. in ./bin/www this is what i can do to get it working:

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

Ok thi的作品,但我不能使用io var其他地方,我真的不想把我的socket.io函数放在 www 文件。

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.

我想这只是基本的sintax,但我无法让这个工作,甚至不使用 module.exports = server server.exports = server module.exports.io = app(io)在www文件

I guess this is just basic sintax, 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

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

So the question is: how can i use socket.io havin this /bin/www file as starting point of my app?

推荐答案

事实证明,这真的是一些基本的sintax问题....我从这个socket.io聊天教程 ...

It turns out it really was some basic sintax problem.... I got these lines from this socket.io chat tutorial...

on ./bin / www,刚刚 var server = app.listen(.....)

on ./bin/www, just after var server = app.listen(.....)

var io = require('socket.io').listen(server);
require('../sockets/base')(io);

所以现在我创建了../sockets/base.js文件,把这个小伙子放在里面:

so now I create the ../sockets/base.js file and put this little fellow inside it:

module.exports = function (io) { // io stuff here... io.on('conection..... }

是啊!现在它的工作...所以我想我真的没有选择其他比起启动socket.io里面的/ bin / www,因为这是我的http服务器启动的地方
目标是现在我可以在其他文件中建立套接字功能,保持模块化,以code> require('fileHere')(io);

Yeah! Now it works... So i guess i really had no option other than starting socket.io inside /bin/www , because that is where my http server was started. The goal is that now i can build socket functionality in other file(s), keeping the thing modular, by require('fileHere')(io);

< 3

这篇关于在Express 4和express-generator / bin / www中使用socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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