Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket with apache mod_proxy_wstunnel doesn't work

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

like image 431
Does Avatar asked Oct 18 '22 01:10

Does


1 Answers

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.

like image 119
gre_gor Avatar answered Oct 21 '22 17:10

gre_gor