【问题标题】:Ratchet - How to prevent other websites from connect to my websocket server?Ratchet - 如何防止其他网站连接到我的 websocket 服务器?
【发布时间】:2019-08-27 21:17:54
【问题描述】:

http://socketo.me/docs/origin 有页面在讨论,但完全不清楚如何实现。

这是我当前的代码(来自http://socketo.me/docs/push 的教程):

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

$loop   = React\EventLoop\Factory::create();
$pusher = new MyApp\Pusher;

// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$pull->on('message', array($pusher, 'onBlogEntry'));

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server('0.0.0.0:8080', $loop); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
    new Ratchet\Http\HttpServer(
        new Ratchet\WebSocket\WsServer(
            new Ratchet\Wamp\WampServer(
                $pusher
            )
        )
    ),
    $webSock
);

$loop->run();

按照http://socketo.me/docs/origin 的教程,它说我应该将变量$checkedApp 添加到类HttpServer 的构造函数中。我检查了这个类的souce code,它只使用了__constructor() 中的一个参数,正如你所见,我已经将一个值传递给了这个构造函数,它是WsServer 类的一个实例。 MyHttpApp 类也不存在。

【问题讨论】:

    标签: php websocket ratchet


    【解决方案1】:

    Aftet 我在源代码中搜索了一下,我找到了位于https://github.com/ratchetphp/Ratchet/blob/master/src/Ratchet/App.php 的文件App.php 并实现OriginChecker 我只需将变量$webServer 的值更改为下面的值:

    $webServer = new Ratchet\Server\IoServer(
        new Ratchet\Http\HttpServer(
            new Ratchet\Http\OriginCheck(
                new Ratchet\WebSocket\WsServer(
                    new Ratchet\Wamp\WampServer(
                        $pusher
                    )
                ),
                array('mydomain.com') //this is the only domain that can connect to the websocket
            )
        ),
        $webSock
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 2015-09-01
      • 2019-07-21
      • 2015-08-29
      相关资源
      最近更新 更多