Two Systems on the same server

Asked

Viewed 55 times

2

I am creating a game server and in this game server will have several channels belonging to the same server, ai was wondering if it is possible to create two Listen on the same server, example:

var net = require('net');

var server = net.createServer((socket) => {
    socket.on('data', onDataFunction());
});

server.listen(8945, '192.168.0.8', () => {
    console.log('Canal 1 iniciado');
});

server.listen(8945, '192.168.0.15', () => {
    console.log('Canal 2 iniciado');
});

That would work normally ?

  • Yes, but not at the same door.

1 answer

2


You can create another http instance and have it listen to the port of your interest. Example:

    var http = require('http');

    http.createServer(onRequest_a).listen(9011);
    http.createServer(onRequest_b).listen(9012);

    function onRequest_a (req, res) {
      res.write('Response from 9011\n');
      res.end();
    }

    function onRequest_b (req, res) {
      res.write('Response from 9012\n');
      res.end();
    }

Test with browser or Curl:

    $ curl http://localhost:9011
    Response from 9011

    $ curl http://localhost:9012
    Response from 9012

Browser other questions tagged

You are not signed in. Login or sign up in order to post.