我正在开发Node.Js中的浏览器游戏,我有这个脚本:
game.js>>
var config = require('./game_config.js');
var mysql = require('mysql');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var connexion = mysql.createConnection({
'host': config.DB_HOST,
'user' : config.DB_USER,
'password' : config.DB_PASS,
'database' : config.DB_NAME
});
var Player = require('./server/class.player.js');
io.on('connect', function(socket) {
console.log('Co');
var player
socket.on('login', function(data) {
connexion.query("SELECT * FROM player WHERE nick = '"+data.login+"' AND pass = '"+data.pass+"'", function(err, rows) {
if (err) {
throw err;
} else {
if (rows.length == 0) {
var dataRet = "LOG";
socket.emit('login', dataRet);
} else {
var p = rows[0];
var dataRet = new Player(p.id, p.nick, p.map_id, p.x, p.y, connexion).toJson();
console.log(dataRet);
}
// Without setTimeout it wouldn't work because the object didn't have the time to instantiate
setTimeout(function() {
socket.emit('login', dataRet);
},1000);
}
});
});
socket.on('disconnect', function(socket) {
console.log('Disco');
});
});
class.Player.js>>
var Player = function (id, name, map_id, x, y, connexion) {
this.id = id;
this.name = name;
this.map_id = map_id ;
this.x = x;
this.y = y;
this.link = connexion;
this.toJson = function () {
return {
'id' : this.id,
'name' : this.name,
'map_id' : this.map_id,
'x' : this.x,
'y' : this.y
};
}
}
module.exports = User;
基本上,我的代码工作得很好,这要归功于" setTimeout()"在game.js中(对于socket.emit()事件)。如果我不使用它,那么对象' dataRet'由于Node.js的异步性,没有时间实例化,所以套接字发出" undefined"或" null"。
所以我在思考,必须有一种方法来监听对象实例化,以便在完成后立即通过socket.io发出它。
答案 0 :(得分:0)
这与您的问题本身无关,但这非常重要 - 您有一个巨大的SQL injection漏洞,任何人都可以对您的数据库执行任何操作。
而不是:
connection.query(
"SELECT * FROM player WHERE nick = '"
+ data.login + "' AND pass = '" + data.pass + "'",
function (err, rows) {
//...
}
);
使用:
connection.escape(data.login)
和connection.escape(data.pass)
取代data.login
和data.pass
或:
connection.query(
"SELECT * FROM player WHERE nick = ? AND pass = ?", [data.login, data.pass],
function (err, rows) {
// ...
}
);
它不仅更安全,而且实际上更容易阅读和理解。请参阅Escaping query values中的node-mysql manual。
现在,回到你的问题。您的Player构造函数没有任何异步,因此您的问题必须是其他问题。在我们这里有什么奇怪的,你的Player.js导出User
(未定义)而不是Player
(已定义)所以我很惊讶它甚至可以工作。或者您可能发布了与您实际使用的代码不同的代码,这可以解释为什么您的代码中没有明显的竞争条件。
但是如果你的Player构造函数正在进行一些异步调用,那么我建议添加一个回调参数并从构造函数中调用它:
var Player = function (id, name, map_id, x, y, connexion, callback) {
this.id = id;
this.name = name;
this.map_id = map_id ;
this.x = x;
this.y = y;
this.link = connexion;
this.toJson = function () {
return {
'id' : this.id,
'name' : this.name,
'map_id' : this.map_id,
'x' : this.x,
'y' : this.y
};
}
// some async call that you have to wait for
// symbolized with setTimeout:
setTimeout(function () {
if (callback && typeof callback === 'function') {
callback(this);
}
}, 1000);
}
然后你可以将回调传递给你的构造函数,所以这个:
} else {
var p = rows[0];
var dataRet = new Player(p.id, p.nick, p.map_id, p.x, p.y, connexion).toJson();
console.log(dataRet);
}
// Without setTimeout it wouldn't work because the object didn't have the time to instantiate
setTimeout(function() {
socket.emit('login', dataRet);
},1000);
可能会改为:
} else {
var p = rows[0];
var dataRet = new Player(p.id, p.nick, p.map_id, p.x, p.y, connexion, function () {
socket.emit('login', dataRet);
}).toJson();
console.log(dataRet);
}
但是在这里,正如我所说的,没有什么是异步的,甚至在你运行setTimeout之前你的dataRet
已经设置好了,所以这不能解决你的问题,但它正在回答你的问题。