2016-09-28 134 views
0

我已經做了很好的準備:Apache的2.4 + PHP7 +的WebSocket
訪問此鏈接,看看我的準備
細節:Cannot load modules/mod_proxy_wstunnel.so into server
但是當我運行一個簡單的WebSocket的演示,我我遇到了一些問題。我不知道如何解決它們。的WebSocket與Apache mod_proxy_wstunnel不起作用

Websocket對我來說是一個新概念。我瞭解到Apache 2.4支持mod_proxy_wstunnel.so的WebSockets。
我的步驟:

  1. 編輯的httpd.conf,負載mod_proxymod_proxy_wstunnel(當然均爲在apachedir /模塊)
LoadModule proxy_module modules/mod_proxy.so 
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so 
  • 添加代理(仍在的httpd)
  • ProxyPass /ws ws://10.180.0.123/ 
    ProxyPassReverse /ws ws://10.180.0.123/ 
    
  • 創建HTML客戶機和PHP服務器(cd apachedir/htdocs
  • client.html

    <html> 
    <script> 
        var socket = new WebSocket("ws://10.180.0.123/server.php"); 
        socket.onopen = function(e){ 
         console.log("open success"); 
        } 
        socket.onmessage = function(e){ 
         console.log("mes:"+e); 
        } 
        //socket.close(); 
        socket.send("Hello World"); 
    </script> 
    <html> 
    

    server.php

    <?php 
        echo "hello"; 
    ?> 
    

    4.啓動Apache的

    /usr/local/apache2/bin/apachectl start 
    

    我的問題:

    1. 當我打開:10.180.0.123/client.html,我得到了以下錯誤:
    Uncaught InvalidStateError: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state. 
    WebSocket connection to 'ws://10.180.0.123/server.php' failed: Error during WebSocket handshake: Unexpected response code: 200 
    

    我想我應該在server.php寫一些代碼,並運行它。我對嗎?我該寫些什麼?我沒有在Google上找到任何信息。

  • 當我打開:10.180.0.123/ws/client.html,我得到以下Apache的錯誤日誌
  • [Wed Sep 28 00:14:54.063989 2016] [proxy:warn] [pid 6646:tid 140326217934592] [client 10.230.0.93:57508] AH01144: No protocol handler was valid for the URL /ws/client.html. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule. 
    

    看來代理模塊沒有被加載,但看到我的截圖:

    loaded modules screenshot

    回答

    1

    的PHP腳本返回 「你好」 是不是WebSocket的服務器。

    您需要運行一個實際的WebSocket服務器並將您的代理配置指向它。

    調查一個基於PHP的WebSocket服務器的Ratchet

    當您運行WebSocket服務器時,您需要設置您的Apache配置。

    假設它運行在同一臺機器上,並在8080端口上:

    ProxyPass /ws ws://localhost:8080/ 
    ProxyPassReverse /ws ws://localhost:8080/ 
    

    和你client.html裏面,你應該將WebSocket的連接ws://10.180.0.123/ws

    +0

    謝謝你的迴應。你能告訴我mod_proxy_wstunnel和Ratchet之間的關係嗎?我可以在mod_proxy_wstunnel上建立我的websocket服務器嗎?我還需要棘輪嗎? – Does

    +0

    'mod_proxy_wstunnel'只是Apache的一個模塊,所以代理支持WebSockets。如果您想從Web服務器的同一端口提供WebSocket連接,或者需要SSL加密的WebSocket連接,而無需實際的WebSocket支持,則只需要一個反向代理。但你絕對需要一個單獨的服務器來提供像棘輪這樣的WebSockets。 –

    +0

    你的解釋很清楚,非常感謝。 – Does

    相關問題