php Ratchet websocket SSL连接?

标签 php ssl websocket ratchet

我有一个 Ratchet 聊天服务器文件

use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use MyAppChat\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
    new WsServer(
        new Chat()
    )
  , 26666
);
$server->run();

我使用 Websocket 连接 ws 并且工作正常

if ("WebSocket" in window) {
    var ws = new WebSocket("ws://ratchet.mydomain.org:8888");
    ws.onopen = function() {
        // Web Socket is connected. You can send data by send() method.
        ws.send("message to send");
    };
    ws.onmessage = function (evt) { 
        var received_msg = evt.data;
    };
    ws.onclose = function() { 
        // websocket is closed. 
    };
} else {
  // the browser doesn't support WebSocket.
}

我想要安全连接,所以我尝试使用 SSL 连接但不起作用。

if ("WebSocket" in window) {
    var ws = new WebSocket("wss://ratchet.mydomain.org:8888");
    ws.onopen = function() {
        // Web Socket is connected. You can send data by send() method.
        ws.send("message to send");
    };
    ws.onmessage = function (evt) { 
        var received_msg = evt.data;
    };
    ws.onclose = function() { 
        // websocket is closed. 
    };
} else {
  // the browser doesn't support WebSocket.
}

我的问题是如何使用 SSL 连接 websocket

有什么想法吗?

最佳答案

如果您使用的是 Apache Web 服务器(2.4 或更高版本),请在 httpd.conf 文件中启用这些模块:

  1. mod_proxy.so
  2. mod_proxy_wstunnel.so

将此设置添加到您的 httpd.conf 文件中

ProxyPass /wss2/ ws://ratchet.mydomain.org:8888/

当您需要 WSS 连接时,在您的 JavaScript 调用中使用此 URL:

var ws = new WebSocket("wss://ratchet.mydomain.org/wss2/NNN");

重新启动 Apache Web 服务器并确保在应用设置(telnet 主机名端口)之前您的 Ratchet 工作程序(Web 套接字连接)已打开。

关于php Ratchet websocket SSL连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16979793/

相关文章:

python - 如何使用 Python 修复 JSON 中未加引号的 nan 值

php - 从 Zend Radio 表单元素中删除一个选项

php - 循环遍历excel中的记录并使用php中excel文件中的值更新mysql数据库中的特定行

ssl - 使用 OpenSSL 正确关闭 SSL 连接

node.js - Azure AMS Node js 应用程序无法建立 TLS 1.2 连接

java - 无法识别分割字符

javascript - 使用安全 WebSocket 连接的 Django channel - WSS ://

php - 递归重命名文件名并转义特殊字符的 Shell 脚本?

javascript - 如何从动态输入字段添加数字?

python - 如何在 python 中编辑请求以添加 TLS 设置?