奇怪的JavaScript在Express / Socket.io应用程序我正在处理

我试图用Brian Ford的优秀的https://github.com/btford/angular-socket-io-seed开发一个像SCADA一样的应用程序,但是我遇到了一些JavaScript代码,理解。 更糟糕的是,我甚至不知道要search什么。 我通过Googlefind的每个例子都使用了第二种语法,在这里至less不起作用。

主app.js中的代码工作,但我需要访问套接字对象,所以我可以传递给我的开发模拟模块,所以我需要改变它。 但是,当我更改套接字连接callback模块不再被加载。 我知道当routes / socket.js中的代码运行时,因为日志行MET3:来自它。

有人可以给我一个线索是什么原始的行为,所以我可以做出改变呢? 这是一个很酷的新的速记我应该使用?

不确定如果是相关的,但我正在运行socket.io 0.9.16和node.js 0.10.29。


var io = require('socket.io').listen(server); // many lines latter io.sockets.on('connection', require('./routes/socket.js')); // What is this? Working Output Express server listening on port 3000 debug - client authorized info - handshake authorized hOEv8Iv7pPO1xLdxdq1V MET1: routes/index.js index() debug - setting request GET /socket.io/1/websocket/hOEv8Iv7pPO1xLdxdq1V **MET3:** routes/socket.js Socket ID [hOEv8Iv7pPO1xLdxdq1V] connected debug - websocket writing 5:::{"name":"send:name","args":[{"sockName":"JohnDoe","sockPage":"RETS"}]} debug - websocket writing 5:::{"name":"send:time","args":[{"time":"Fri Jul 25 2014 17:39:32 GMT-0400 (EDT)"}]} 

 var io = require('socket.io').listen(server); // many lines latter io.sockets.on('connection', function (socket) { console.log ("MET0: app.js io.sockets.on() running"); require('./routes/socket.js'); // This should work }); Broken Output Express server listening on port 3000 debug - client authorized info - handshake authorized Hn9It34K2OCT6o8ceinF **MET0:** app.js io.sockets.on() running MET1: routes/index.js index() debug - emitting heartbeat for client Hn9It34K2OCT6o8ceinF debug - websocket writing 2:: debug - set heartbeat timeout for client Hn9It34K2OCT6o8ceinF debug - got heartbeat packet debug - cleared heartbeat timeout for client Hn9It34K2OCT6o8ceinF 

 io.sockets.on('connection', require('./routes/socket.js')); // What is this? 

从本质上讲,这就是说,将从require('./routes/socket.js')返回的任何东西传递给套接字函数。 如果您不熟悉在节点中如何使用模块,则可能是/routes/socket.js包含如下内容:

 module.exports = function() { // Do some work }; 

这意味着上面的函数将被require调用返回。 看看socket.js ,看看返回的是什么。