gpt4 book ai didi

javascript - Ratchet Websocket Apache2 SSL 无法连接到 PHP websocket

转载 作者:行者123 更新时间:2023-12-04 16:13:51 26 4
gpt4 key购买 nike

一直在研究 Ratchet over SSL 的问题,并一直试图通过启用 proxy.load 和 proxy_wstunnel.load mods 来让我的 Apache2 服务器配置工作。

/etc/apache2/mods-enabled:
proxy.conf -> ../mods-available/proxy.conf
proxy.load -> ../mods-available/proxy.load
proxy_wstunnel.load -> ../mods-available/proxy_wstunnel.load

我在 apache2.conf 的末尾添加了以下行:
ProxyPass /wss2/ ws://domain.com:8080
PHP socket-server.php 文件:
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Websocket\SocketControl;
use Ratchet\Session\SessionProvider;
use Symfony\Component\HttpFoundation\Session\Storage\Handler;

require dirname(__DIR__) . '/vendor/autoload.php';

$memcache = new Memcache;
$memcache->connect('localhost', 11211);

$session = new SessionProvider(
new SocketControl,
new Handler\MemcacheSessionHandler($memcache)
);

$server = IoServer::factory(
new HttpServer(
new WsServer(
$session
)
),
8080
);

$server->run();

尝试连接socket服务器的相关JS代码:
var websocket = {
wsUri:"wss://domain.com/wss2/NNN",
socket:null,
cryptKey:null,
init:function(){
try {
if (typeof MozWebSocket == "function")
WebSocket = MozWebSocket;
if ( websocket.socket && websocket.socket.readyState == 1 )
websocket.socket.close();

websocket.socket = new WebSocket( websocket.wsUri );
websocket.socket.onopen = websocket.onopen.bind();
websocket.socket.onclose = websocket.onclose.bind();
websocket.socket.onmessage = websocket.onmessage.bind();
websocket.socket.onerror = websocket.onerror.bind();

} catch (exception) {
console.log("ERROR: " + exception);
}
},

尝试连接时收到的错误:
WebSocket connection to 'wss://domain.com/wss2/NNN' failed: Error during WebSocket handshake: Unexpected response code: 502

最佳答案

我想我发布这个帖子晚了,但我在这里也有一个带有路由的工作示例。根据最新的 Ratchet 版本,代码运行良好。

<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
require __DIR__ . '/vendor/autoload.php';

use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Psr\Http\Message\RequestInterface;
use Ratchet\Http\Router;
use Ratchet\Http\HttpServerInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\HttpFoundation\Request;

$routes = new RouteCollection;
$loop = React\EventLoop\Factory::create();

$decorated = new WsServer(new ChatRoom);
$decorated->enableKeepAlive($loop);
$routes->add('chat-room', new Route('/chat-room', array('_controller' => $decorated), array('Origin' => $GLOBALS['Address']),
array(), $GLOBALS['Address'], array(), array('GET')));

$decorated = new WsServer(new SingleChat);
$decorated->enableKeepAlive($loop);
$routes->add('single-chat', new Route('/single-chat', array('_controller' => $decorated), array('Origin' => $GLOBALS['Address']),
array(), $GLOBALS['Address'], array(), array('GET')));

$decorated = new WsServer(new Ratchet\Server\EchoServer);
$decorated->enableKeepAlive($loop);
$routes->add('echo', new Route('/echo', array('_controller' => $decorated), array('Origin' => $GLOBALS['Address']),
array(), $GLOBALS['Address'], array(), array('GET')));


$app = new HttpServer(new Router(new UrlMatcher($routes, new RequestContext)));

$secure_websockets = new \React\Socket\Server('0.0.0.0:8091', $loop);
$secure_websockets = new \React\Socket\SecureServer($secure_websockets, $loop, [
'local_cert' => 'path/to/certificate.crt',
'local_pk' => '/path/to/your-key.key',
'verify_peer' => false
]);

$secure_websockets_server = new \Ratchet\Server\IoServer($app, $secure_websockets, $loop);
$secure_websockets_server->run();

问候

关于javascript - Ratchet Websocket Apache2 SSL 无法连接到 PHP websocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36903628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com