これが私のapp.jsの外観です
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
static = require('node-static'); // for serving files
var game = require('./game.js').createGame();
// This will make all the files in the current folder
// accessible from the web
var fileServer = new static.Server('./');
// This is the port for our web server.
// you will need to go to http://localhost:8080 to see it
app.listen(8080);
// Listen for incoming connections from clients
io.sockets.on('connection', function (socket) {
handle Events ...
});
exports.io = io;
exports.game = game;
作成されたsocket.iolistnerまたはゲームインスタンスにアクセスしようとすると、未定義というエラーが表示されます。これは私がtrick、jsでそれにアクセスしようとしている方法です
var game = require('./app.js').game;
var socketio = require('./app.js').io;
var PlayerMove = require('./playerMove.js');
これは、trick.jsが実際にapp.jsの前に実行されているためである可能性があります(デバッグポイントを配置し、そこで確認されました)。これを回避するにはどうすればよいですか?node.jsでオブジェクトインスタンスを公開するためのより良い方法はありますか?おそらく使用する必要があるパターンはありますか?