user2337969
user2337969

Reputation: 703

socket.io: How to abandon the url:port scheme?

I started playing with node.js and socket.io and can't figure out how to make it so you don't have to type in "url:port" in the browser but just the url. Instead i want to only type in the url and then everything should come up, like in my unfinished single player game here: http://space.bonsaiheld.org/ (you guess right: i want to make it multiplayer). The game must not run on port 80/443 as those ports are dedicated to the webserver.

It should be like this: http://ondras.zarovi.cz/games/just-spaceships/ and not "ip/url: port". How to do that?

app.js

// Socket.IO
var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs');

// Start the server on port 9000
app.listen(9000);

// Send 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.');
}); // Connection listener

index.html

   <html>
    <head>
    <meta charset="utf-8">
    <title>Mehrspielerklötzchen</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>

EDIT: I basically want that the game runs on Port 9000, but the index.html be reachable over a normal/portless URL like sub.domain.com/game/ and not URL:Port.

EDIT 2: Reduced the question to only one as my two problems happen to be seperate ones.

EDIT 3: Solved! I figured it out myself, it's totally easy:

New extremly simplified server code:

var io = require('socket.io').listen(9000);

// Connection listener
io.sockets.on('connection', function (client)
{
    console.log('Connection established.');
}); // Connection listener

New index.html (reachable over file://folder/index.html)

<html>
<head>
<meta charset="utf-8">
<title>Mehrspielerklötzchen</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>

Thanks to everyone who helped. I guess it would have also worked with port redirecting. But it seems that i don't even need it. Now Apache serves the files as always but socket.io listens for port 9000 and the index.html (or whatever file i want) connects to the server! This also means that this file could now lie EVERYWHERE, even on another server or local. Perfect. :))

Upvotes: 1

Views: 1125

Answers (1)

Bergi
Bergi

Reputation: 664195

Use the default HTTP/HTTPS ports 80/443 (which are default ports for WS/WSS as well).

Of course you can redirect them to other, "internal" ports.

Upvotes: 1

Related Questions