使用 apache mod_proxy_wstunnel 的 Websocket 不工作

Websocket with apache mod_proxy_wstunnel doesn't work

我已经做好了准备:Apache 2.4 + PHP7 + WebSocket
访问这个 link 看看我的准备工作
详情:
但是当我 运行 一个简单的 WebSocket 演示时,我遇到了一些问题。我不知道如何解决它们。

Websocket 对我来说是一个新概念。我了解到 Apache 2.4 支持 mod_proxy_wstunnel.so.
的 WebSockets 我的步数:

  1. 编辑httpd.conf,加载mod_proxymod_proxy_wstunnel(当然两者都在apachedir/modules)
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
  1. 添加代理(仍在 httpd 中)
ProxyPass /ws ws://10.180.0.123/
ProxyPassReverse /ws ws://10.180.0.123/
  1. 创建 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.start 阿帕奇

/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.

的任何信息
  1. 当我打开: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.

代理模块似乎没有加载,但是看我的截图:

返回 "hello" 的 PHP 脚本不是 WebSocket 服务器。

您需要 运行 一个实际的 WebSocket 服务器并将您的代理配置指向它。

查看 Ratchet 基于 PHP 的 WebSocket 服务器。

当您拥有 WebSocket 服务器 运行ning 时,您需要设置 Apache 配置。

假设它 运行s 在同一台机器和端口 8080 上:

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

在您的 client.html 中,您应该将 WebSocket 连接到 ws://10.180.0.123/ws