如何使用 Ratchet 和 Apache 将 WSS 与 HTTPS 结合使用?

问题描述 投票:0回答:1

我使用 PHP 和 Apache 制作了一个网站,并使用 Ratchet 与我在 Raspberry Pi 上托管的 Weksockets 一起工作,Raspberry Pi 运行 Raspbian GNU/Linux 11(牛眼)。当我使用 Cerbot 设置 SSL 认证后,我进行了尝试,发现我必须使用 WSS 而不是 WS。 我有这些文件: 应用程序.js :

var hostname = window.location.hostname;
var port = 8001;
var ws = new WebSocket('ws://' + hostname + ':' + port + "?id_class=" + id + "&id_user=" + id_user + "&chap_name=" + chap_name + "");

和 server.php 文件:

<?php

require __DIR__ . '/../vendor/autoload.php';

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

require "EditorServer.php";
require __DIR__ . "/../vendor/autoload.php";

$port = isset($_SERVER["CHAT_SERVER_PORT"]) ? $_SERVER["CHAT_SERVER_PORT"] : 8001;
$bindAddr = isset($_SERVER['CHAT_BIND_ADDR']) ? $_SERVER['CHAT_BIND_ADDR'] : '0.0.0.0';


$file = "lecon.txt";

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new MyApp\EditorServer($file)
        )
    ),
    $port, $bindAddr
);

printf("Websocket Editor server running on %s:%s.\n--\n", $bindAddr, $port);
$server->run();

所以我尝试在 app.Js 文件中放入“wss”而不是“ws”,但在 Google Chrome 控制台中显示“app.js:14 WebSocket 连接到 'wss://MYSITE:8001/?id_class =1&id_user=6&chap_name='失败:"。我查了很多教程,但每次都不起作用。那么你知道如何让它发挥作用吗?

php ssl websocket ratchet
1个回答
0
投票

您还需要相应调整端口号。 WS 和 WSS 始终在不同的端口上提供服务。

© www.soinside.com 2019 - 2024. All rights reserved.