首页 文章

在服务器上使用带有nodejs的socket.io,并使用apache作为反向代理

提问于
浏览
9

我正在尝试使用Node.js和Socket.IO来促进浏览器和客户端之间的消息传递,遵循the guide .

但是,我必须在Apache后面设置Node反向代理 . 因此,对于节点而不是example.com:8080,我使用的是example.com/nodejs/ .

这似乎会导致Socket.IO失去理智 . 这是我的节点应用程序

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

// this has to be here, otherwise the client tries to 
// send events to example.com/socket.io instead of example.com/nodejs/socket.io
io.set( 'resource', '/nodejs/socket.io' );

io.sockets.on('connection', function (socket) {

  socket.emit('bar', { one: '1'});

  socket.on('foo', function( data )
  {
    console.log( data );
  });

});

这是我的客户端文件的样子

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Socket.IO test</title>

  <script src="http://example.com/nodejs/socket.io/socket.io.js"></script>

  <script>

  var socket = io.connect('http://example.com/nodejs/');

  console.log( socket );

  socket.on( 'bar', function (data)
  {
    console.log(data);
    socket.emit( 'foo', {bar:'baz'} );
  });

  socket.emit('foo',{bar:'baz'});

  </script>
</head>
<body>
  <p id="hello">Hello World</p>
</body>
</html>

这里的问题是http://example.com/nodejs/socket.io/socket.io.js的脚本引用 . 它不会返回预期的javasscript内容 - 而是返回"Welcome to socket.io",就好像我点击http://example.com/nodejs/一样 .

知道如何让这个工作吗?

3 回答

  • 4

    这最终成为一个多管齐下的解决方案 .

    首先,在服务器端,我必须像这样设置 endpoints

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

    然后,在客户端,正确连接看起来像这样

    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(){} );
    

    在此之后,一切都奏效了 . 请记住,在此服务器上,Apache在内部将example.com/nodejs代理到端口8080 .

  • 0

    我不知道socket.io如何处理子目录上的请求't think this has anything to do with your apache proxy, but some ' quirks' . 在这里看到我的答案 . NGINX configuration to work with Socket.IO

    基本上,您需要使用此连接语句:

    var socket = io.connect('http://example.com', {resource:'nodejs/socket.io'});

  • 8

    如果有人感兴趣,只有这对我有用 . 用您的Nodejs端口替换端口3000

    您的VirtualHost中的Apache 2.2.14

    <IfModule mod_proxy.c>
          <Proxy *>
            Order allow,deny
            allow from all
          </Proxy>
        </IfModule>
    
        RewriteEngine on
    
        RewriteCond %{QUERY_STRING} transport=polling
        RewriteRule /(.*)$ http://localhost:3001/$1 [P]
    
        ProxyRequests off
        ProxyPass /socket.io/ ws://localhost:3001/socket.io/
        ProxyPassReverse /socket.io/ ws://localhost:3001/socket.io/
    

    客户端连接:

    var myIoSocket = io.connect($location.protocol() + '://' + $location.host(), {path: '/socket.io'});
    

    无需在Node.js端更改任何内容 .

相关问题