Express サーバー用の Node.js と websockets 用の Socket.io を使用して、マルチプレイヤー HTML5 ゲームを開発しています。アプリケーションがリッスンしているポートにブラウザでアクセスしようとすると、空白のページが表示されます。ただし、index.html ファイルに (サーバーの実行中に) 直接アクセスすると、完全に機能します。
「ファイルに直接アクセスする」というのは、 file:///C:/Program%20Files/nodejs/SupermarketChallenge/public/index.html を意味することに注意してください。
これは、プレイするためにローカルホスト経由でアクセスする必要がある同居人と一緒にゲームをテストするまでは問題ありませんでした。
関連するサーバーコードは次のとおりです。
var express = require('express'),
//Create an express server
app = express.createServer(),
//attach socket.io to the server
supermarket = require('socket.io').listen(app);
//Configure a static directory for public files
app.configure(function() {
app.use(express.static(__dirname + '/public'));
});
//set the port to listen to
app.listen(3000);
そしてクライアント:
var socket = io.connect('http://localhost:3000');