PHP WebSocket ZMQ - 聊天操作 - 向特定用户发送数据

2023-12-24

我正在开发一个基于 Symfony 2.2.11 的 PHP 项目,我安装了与以下教程相关的 socketohttp://socketo.me/docs/install http://socketo.me/docs/install使我的聊天脚本正常工作。

服务器命令.php// 启动WebSocket服务器的命令行代码

$oLoop = Factory::create();

    // Listen for the web server to make a ZeroMQ push after an ajax request
    $oContext = new Context($oLoop);
    $oPull = $oContext->getSocket(\ZMQ::SOCKET_PULL);
    // LET IT 127.0.0.1
    $oPull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
    $oPull->on('message', array($oChat, 'onMessage'));

    // Set up our WebSocket server for clients wanting real-time updates
    $oWebSock = new Server($oLoop);
    $oWebSock->listen(7979, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
    $webServer = new IoServer(
        new HttpServer(
            new WsServer(
                new WampServer(
                    $oChat
                )
            )
        ),
        $oWebSock
    );

    $oLoop->run();

将消息添加到数据库后:消息控制器.php

....
// This is our new stuff
        $oContext = new \ZMQContext();
        $oSocket = $oContext->getSocket(\ZMQ::SOCKET_PUSH, 'PushMe');
        $oSocket->connect("tcp://mydomain:5555");

        $aData = array(
            'topic'         => 'message',
            'sUsername'     => $oUserCurrent->getUsername(),
            'sMessage'      => $sMessage
        );

        $oSocket->send(json_encode($aData));
.....

聊天服务:Chat.php

/**
 * A lookup of all the topics clients have subscribed to
 */
public function onSubscribe(ConnectionInterface $conn, $topic) 
{
    // When a visitor subscribes to a topic link the Topic object in a  lookup array
    $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);

}

/**
 * @param string JSON'ified string we'll receive from ZeroMQ
 */
public function onMessage($jData) 
{
    $aData = json_decode($jData, true);

    var_dump($aData);

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

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

    // This sends out everything to multiple users, not what I want!!

    // re-send the data to all the clients subscribed to that category
    $topic->broadcast($aData);
}

接收数据的JS代码:messages.html.twig :

var conn = new ab.Session(
                    'ws://mydomain:7979' // The host (our Ratchet WebSocket server) to connect to
                  , function() {            // Once the connection has been established
                        conn.subscribe('message', function(topic, data) 
                        {
                            console.log(topic);
                            console.log(data);
                        });
                    }
                  , function() {            // When the connection is closed
                        console.warn('WebSocket connection closed');
                    }
                  , {                       // Additional parameters, we're ignoring the WAMP sub-protocol for older browsers
                        'skipSubprotocolCheck': true
                    }
                );

所以一切工作都很完美,当我发送一条新消息时,它会发送到数据库,然后到达聊天页面。

问题 :JS脚本在哪里,数据就落在哪里,结果是所有用户都可以获得相同的记录消息

ASKING :我怎样才能让数据落地特定用户 page ?

谢谢


您正在使用Ratchet在后端,对吗?

因此,这里有您需要的很好的案例示例:

http://socketo.me/docs/hello-world http://socketo.me/docs/hello-world

你应该把你的客户连接保留在里面$clients属性(不是资源 ID 的集合!)。因此,您可以从此集合中选择一个元素并仅向该客户端发送消息。

Example:

public function onSubscribe(ConnectionInterface $conn, $topic) 
{
    // When a visitor subscribes to a topic link the Topic object in a  lookup array
    $subject = $topic->getId();
    $ip = $conn->remoteAddress;

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

    $this->clients[] = $conn; // you add connection to the collection

    $conn->send("Hello new user!"); // you send a message only to this one user
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PHP WebSocket ZMQ - 聊天操作 - 向特定用户发送数据 的相关文章

  • 垃圾邮件打败了我所有的验证码

    我有一个 WordPress 博客 我在上面使用验证码插件的时间最长 因为它有效 最近我收到了大量的垃圾邮件 然后我尝试执行通过 PHP 生成的 将这些随机数添加在一起 但仍然没有成功 我不太确定需要做什么来阻止这些垃圾邮件 但这很烦人 我
  • 使用 phpdocx 下载损坏的 .docx

    我有一个项目 我们使用 phpdocx pro 在模板中生成 docx 文件 我可以很容易地将数据输入到模板中 但是当下载文件并在 MS Word 2010 中打开时 程序报告无法打开文件 因为内容存在问题 详细信息是 文件已损坏 并且无法
  • file_get_contents 大文件上传

    我正在尝试使用 fsockopen 上传 2GB 以上的大文件 但 file get content 出现以下错误 我无法在内存中存储大文件 我需要分块发送数据 但不知道如何执行此操作 请问有人可以指导我吗 致命错误 允许的内存大小 134
  • 将查询字符串附加到任何形式的 URL

    我要求用户在文本框中输入 URL 并需要向其附加查询字符串 URL 的可能值如下 http www example com http www example com http www example com a http www examp
  • PHP DOM - 剥离 span 标签,保留其内容

    我希望采用如下标记 span class test Some text that is strong bolded strong and contains a a href link a span 并在 PHP 中找到剥离跨度的最佳方法 剩
  • 从 php 到 JavaScript 的数组

    我正在尝试使用 json 将数组列表从 php 传输到 javascript 但它不起作用 JS ajax url getProfilePhotos php type post post or get method data if you
  • Laravel/00webhost 错误 404。在此服务器上找不到请求的 URL

    1 将我的文件上传到 000webhost 我将公用文件夹中的所有文件放置到公共 html然后我创建了一个名为laravel我在那里上传了所有其他文件 这是我的目录结构 laravel app 引导程序 config 公共 html 索引
  • 从 php 执行 bash 脚本并立即输出回网页

    我有一组 bash 和 Perl 脚本 开发在 Linux Box 上部署所需的目录结构 可选 从svn导出代码 从这个源构建一个包 这在终端上运行良好 现在 我的客户请求此流程的 Web 界面 例如 某些页面上的 创建新包 按钮将一一调用
  • PHP-docker容器中的环境变量

    我想在我的 docker 容器中显示一个环境变量 PHP 脚本如下所示 我使用 OpenShift 来启动容器 PHP 容器显示 env is 现在我更改容器的 dc 配置 oc env dc envar USER Pieter deplo
  • 如何在CentOS 5.3上安装php-mongodb?

    我已经在我的 VPS 上安装了 mongoDB 效果很好 现在我想安装 php 驱动程序以使 php 与 mongoDB 一起工作 我跟着蒙戈安装 http www php net manual en mongo installation
  • 如何在php中使用一张图像绘制形状

    我需要使用图像的一部分来创建帧图像 例如 用户将从后端上传图像片段 现在我需要根据前端用户的要求在前端创建一个框架 用户将选择框架的高度和宽度 然后他将选择该图像片段 如下所示 我没有办法做到这一点 我尝试通过 css 和 html can
  • Laravel 登录后重定向回来

    登录后如何重定向返回页面 在 Laravel 5 2 中 认证控制器 protected redirectTo 重定向用户
  • 使用 :hover 作为元素的内联样式(使用 HTML/CSS/php)[重复]

    这个问题在这里已经有答案了 可能的重复 如何将 a hover 规则嵌入到文档中间的样式属性中 https stackoverflow com questions 131653 how do i embed an ahover rule i
  • php oracle客户端oci8安装出现什么问题

    我尝试了安装 PHP Oracle 客户端的所有过程 1 我安装了客户端版本8和32位 2 我在php ini中取消了oci的注释 3 重新启动Wamp 4 不确定是否真的安装 但我在 php ini 中得到了引用 5 但仍然无法连接 泰汉
  • PHP Intl 扩展线程安全吗?

    我一直在阅读有关 PHP 中的语言环境的内容 看起来setlocale 线程有问题 我对线程不太熟悉 文档提到它不是线程安全的 我想让我的项目能够处理某些数字格式 并且 Intl 扩展似乎很有趣 http php net manual en
  • 使(文本到图像)图像具有一定的宽度但无限的长度?

    我有下面的代码 可以用大量文本生成图像 我希望该图像的宽度为 700 像素 我还希望它保留字符串所具有的段落结构 该字符串来自 MySQL 数据库 我怎样才能实现这一点 font 2 width imagefontwidth font st
  • 在 PHP 中接受带有小数点和千位分隔符的国际数字

    对于用户可以输入能量值来计算相应费用的在线计算器 我需要 PHP 脚本来接受各种用户输入 200 万又四分之一焦耳 的值可以输入为 2000000 25 默认表示法 2 000 000 25 带千位分隔符 2000000 25 逗号作为小数
  • 点击 %40 变为 %2540

    当单击包含 符号的链接时 该网址给我 40 这就是我想要的 但是一旦我点击它 一秒钟后它就在我点击后变成了 2540 单击是在电子邮件内 然后定向到网站 其中 40 更改为 2540 我怎样才能让它停止变化 它现在得到这样的参数 email
  • if/else 简写来定义变量

    我很难理解 if else 的 php 简写是如何描述的here https stackoverflow com questions 20233207 php if shorthand and echo in one line possib
  • 我可以让 swagger-php 在查询字符串上使用数组吗?

    我使用 Swagger php 当我定义查询字符串上的参数时 它可以是一个数组 但据我所知 它不支持这种查询字符串 https api domain tld v1 objects q 1 q 5 q 12 我相信这会被设定in the co

随机推荐