我正在使用NodeJS和Socket.io。这是我的index.js
var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var fs = require('fs');
app.listen(3000);
console.log("Listening on port 3000");
function handler(req, res) {
fs.readFile(__dirname + '/index.html', function( err, data ) {
if( err ) {
res.writeHead( 500 );
return res.end('Error loading index.html');
}
res.writeHead( 200 );
res.end( data );
});
}
这是我的index.html
<!DOCTYPE html>
<html>
<head>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<title>Impact Game</title>
<style type="text/css">
html,body {
background-color: #333;
color: #fff;
font-family: helvetica, arial, sans-serif;
margin: 0;
padding: 0;
font-size: 12pt;
}
#canvas {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
</style>
<script src="lib/impact/impact.js"></script>
<script src="lib/game/main.js"></script>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>
当我去localhost:3000时,我得到&#34; SyntaxError:期望的表达式,得到&#39;&lt;&#39; main.js:1:0&#34;
我试图遵循的教程是针对ImpactJS + NodeJS + Socket.IO制作多人游戏,但这是我迄今为止最远的。
以下是main.js的内容
ig.module(
'game.main'
)
.requires(
'impact.game',
'impact.font'
)
.defines(function(){
MyGame = ig.Game.extend({
// Load a font
font: new ig.Font( 'media/04b03.font.png' ),
init: function() {
// Initialize your game here; bind keys etc.
},
update: function() {
// Update all entities and backgroundMaps
this.parent();
// Add your own, additional update code here
},
draw: function() {
// Draw all entities and backgroundMaps
this.parent();
// Add your own drawing code here
var x = ig.system.width/2,
y = ig.system.height/2;
this.font.draw( 'It Works!', x, y, ig.Font.ALIGN.CENTER );
}
});
// Start the Game with 60fps, a resolution of 320x240, scaled
// up by a factor of 2
ig.main( '#canvas', MyGame, 60, 320, 240, 2 );
});
答案 0 :(得分:5)
看看这个:
function handler(req, res) { fs.readFile(__dirname + '/index.html', function( err, data ) {
无论浏览器要求什么,您都将提供index.html
因此,当浏览器要求lib/impact/impact.js
或lib/impact/main.js
时,您会向其提供index.html
的内容。
您需要注意requested(req.url
)的内容并返回正确的内容。