Two listen on the same server

2

I am creating a game server and in this game server will have several channels belonging to the same server, there I was thinking if it is possible to create two listen on the same server,

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');
});

Would this work normally?

    
asked by anonymous 05.05.2017 / 01:10

1 answer

2

You can create another instance of http and put it to 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
    
24.05.2017 / 15:32