5

I have made a good preparation: Apache 2.4 + PHP7 + WebSocket
Visit this link to see my preparation
details: Cannot load modules/mod_proxy_wstunnel.so into server
But when I run a simple WebSocket demo, I am meet with some problems. I have no idea how to solve them.

Websocket is a new concept to me. I have learned that Apache 2.4 supports WebSockets with mod_proxy_wstunnel.so.
My steps:

  1. Edit httpd.conf, load mod_proxy and mod_proxy_wstunnel (of course both are in apachedir/modules)
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
  1. Add proxy (still in httpd)
ProxyPass /ws ws://10.180.0.123/
ProxyPassReverse /ws ws://10.180.0.123/
  1. create HTML client and PHP server (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.start apache

/usr/local/apache2/bin/apachectl start

My questions:

  1. When I open: 10.180.0.123/client.html, I got the following error:
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  

I guess I should write some code in server.php, and run it. Am I right? And what should I write. I didn't find any information on google.

  1. When I open: 10.180.0.123/ws/client.html, I get the following Apache error logs
[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.

It seems the proxy module was not be loaded, but see my screenshot:

loaded modules screenshot

1 Answer 1

3

A PHP script returning "hello" is not a WebSocket server.

You need to run an actual WebSocket server and point your proxy configuration to it.

Look into Ratchet for a PHP based WebSocket server.

When you have the WebSocket server running, you need to setup your Apache config.

Assuming it runs on the same machine and on port 8080:

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

And inside your client.html, you should connect your WebSocket to ws://10.180.0.123/ws.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your response.Can you tell me the relationship between mod_proxy_wstunnel and Ratchet?Can i build my websocket server base on mod_proxy_wstunnel?Do i still need Ratchet?
mod_proxy_wstunnel is just a module for Apache, so proxying supports WebSockets. You only need a reverse proxy, if you want to serve WebSocket connections from the same port as you web server or to support SSL encrypted WebSocket connection without the need for the actual WebSocket to support it. But you definitely need a separate server serving WebSockets like Ratchet.
Your explaination is very clear.Thank you very much.
Do you have some ideas about my issue 2:[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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.