Ratchet PHP WAMP - React / ZeroMQ - 特定用户广播

2023-12-21

Note: 这是not这个问题 https://stackoverflow.com/questions/17583903/how-to-get-the-connection-object-of-a-specific-user它利用MessageComponentInterface。我在用WampServerInterface相反,所以这个问题具体涉及该部分。我需要带有代码示例和解释的答案,因为我认为这对将来的其他人有帮助。

尝试对单个用户进行循环推送

我正在使用 Ratchet 和 ZeroMQ 的 WAMP 部分,目前我有一个工作版本推送集成教程 http://socketo.me/docs/push.

我正在尝试执行以下操作:

  • Zeromq 服务器已启动并正在运行,准备记录订阅者和取消订阅者
  • 用户通过 websocket 协议在浏览器中连接
  • A loop开始发送数据到特定用户谁要求的
  • 当用户断开连接时,该用户数据的循环将停止

我有第 (1) 点和第 (2) 点工作,但我遇到的问题是第三点:

首先:如何仅向每个特定用户发送数据?广播将其发送给每个人,除非“主题”最终可能是个人用户 ID?

第二:我有一个很大的安全问题。如果我要从客户端发送哪个用户 ID 想要订阅(这似乎是我需要的),那么用户只需将变量更改为另一个用户的 ID,然后就会返回他们的数据。

第三:我必须运行一个单独的 php 脚本包含用于启动实际循环的 Zeromq 代码。我不确定这是最好的方法,我宁愿让它完全在代码库中工作,而不是单独的 php 文件。这是我需要整理的一个主要领域。

以下代码显示了我目前拥有的内容。

仅从控制台运行的服务器

我逐字输入php bin/push-server.php运行这个。订阅和取消订阅都会输出到此终端以用于调试目的。

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

$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5555');
$pull->on('message', array($pusher, 'onMessage'));

$webSock = new React\Socket\Server($loop);
$webSock->listen(8080, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new Ratchet\Server\IoServer(
    new Ratchet\WebSocket\WsServer(
        new Ratchet\Wamp\WampServer(
            $pusher
        )
    ),
    $webSock
);

$loop->run();

通过 websocket 发送数据的 Pusher

我省略了无用的东西并专注于onMessage() and onSubscribe()方法。

public function onSubscribe(ConnectionInterface $conn, $topic) 
{
    $subject = $topic->getId();
    $ip = $conn->remoteAddress;

    if (!array_key_exists($subject, $this->subscribedTopics)) 
    {
        $this->subscribedTopics[$subject] = $topic;
    }

    $this->clients[] = $conn->resourceId;

    echo sprintf("New Connection: %s" . PHP_EOL, $conn->remoteAddress);
}

public function onMessage($entry) {
    $entryData = json_decode($entry, true);

    var_dump($entryData);

    if (!array_key_exists($entryData['topic'], $this->subscribedTopics)) {
        return;
    }

    $topic = $this->subscribedTopics[$entryData['topic']];

    // This sends out everything to multiple users, not what I want!!
    // I can't send() to individual connections from here I don't think :S
    $topic->broadcast($entryData);
}

开始循环使用上述 Pusher 代码的脚本

这是我的问题 - 这是一个单独的 php 文件,希望将来可以集成到其他代码中,但目前我不确定如何正确使用它。我是否从会话中获取用户的 ID?我仍然需要从客户端发送它......

// Thought sessions might work here but they don't work for subscription
session_start();
$userId = $_SESSION['userId'];

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

$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:5555");

$i = 0;
$loop->addPeriodicTimer(4, function() use ($socket, $loop, $userId, &$i) {

   $entryData = array(
       'topic'     => 'subscriptionTopicHere',
       'userId'    => $userId
    );
    $i++;

    // So it doesn't go on infinitely if run from browser
    if ($i >= 3)
    {
        $loop->stop();
    }

    // Send stuff to the queue
    $socket->send(json_encode($entryData));
});

最后是客户端js订阅

$(document).ready(function() { 

    var conn = new ab.Session(
        'ws://localhost:8080' 
      , function() {            
            conn.subscribe('topicHere', function(topic, data) {
                console.log(topic);
                console.log(data);
            });
        }
      , function() {          
            console.warn('WebSocket connection closed');
        }
      , {                       
            'skipSubprotocolCheck': true
        }
    );
});

结论

上面的内容有效,但我确实需要弄清楚以下内容:

  • 如何向个人用户发送个人消息?当他们访问在 JS 中启动 websocket 连接的页面时,我是否也应该启动将内容推送到 PHP 队列中的脚本(zeromq)?这就是我目前手动做的事情,它只是感觉不对劲.

  • 当从 JS 订阅用户时,从会话中获取用户 ID 并从客户端发送它是不安全的。这可能是伪造的。请告诉我有一个更简单的方法,如果有的话,如何实现?


Note:我的回答在这里does not包括对 ZeroMQ 的引用,因为我不再使用它了。但是,我确信如果需要,您将能够通过此答案了解如何使用 ZeroMQ。

Use JSON

首先也是最重要的,Websocket RFC https://www.rfc-editor.org/rfc/rfc6455 and WAMP规格 http://wamp.ws/spec/声明要订阅的主题必须是string。我在这里有点作弊,但我仍然遵守规范:我改为传递 JSON。

{
    "topic": "subject here",
    "userId": "1",
    "token": "dsah9273bui3f92h3r83f82h3"
}

JSON 仍然是一个字符串,但它允许我传递更多数据来代替“主题”,并且 PHP 可以很简单地执行json_decode()在另一端。当然,您应该验证您是否确实收到了 JSON,但这取决于您的实现。

那么我在这里经历了什么,为什么?

  • Topic

主题是用户订阅的主题。您可以使用它来决定将哪些数据传递回用户。

  • UserId

显然是用户的ID。你must使用下一部分验证该用户是否存在并且允许订阅:

  • Token

这应该是一个one use随机生成的令牌,在 PHP 中生成,并传递给 JavaScript 变量。当我说“一次使用”时,我的意思是每次重新加载页面时(并且,通过扩展,在每个 HTTP 请求中),您的 JavaScript 变量应该有一个新的令牌。该令牌应根据用户 ID 存储在数据库中。

然后,一旦发出 websocket 请求,您就可以将令牌和用户 ID 与数据库中的内容相匹配,以确保用户确实是他们所说的人,并且他们没有乱搞 JS 变量。

注意:在您的事件处理程序中,您可以使用$conn->remoteAddress获取连接的 IP,因此如果有人尝试恶意连接,您可以阻止他们(记录他们或其他内容)。

为什么这有效?

它之所以有效,是因为每次有新的连接建立时,唯一的令牌确保任何用户都无法访问其他人的订阅数据。

服务器

这是我用于运行循环和事件处理程序的内容。我正在创建循环,执行所有装饰器样式对象创建,并传入我的 EventHandler(我很快就会介绍)以及循环。

$loop = Factory::create();

new IoServer(
    new WsServer(
        new WampServer(
            new EventHandler($loop) // This is my class. Pass in the loop!
        )
    ),
    $webSock
);

$loop->run();

事件处理程序

class EventHandler implements WampServerInterface, MessageComponentInterface
{
    /**
     * @var \React\EventLoop\LoopInterface
     */
    private $loop;

    /**
     * @var array List of connected clients
     */
    private $clients;

    /**
     * Pass in the react event loop here
     */
    public function __construct(LoopInterface $loop)
    {
        $this->loop = $loop;
    }

    /**
     * A user connects, we store the connection by the unique resource id
     */
    public function onOpen(ConnectionInterface $conn)
    {
        $this->clients[$conn->resourceId]['conn'] = $conn;
    }

    /**
     * A user subscribes. The JSON is in $subscription->getId()
     */
    public function onSubscribe(ConnectionInterface $conn, $subscription)
    {
        // This is the JSON passed in from your JavaScript
        // Obviously you need to validate it's JSON and expected data etc...
        $data = json_decode(subscription->getId());
        
        // Validate the users id and token together against the db values
        
        // Now, let's subscribe this user only
        // 5 = the interval, in seconds
        $timer = $this->loop->addPeriodicTimer(5, function() use ($subscription) {
            $data = "whatever data you want to broadcast";
            return $subscription->broadcast(json_encode($data));
        });

        // Store the timer against that user's connection resource Id
        $this->clients[$conn->resourceId]['timer'] = $timer;
    }

    public function onClose(ConnectionInterface $conn)
    {
        // There might be a connection without a timer
        // So make sure there is one before trying to cancel it!
        if (isset($this->clients[$conn->resourceId]['timer']))
        {
            if ($this->clients[$conn->resourceId]['timer'] instanceof TimerInterface)
            {
                $this->loop->cancelTimer($this->clients[$conn->resourceId]['timer']);
            }
        }
    
        unset($this->clients[$conn->resourceId]);
    }

    /** Implement all the extra methods the interfaces say that you must use **/
}

基本上就是这样。这里的要点是:

  • 唯一令牌、用户 ID 和连接 ID 提供了确保一个用户无法看到另一用户的数据所需的唯一组合。
  • 唯一令牌意味着,如果同一用户打开另一个页面并请求订阅,他们将拥有自己的连接 ID + 令牌组合,因此同一用户不会在同一页面上拥有双倍的订阅(基本上,每个连接都有自己的连接)个人数据)。

扩大

在对数据进行任何操作之前,您应该确保所有数据都经过验证,而不是黑客尝试。使用类似的方法记录所有连接尝试Monolog https://github.com/Seldaek/monolog,并在发生任何严重情况时设置电子邮件转发(例如由于有人是混蛋并试图破解您的服务器而导致服务器停止工作)。

结束点

  • 验证一切。我怎么强调都不为过。您的独特令牌会根据每个请求而变化重要的.
  • 请记住,如果您在每个 HTTP 请求上重新生成令牌,并且在尝试通过 Websocket 连接之前发出 POST 请求,则必须在尝试连接之前将重新生成的令牌传回 JavaScript(否则您的令牌将无效)。
  • 记录一切。记录每个连接、询问主题和断开连接的人。 Monolog 对此非常有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Ratchet PHP WAMP - React / ZeroMQ - 特定用户广播 的相关文章

随机推荐