【问题标题】:Ratchet Websocket Apache2 SSL unable to make connection to PHP websocketRatchet Websocket Apache2 SSL 无法连接到 PHP websocket
【发布时间】:2016-08-22 13:35:11
【问题描述】:

一直在研究 Ratchet over SSL 的问题,并试图通过启用 proxy.load 和 proxy_wstunnel.load 模块来使我的 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

【问题讨论】:

    标签: javascript php apache sockets ratchet


    【解决方案1】:

    我想我发布这个比较晚,但我这里也有一个路由的工作示例。根据最新的棘轮版本,该代码运行良好。

    <?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();
    

    问候

    【讨论】:

      【解决方案2】:

      问题出在您使用的网址中:

      wsUri:"wss://domain.com/wss2/NNN",
      

      你可以试试:

      wsUri:"wss://domain.com/wss2",
      

      因为您在配置中链接到该 URL

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-24
        • 1970-01-01
        • 2019-10-27
        • 2013-06-03
        • 2019-08-14
        相关资源
        最近更新 更多