在 node.js 中分离文件服务器和 socket.io 逻辑

我对 node.js 相当陌生,我发现随着项目规模的增长,将一个项目分成多个文件是相当复杂的。之前我有一个很大的文件,它既是一个文件服务器,也是一个多人 HTML5游戏的 Socket.IO 服务器。在理想情况下,我希望将文件服务器、 socket.IO 逻辑(从网络读取信息并将其写入带有时间戳的缓冲区,然后将其发送给所有其他参与者)和游戏逻辑分开。

使用 socket.io 中的第一个示例来演示我的问题,通常有两个文件。app.js是服务器,index.html被发送到客户端。

App.js:

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')


app.listen(80);


function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}


res.writeHead(200);
res.end(data);
});
}


io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});

.html:

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>

为了分离文件服务器和游戏服务器逻辑,我需要在一个文件中定义函数“处理程序”,我需要一个使用 io.sockets.on ()回调的匿名函数在另一个文件中,我还需要第三个文件来成功地包含这两个文件。现在我尝试了以下方法:

Js:

var fileserver = require('./fileserver.js').start()
, gameserver = require('./gameserver.js').start(fileserver);

Js:

var app = require('http').createServer(handler),
fs = require('fs');


function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}


res.writeHead(200);
res.end(data);
});
}


module.exports = {
start: function() {
app.listen(80);
return app;
}
}

游戏服务器:

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


function handler(socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
}


module.exports = {


start: function(fileserver) {
io.listen(fileserver).on('connection', handler);
}


}

这似乎可以工作(静态内容被正确地提供,当客户端连接时,控制台清楚地显示与 Socket.IO 的握手) ,尽管没有数据被发送。就好像从来没有实际调用 socket.sent ()和 socket.on ()一样。我甚至修改了处理程序()在 gameserver.js中添加 console.log('User connected');,但这从来没有显示。

我如何能够在一个文件中使用 Socket.IO,在另一个文件中使用文件服务器,并且仍然期望两者都能正确操作?

51851 次浏览

我会做这样的事。

应用程序

var app = require('http').createServer(handler),
sockets = require('./sockets'),
fs = require('fs');


function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}


res.writeHead(200);
res.end(data);
});
}


sockets.startSocketServer(app);
app.listen(80);

还有 Sockets.js

var socketio = require('socket.io'),
io, clients = {};


module.exports = {


startSocketServer: function (app) {
io = socketio.listen(app);


// configure
io.configure('development', function () {
//io.set('transports', ['websocket', 'xhr-polling']);
//io.enable('log');
});


io.configure('production', function () {
io.enable('browser client minification');  // send minified client
io.enable('browser client etag');          // apply etag caching logic based on version number
io.set('log level', 1);                    // reduce logging
io.set('transports', [                     // enable all transports (optional if you want flashsocket)
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
});
//


io.sockets.on('connection', function (socket) {
console.log("new connection: " + socket.id);


socket.on('disconnect', function () {
console.log("device disconnected");


});


socket.on('connect_device', function (data, fn) {
console.log("data from connected device: " + data);
for (var col in data) {
console.log(col + " => " + data[col]);
}




});
});
}
};

i just copy&pasted some of my old code - don't really know what changed in the last versions of socket.io, but this is more about the structure than the actual code.

我只会用2个文件,而不是3个。 当你考虑进一步分解它,也许一个其他文件为不同的路由..。

希望这能帮上忙。

在 socket.io 0.8中,应该使用 io.sockets.on('...')附加事件,除非使用名称空间,否则似乎没有使用 sockets部分:

io.listen(fileserver).sockets.on('connection', handler)

最好避免这样链接它(您可能希望稍后使用 io对象)。我现在的做法是:

// sockets.js
var socketio = require('socket.io')


module.exports.listen = function(app){
io = socketio.listen(app)


users = io.of('/users')
users.on('connection', function(socket){
socket.on ...
})


return io
}

然后在创建服务器 app之后:

// main.js
var io = require('./lib/sockets').listen(app)

I've another solution. You can use require.js creating a module and pass "app" as an argument. Within the module you can start socket.io and organize your sockets.

App.js :

  var requirejs = require('requirejs');


requirejs.config({
baseUrl: './',
nodeRequire: require
});


requirejs(['sockets'], function(sockets) {


var app = require('http').createServer()
, fs  = require('fs')
, io  = sockets(app);


// do something
// add more sockets here using "io" resource


});

Socket.js模块中,你可以这样做:

  define(['socket.io'], function(socket){
return function(app){
var server = app.listen(3000)
, io     = socket.listen(server);


io.sockets.on('connection', function (socket) {
console.log('connected to socket');


socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});


// more more more


});


return io;
}
});

我希望我的贡献能帮到你。

我在这方面也尝试过,我对结果相当满意。查看 https://github.com/hackify/hackify-server获取源代码。