Recently, I've been delving into node.js and socket.io, but I'm struggling to eliminate the need to type "url:port" in the browser. Instead, I want users to simply enter the URL and have everything load up, similar to my work-in-progress single player game located here: (yes, I aim to make it multiplayer). However, I don't want the game to run on port 80/443 as those are reserved for the web server.
I desire a setup like this one: without having to include "ip/url:port". How can I achieve this?
Here's my app.js code:
// Setting up Socket.IO
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs');
// Running the server on port 9000
app.listen(9000);
// Sending index.html to the player
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);
});
}
// Connection listener
io.sockets.on('connection', function(client) {
console.log('New connection established.');
});
Here's the index.html content:
<html>
<head>
<meta charset="utf-8">
<title>Multiplayer Blocks</title>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<canvas id="canvas" style="background: #000000;">
<script>
// Socket.IO
var socket = io.connect('http://localhost:9000');
</script>
</body>
</html>
UPDATE: Ideally, I want the game to operate on Port 9000, while the index.html is accessible through a standard/portless URL like sub.domain.com/game/ instead of URL:Port.
UPDATE 2: I've condensed my question into just one inquiry as I realized my two issues are distinct.
UPDATE 3: Problem solved! I managed to figure it out myself, and it's surprisingly straightforward:
Here's the new simplified server code:
var io = require('socket.io').listen(9000);
// Connection listener
io.sockets.on('connection', function(client) {
console.log('Connection established.');
});
And here's the updated index.html (accessible via file://folder/index.html):
<html>
<head>
<meta charset="utf-8">
<title>Multiplayer Blocks</title>
<script src="http://localhost:9000/socket.io/socket.io.js"></script>
</head>
<body>
<canvas id="canvas" style="background: #000000;">
<script>
// Socket.IO
var socket = io.connect('http://localhost:9000');
</script>
</body>
</html>
Big thanks to everyone who offered assistance. It seems that Apache now serves the files as usual, while socket.io listens on port 9000 and connects the index.html (or any other file) to the server — meaning this file can be placed anywhere, even on another server or locally. Perfect solution! :))