Socket.io: Connect from one server to another

后端 未结 6 1091
星月不相逢
星月不相逢 2020-12-16 15:36

I\'m trying to make a nodejs(socket.io) server to communicate with another one. So the client emits an event to the \'hub\' server and this server emits an event to some sec

6条回答
  •  死守一世寂寞
    2020-12-16 15:52

    The native Node TCP module is probably what you want - I wanted to do what you're trying to do but it seems that the fact that WebSockets are strictly many-browser to server, or browser to many-server.

    You can weave a tcp strategy into your websocket logic.

    Using tcp:

    var net = require('net');
    var tcp = net.connect({port: 3000, host: 'localhost'});
    tcp.on('connect', function(){
      var buffer = new Buffer(16).fill(0);
      buffer.write('some stuff');
      tcp.write(buffer);
    });
    
    tcp.on('data', function(data){console.log('data is:', data)});
    tcp.on('end', cb);
    tcp.on('error', cb);
    

    I would use a bridge pattern with this:

    https://www.google.com/search?q=javascript+bridge+pattern&aq=f&oq=javascript+bridge+pattern&aqs=chrome.0.57.6617&sourceid=chrome&ie=UTF-8

    OR, use the Node Module https://npmjs.org/package/ws-tcp-bridge

    I also heard that using redis can be quite helpful - Socket.io uses this as a fallback.

    Hope this helps...

    Cheers

提交回复
热议问题