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:
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
ProxyPass /ws ws://10.180.0.123/
ProxyPassReverse /ws ws://10.180.0.123/
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:
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.
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:
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With