如何使用Socket.io设置使Apache和Node.js正常工作?

如何使用Socket.io设置使Apache和Node.js正常工作?,node.js,apache,socket.io,Node.js,Apache,Socket.io,从Node.js/Socket.io开始。最终,我希望在使用Node.js服务器推送和扩充部分内容的同时,为Apache中的页面提供服务。这只是一个小测试,看看它是如何工作的。以下是我的设置: Apache在端口上运行8080 Node.js在端口8000上运行 我将index.html页面(见下文)复制到各自的根目录中 当我打开http://localhost:8000一切正常,我可以看到完整的消息交换 当我打开http://localhost:8080一切看起来都正常(根据请求和响应头以

从Node.js/Socket.io开始。最终,我希望在使用Node.js服务器推送和扩充部分内容的同时,为Apache中的页面提供服务。这只是一个小测试,看看它是如何工作的。以下是我的设置:

  • Apache在端口上运行8080
  • Node.js在端口8000上运行
我将index.html页面(见下文)复制到各自的根目录中

  • 当我打开
    http://localhost:8000
    一切正常,我可以看到完整的消息交换

  • 当我打开
    http://localhost:8080
    一切看起来都正常(根据请求和响应头以及Node.js日志,与1相比)。但我可以在Node.js或Firebug日志中看到没有消息交换

  • 当我第一次打开Node.js所服务的页面时,关闭它,然后打开Apache所服务的页面。我可以在Node.js终端中看到“已连接”、“已断开连接”和“已连接”消息

  • 问题是:如何使case2.起作用

    第页:

    因此,我知道以下问题:

    但我还是不能让它工作

    我试过:

    • “将路径更改为/socket.io client/dist/socket.io.js”
    在浏览器中:ReferenceError:未定义io。当我转到
    http://localhost:8000/socket.io-client/dist/socket.io.js
    欢迎使用socket.io。答复

    在服务器日志中:info-unhandled socket.io url

    • “从Apache静态服务器提供socket.io.js”
    那怎么办?Apache不知道如何处理
    /socket.io/socket.io.js
    node\u模块
    automagic

    • dark_ruby建议设置一个代理
    我将Apache根目录中index.html页面中的脚本源更改为:

    <script src="http://localhost:8080/socket.io/"></script>
    
    这样的配置打开
    http://localhost:8080
    与上面的2中的结果相同。

    $ node node-server.js
       info  - socket.io started
       debug - served static content /socket.io.js
       debug - client authorized
       info  - handshake authorized U9xPRw_0rRpsv-K9qVkS
       debug - setting request GET /socket.io/1/websocket/U9xPRw_0rRpsv-K9qVkS
       debug - set heartbeat interval for client U9xPRw_0rRpsv-K9qVkS
       debug - client authorized for 
       debug - websocket writing 1:: 
       debug - emitting heartbeat for client U9xPRw_0rRpsv-K9qVkS
       debug - websocket writing 2:: 
       debug - set heartbeat timeout for client U9xPRw_0rRpsv-K9qVkS
       debug - got heartbeat packet
       debug - cleared heartbeat timeout for client U9xPRw_0rRpsv-K9qVkS
       debug - set heartbeat interval for client U9xPRw_0rRpsv-K9qVkS
       ... 
       info  - transport end (undefined)
       debug - set close timeout for client U9xPRw_0rRpsv-K9qVkS
       debug - cleared close timeout for client U9xPRw_0rRpsv-K9qVkS
       debug - cleared heartbeat interval for client U9xPRw_0rRpsv-K9qVkS
       debug - discarding transport
    

    i、 e.无消息。

    您的io.sockets位于处理程序函数内部。 我假设当您向NodeJS服务器发出请求时会调用它。在此之前,NodeJS服务器永远不会执行您的IO代码

    尝试将其移出处理程序并添加到app.js中


    无需更改路径或代理。你最初发布的代码很好。问题不在于加载socket.io.js文件。但是节点服务器检测io事件。

    尝试在apache中设置代理,我知道nginx可以做到这一点,非常肯定apache也可以做到well@dark_ruby我试过了,但还是没有乐趣。请查看更新的问题。我不知道足够的Apache/socket.io来回答您,但您应该看看sails.js框架,它默认包括socket.io,可能还有一些“魔法”,可以帮助您解决问题!这是关于插座的文件:Doh!好的。这就解释了一切,是的,不需要代理。顺便说一句,这正是你要做的。谢谢大家!@nightgaunt我的io.socket代码如您所述位于app.js文件中,但我在托管网站时仍面临问题。请查看:
    <script src="http://localhost:8080/socket.io/"></script>
    
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    ProxyPass /socket.io/ http://localhost:8000/socket.io/socket.io.js
    
    $ node node-server.js
       info  - socket.io started
       debug - served static content /socket.io.js
       debug - client authorized
       info  - handshake authorized U9xPRw_0rRpsv-K9qVkS
       debug - setting request GET /socket.io/1/websocket/U9xPRw_0rRpsv-K9qVkS
       debug - set heartbeat interval for client U9xPRw_0rRpsv-K9qVkS
       debug - client authorized for 
       debug - websocket writing 1:: 
       debug - emitting heartbeat for client U9xPRw_0rRpsv-K9qVkS
       debug - websocket writing 2:: 
       debug - set heartbeat timeout for client U9xPRw_0rRpsv-K9qVkS
       debug - got heartbeat packet
       debug - cleared heartbeat timeout for client U9xPRw_0rRpsv-K9qVkS
       debug - set heartbeat interval for client U9xPRw_0rRpsv-K9qVkS
       ... 
       info  - transport end (undefined)
       debug - set close timeout for client U9xPRw_0rRpsv-K9qVkS
       debug - cleared close timeout for client U9xPRw_0rRpsv-K9qVkS
       debug - cleared heartbeat interval for client U9xPRw_0rRpsv-K9qVkS
       debug - discarding transport