Using socket.io with nodejs on a server with apache as a reverse proxy

前端 未结 3 407
执笔经年
执笔经年 2021-01-02 06:08

I\'m attempting to use Node.js with Socket.IO to faciliate messaging between the browser and client, following the guide.

However, I had to setup Node reverse-proxie

3条回答
  •  自闭症患者
    2021-01-02 06:48

    This ended up being a multi-pronged solutions.

    First, on the server end of things, I had to set up the endpoints like this

    var io = require('socket.io').listen(8080);
    
    var rootSockets = io.of('/nodejs').on('connection', function(socket)
    {
      // stuff
    });
    
    var otherSockets = io.of('nodejs/other').on('connection', function(socket)
    {
      // stuff
    });
    

    Then, on the client-side, to properly connect looks like this

    var socket = io.connect(
        'http://example.com/nodejs/'
      , {resource: 'nodejs/socket.io'}
    );
    
    // The usage of .of() is important
    socket.of('/nodejs').on( 'event', function(){} );
    socket.of('/nodejs/other').on( 'event', function(){} );
    

    After this, it all worked. Remember, on this server Apache is proxying example.com/nodejs to port 8080 internally.

提交回复
热议问题