2013-04-07 101 views
-3

我正在使用node.js + express + socket.io編寫遊戲。花了很長時間纔得到我的位置,但我覺得它終於得到了我。 我有這樣servs HTML和JS文件服務器:客戶端的Node.js javascript模塊

app.post('/game', function(req, res) { 
    handleGame(req, res); //it opens file using fs and forwards with res.end(readFile); 
}); 

app.get('/scripts/*', function(req, res) { 
    handleStatic(req, res); //same schema 
}); 

而現在,我登錄和現場game.html(從第一app.post收到)。 它裝的很好,initRight更改爲'bbbb',如果我在Firefox中打開'源代碼',我可以點擊並查看player.jsgame.js源代碼。 Hovewer,在game.js模塊的init()函數中,應該再次將initRight字段更改爲'AAAAA',但正如我所見,玩家模塊中的玩家功能在遊戲模塊內部是不可見的(我發現當我將socket.io處理程序移動到不同的模塊)。我這樣做是因爲我已經在不同的項目中看到過它,並且我真的不希望接下來會讓一些require.js獲得require()函數等。我的問題是它應該工作嗎?如果是的話爲什麼它可能不在這裏?

+0

很難說出你在問什麼。我建議你在這裏顯示與問題相關的代碼。 – Jivings 2013-04-07 15:58:24

回答

1

雖然你的問題很混亂,我沒有看到你的問題與NodeJS的連接,問題是你的JavaScript有多個問題。

我複製你的代碼到JSBin:

http://jsbin.com/iqudic/4/edit(和生活:http://jsbin.com/iqudic/4

然後,我清理了一點點。

您可能希望將關聯的屬性放在Player對象的實例上,而不是像您一樣將它們作爲新對象返回。

var Player = function(playerNick, playerId, playerSocket) { 
    this.nick = playerNick; 
    this.id = playerId; 
    this.socket = playerSocket; 
}; 

其次,這是造成AAAAAA不出現:

function init() { 
    // socket and socket.id should be defined before creating the Player 
    localPlayer = new Player('AAAAAA', socket.id, socket); 
    document.getElementById('initRight').innerHTML = localPlayer.nick; 
} 

你的代碼假定存在init函數中使用一個socket.idsocket對象。它在這條線上崩潰。您應該始終查看您最喜愛的Web瀏覽器的控制檯窗口。

socket is not defined

+0

謝謝你WiredPrairie,你的回答已經解決了我的兩個問題;) – aragornsql 2013-04-07 17:04:03