0
我有以下代碼:我在node.js中接收消息和對象插座未定義
function Socket(io, playGame, mapper) {
io.on('connection', function (socket) {
// message handler for the chat message
socket.on('sendChat', function (data) {
console.log(socket);
console.log(data);
console.log('recieved chat');
var connectedPlayer = playGame.findConnectedPlayer(socket);
if (!connectedPlayer)
return;
var connectedGame = playGame.findConnectedGame(socket, connectedPlayer.gameId);
if (!connectedGame)
return;
// send update game with players properly ordered
for (socketIndex in this.sockets) {
var socket = this.sockets[socketIndex];
// send the new data to each player
socket.socket.emit('chatUpdate', { chatText: data.chat });
}
});
// message handler for join game message
socket.on('joinGame', function (data) {
console.log('recieved join:', JSON.stringify(data));
if (!playGame.newConnectedPlayer(socket, data))
return;
...
在用於sendChat的方法中,插座是未定義的。在joinGame的方法中,定義了套接字。我嘗試了幾個想法,但問題依然存在。任何幫助,將不勝感激。
你確定'this.sockets'和'this.sockets [socketIndex]'存在嗎? – 2014-10-30 21:01:28