ste*_*esu 69 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);
});
});
Run Code Online (Sandbox Code Playgroud)
index.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>
Run Code Online (Sandbox Code Playgroud)
要分离文件服务器和游戏服务器逻辑,我需要在一个文件中定义函数"handler",我需要匿名函数使用io.sockets.on()的回调在另一个文件中,我还需要一个第三个文件成功包含这两个文件.现在我尝试了以下内容:
start.js:
var fileserver = require('./fileserver.js').start()
, gameserver = require('./gameserver.js').start(fileserver);
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
游戏服务器:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎有效(静态内容已正确提供,并且控制台在客户端连接时清楚地显示与Socket.IO的握手),尽管没有数据发送.就好像从未实际调用过socket.emit()和socket.on()一样.我甚至修改了handler()gameserver.js以添加console.log('User connected');但是从未显示过.
如何在一个文件中安装Socket.IO,在另一个文件中安装文件服务器,并且仍然希望两者都能正常运行?
Ric*_*asi 83
在socket.io 0.8中,您应该使用附加事件io.sockets.on('...'),除非您使用名称空间,否则您似乎缺少该sockets部分:
io.listen(fileserver).sockets.on('connection', handler)
Run Code Online (Sandbox Code Playgroud)
避免以这种方式链接它可能更好(您可能希望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
}
Run Code Online (Sandbox Code Playgroud)
然后在创建服务器之后app:
// main.js
var io = require('./lib/sockets').listen(app)
Run Code Online (Sandbox Code Playgroud)
我会做这样的事情.
app.js
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);
Run Code Online (Sandbox Code Playgroud)
和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]);
}
});
});
}
};
Run Code Online (Sandbox Code Playgroud)
我只是复制并粘贴了一些旧代码 - 不知道socket.io的最新版本中发生了什么变化,但这更多是关于结构而不是实际代码.
我只会使用2个文件用于您的目的,而不是3.当您考虑进一步拆分时,可能是另一个文件用于不同的路线...
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
40761 次 |
| 最近记录: |