- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
Note: This is not the same as this question which utilises
MessageComponentInterface
. I am usingWampServerInterface
instead, so this question pertains to that part specifically. I need an answer with code examples and an explanation, as I can see this being helpful to others in the future.
我正在使用 Ratchet 和 ZeroMQ 的 WAMP 部分,我目前有 push integration tutorial 的工作版本.
我正在尝试执行以下操作:
我的第 (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();
我省略了无用的东西,专注于 onMessage()
和 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);
}
这是我的问题 - 这是一个单独的 php 文件,希望将来可以集成到其他代码中,但目前我不确定如何正确使用它。我是否从 session 中获取用户 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));
});
$(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 订阅用户时,从 session 中获取用户 ID 并从客户端发送是不安全的。这可能是伪造的。请告诉我有一个更简单的方法,如果是,如何?
最佳答案
Note: My answer here does not include references to ZeroMQ, as I am not using it any more. However, I'm sure you will be able to figure out how to use ZeroMQ with this answer if you need to.
首先,Websocket RFC和 WAMP Spec声明要订阅的主题必须是一个字符串。我在这里有点作弊,但我仍然遵守规范:我正在传递 JSON。
{
"topic": "subject here",
"userId": "1",
"token": "dsah9273bui3f92h3r83f82h3"
}
JSON 仍然是一个字符串,但它允许我传递更多数据来代替“主题”,并且 PHP 在另一端执行 json_decode()
很简单。当然,您应该验证您确实收到了 JSON,但这取决于您的实现。
主题是用户订阅的主题。您可以使用它来决定将哪些数据传回给用户。
显然是用户的ID。您必须使用下一部分验证此用户是否存在并且是否允许订阅:
这应该是一个一次使用随机生成的 token ,在您的 PHP 中生成,并传递给 JavaScript 变量。当我说“一次使用”时,我的意思是每次你重新加载页面(并且,通过扩展,在每个 HTTP 请求上),你的 JavaScript 变量应该在那里有一个新的标记。此 token 应根据用户 ID 存储在数据库中。
然后,一旦发出 websocket 请求,您将 token 和用户 ID 与数据库中的那些相匹配,以确保用户确实是他们所说的人,并且他们没有乱用 JS 变量。
Note: In your event handler, you can use
$conn->remoteAddress
to get the IP of the connection, so if someone is trying to connect maliciously, you can block them (log them or something).
之所以有效,是因为每次新连接通过时,唯一 token 可确保任何用户都无法访问其他任何人的订阅数据。
这是我用于运行循环和事件处理程序的内容。我正在创建循环,完成所有装饰器样式对象的创建,并传入我的 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 **/
}
基本上就是这样。这里的要点是:
在对数据进行任何操作之前,您应该确保所有数据都经过验证而不是黑客尝试。使用类似 Monolog 的方式记录所有连接尝试,并在发生任何严重问题时设置电子邮件转发(例如服务器停止工作,因为有人是 SCSS 并试图入侵您的服务器)。
关于php - Ratchet PHP WAMP - React/ZeroMQ - 特定用户广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19065799/
如何在不丢失配置的情况下升级 WAMP 服务器?我当前的版本是 WAMP 3.0.8,我想将其升级到 WAMP 3.1.0。 来自 WAMP 3.0.8 Apache 2.4.23 MySQL 5.7
我已经尝试通过路由器进行端口转发,并且还禁用了我的防火墙。 我还编辑了 httpd.conf。在那里我改变了 Listen 80 到 Listen 8080 我还从 localhost:80 更改了
无论如何,我可以在 Windows 7 计算机上的 USB 上使用 PHP、mySQL、apache、phpmyadmin 吗?询问的原因是我没有足够的权限在计算机上安装 XAMPP 之类的软件包,我
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
我在 Web 浏览器中使用 autobahn-js (0.11.2),在后端使用 crossbar 消息路由器 (v17.2.1)。 在网络断开的情况下(例如由于网络不佳),autobahn-js 客
我正在使用 Wamp 2.2,编辑 conf/extra/httpd-vhosts.conf 编辑此文件以添加 VirtualHosts ,但是当我在 httpd.conf 中取消注释行时 code>
作为我用来验证为站点完成的编程的一些自动部署 + 测试脚本的一部分,我有一些更新 Apache 配置文件的脚本。我想以编程方式重新启动 WAMP,以使更改生效。有没有好的方法可以做到这一点? 脚本是p
您好 Java 代理开发框架开发人员!我想将 WAMP 与 JADE 一起使用,我可以这样做吗,很简单,因为我非常反对 WAMP。还是 JADE 只推荐与 Java 相关的技术进行接口(interfa
我安装了wamp2.4。我卸载了然后安装wamp2.5。现在我在 "C:\wamp\bin\mysql" 中有 2 个文件夹 mysql5.6.12 mysql5.6.17 我将所有数据库文件夹从my
我有一个运行项目的本地 WAMP 服务器 (c:\wamp\www\myProject)。我想使用 PhpStorm 编辑和调试这个项目。我想我必须在 PhpStorm 中创建一个项目并将我的源代码导
我在Windows 8上安装了wamp,我关闭了Skype端口80,我使用了桌面版本的Skype。现在我看不到我的本地主机或 phpmyadmin。哎呀! Google Chrome 无法连接到本地主
我无法删除 C 盘中的 WAMP/bin/mysql 文件夹,每当我按下删除键时,都会出现一个对话框:该操作无法完成,因为该文件已在另一个程序中打开。关闭文件并重试。注意:我已经卸载了 wamp 服务
我想跟踪执行查询所需的时间我提到了 this post但我没有时间只得到查询。在使用我的 Web 应用程序一段时间后,使用 select、update 和 insert 查询(不是来自控制台,而是真正
很长一段时间以来,我一直在使用 php 5.3.5 安装 wamp现在我正在使用 Symfony2.1,需要国际扩展所以我下载并安装了 php 5.4.6,第一次一切正常 但现在我在一个新项目上,我需
我有一个快速的问题。我是第一次尝试使用 Laravel。为此,我正在使用 Wamp。我不知道这是否重要,但我在这个地址设置了 wamp 的 DocumentRoot : DocumentRoot "C
我刚刚下载并安装了 WAMP,当我打开它时,我收到一个警告框 Aestan Tray Menu has stopped working -"A problem has caused program t
我正在尝试从我的本地网站建立 wamp 连接。我在服务器端使用 Ratchet/Wamp,在客户端使用 autobahn|js。该服务运行良好,我能够连接,正在服务器上触发 onOpen/onClos
我已经在我的 Windows 7 机器上下载并安装了 WAMP 服务器版本 2.1d。当我从开始菜单中选择“启动 WAMP 服务器”时,托盘图标出现,但没有任何功能。当我悬停时,工具提示告诉我 2 个
我以前从未遇到过 Wamp 的问题,找不到问题所在。我的 www 文件夹中有几个项目(运行 Windows 7)。 我的主机文件有一行 127.0.0.1 localhost未注释 当我去http:/
我已经在我的 Windows 8 上安装了 WAMP,现在出于某些原因我需要知道它是 32 位还是 64。浏览并查看“关于”和版本/许可证文件,但找不到任何地方。顺便说一下,它是 WAMPServer
我是一名优秀的程序员,十分优秀!