- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我只是这个涉及 websocket、Ratchet 和 ZeroMQ 的整个领域的初学者。
我的基本理解:
websocket
有助于在服务器和客户端之间创建开放连接。
Ratchet
是一个基于 PHP 的库,它使用 PHP 的核心 Socket 函数来创建一个 PHP 套接字框架,使我们能够轻松地进行 PHP 套接字编程。
ZeroMQ
是一个套接字库,可帮助非 Ratchet 应用程序(其他 PHP 脚本)通过 Ratchet 套接字和网络套接字发送数据。
我正在按照 ratchet 中关于“hello world”和“pusher”的教程进行操作,但它们似乎都不完整,并且只教授如何仅使用控制台。我还在 github 中找到了 Ratchet 示例,但没有正确记录。我正在寻找一个完整的示例(带有专用的 html 页面和 javascript)
下面是我正在处理的代码:这是我正在为其发出 Ajax 请求的 Controller 方法之一。此方法将创建一个新帖子(假设)。我想在 ZeroMq 的帮助下通过广播/推送在多个客户端的浏览器中动态更新帖子列表。
Controller 中的一个方法:
public function create_new_post(){
// ------
// code to create a new post.
// -------
// After creating a post
$response = [
'new_post_title' => $title,
'post_id' => $id
];
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:8000");
$socket->send(json_encode($response));
}
推送文件:
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
class Pusher implements WampServerInterface{
public function onPostEntry($data){
// Data that were sent by ZeroMQ through create_new_post() method
$entry_data = json_decode($data);
// AND AFTER THIS, I DONT HAVE CLUE OF WHAT TO DO NEXT !!
}
}
运行服务器的 Shell 脚本:
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:8000');
$pull->on('message', array($pusher, 'onBidEntry'));
// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server($loop);
$webSock->listen(8080, '0.0.0.0');
$webServer = new Ratchet\Server\IoServer(
new Ratchet\Http\HttpServer(
new Ratchet\WebSocket\WsServer(
new Ratchet\Wamp\WampServer(
$pusher
)
)
),
$webSock
);
$loop->run();
Shell 脚本仅说明它将在端口 8080 中提供服务,但我将如何提及我的路由。可以说我只希望在“mysite/allposts”页面中打开连接。另外,我必须在客户端(一个 javascript 文件)编写的脚本是什么,以及如何通过客户端更新特定的 DOM 对象来接收这些新数据。
最佳答案
我遵循了您所谈论的示例。他们对我来说似乎并不完整,但我明白你的意思。 Ratchet 是一个服务器端脚本,只允许您编写一个实现 websockets 并且能够监听 ZMQ 消息的服务。您将在命令行上启动 Ratchet 脚本,它作为与 Apache 并行的服务运行。
这完全独立于 websocket 的客户端。就像他们推荐的那样,我在客户端使用了 Autobahn.js。这个库实现了 WAMP 协议(protocol)。它最大限度地简化了客户端代码。
您的代码的问题是,class Pusher implements WampServerInterface
没有public function onPostEntry
。此类必须实现 WampServerInterface
,这意味着它必须至少具有以下功能:
还有其他一些更高级的功能,比如在客户端上调用
远程过程。
在发送方(ZMQ 消息),输入以下代码:
$zmq = new ZMQWrapper;
$zqm->publish('posts', $response);
class ZMQWrapper {
function __construct(){
$this->context = new ZMQContext();
$this->socket = $this->context->getSocket(ZMQ::SOCKET_PUSH);
$this->socket->setSockOpt(ZMQ::SOCKOPT_LINGER, 500);
$this->socket->connect("tcp://127.0.0.1:" . ZMQ_PORT);
}
function publish($topic, $msg){
$data = ['topic' => "mb.$topic", 'msg' => $msg];
$this->socket->send(json_encode($data), ZMQ::MODE_DONTWAIT);
}
}
在 pusher 文件中放一些像这样的东西:
public function onSubscribe(ConnectionInterface $conn, $topic) {
$log = $this->getLogger();
$topicId = $topic->getId();
$log->info(sprintf('A client subscribed to %s', $topicId));
// you could broadcast that user x joined the discussion
}
public function onUnSubscribe(ConnectionInterface $conn, $topic) {
$log = $this->getLogger();
$topicId = $topic->getId();
$log->info(sprintf('A client unsubscribed from %s', $topicId));
// you could broadcast that user x leaved the discussion
}
public function onOpen(ConnectionInterface $conn) {
$log = $this->getLogger();
$log->info(sprintf('Client %d connected', $conn->resourceId));
$this->clients[$conn->resourceId] = array(); // this will allow you to save state information of the client, you can modify in onSubscribe and onUnsubscribe
// clients will contain the list of all clients
}
public function onClose(ConnectionInterface $conn) {
$log = $this->getLogger();
$log->info(sprintf('Client %d disconnected', $conn->resourceId));
// you could broadcast that user x leaved the discussion
}
public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) {
$log = $this->getLogger();
$topicId = $topic->getId();
$log->info(sprintf('Client %d published to %s : %s', $conn->resourceId, $topicId, json_encode($event)));
foreach($topic->getIterator() as $peer){
if(!in_array($peer->WAMP->sessionId, $exclude)){
$peer->event($topicId, $event);
}
}
}
最后一 block 在客户端。如果用户打开页面 mysite/allposts
,您将在 javascript 中包含 autobahn.js
。 websocket 将在变量 ab
下可用。然后你做:
打开页面时:
var currentSession;
ab.connect(
Paths.ws,
function(session) { // onconnect
currentSession = session
onWsConnect(session)
},
function(code, reason, detail) {// onhangup
onWsDisconnect(code, reason, detail)
},{
maxRetries: 60,
retryDelay: 2000,
skipSubprotocolCheck: true
}
)
currentSession.subscribe('posts', onPostReceived)
function onPostReceived(topic, message){
//display the new post
}
关闭页面时:
currentSession.unsubscribe(topic)
您注意到我将所有内容都保持得非常笼统。这使我可以让同一个系统处理多种类型的消息。不同之处在于 ZMQ 消息和 currentSession.subscribe
的参数。
在我的实现中,我还跟踪打开连接的登录用户,但我删除了这部分代码。
希望对您有所帮助。
关于javascript - 如何将 ZeroMQ 套接字与 Ratchet web-socket 库绑定(bind),以便为 php 应用程序实时应用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31366855/
你能举一些zeromq的例子吗? 最佳答案 假设您想要某种公告板。您希望通过订阅公告板来只允许某些人看到它。 这可以使用 ZeroMQ 的发布者/订阅者模型来完成。 现在,假设您需要发送一些异步消息。
因此,正如我在上一篇文章中所问的那样,我希望能够使用不同语言编写的程序或函数在它们之间进行通信。 我最近遇到了 zeromq,我试图弄清楚这是否可以帮助我,因为它提供了某种套接字。例如,zeromq
与通过 POLLIN 多路复用多个套接字有何不同? while True: socks = dict(poller.poll()) if socks.get(control_recei
我正在设计一个与 ZeroMQ 对话的服务器应用程序。无需深入细节,服务器将存储和服务(来自查询请求)(eventid, eventstring)元组。 我的问题涉及有线协议(protocol)的设计
我有一个服务器(在 Amazon 上运行)和一个连接到它的客户端。建立连接后,客户端和服务器专门相互通信并发送消息。 例如 1. Client -> Server 2. Client -> Serve
我正在开发一个新的客户端-服务器应用程序 (.Net),并且到目前为止一直在使用 WCF,它非常适合应用程序的请求-响应方法。然而,我被要求用基于套接字的解决方案替换它,部分是为了支持非 .Net 客
我正在尝试做一个发布/订阅架构,其中多个发布者和多个订阅者存在于同一总线上。根据我在互联网上阅读的内容,只有一个套接字应该调用 bind(),而所有其他套接字(无论是 pub 还是 sub)都应该调用
使用zeromq,发送者发送10条消息后,发送者崩溃。 场景1:接收方正在一条一条地处理消息,花费了一些明显的时间成本,在这种情况下它还会收到 10 条消息吗? 场景 2:另一种情况是,当接收器崩溃时
我有一个 ZeroMQ 套接字,它正在从不同机器上的多个进程接收数据。在不改变数据内容的情况下,有没有办法识别数据的来源呢?具体来说,我想要发送者的 IP 地址(如果它来自 TCP 连接)。 最佳答案
有人知道在哪里可以找到有关 ZeroMQ 延迟与 29 West LBM 等竞争对手的性能详细信息吗? 看起来便宜得多,但我找不到任何指标来决定哪个更合适。 最佳答案 ZeroMQ 和 29West
有没有办法在不使用转发器概念的情况下使用 zeroMQ 库进行消息广播? 最佳答案 是的,一个 PUB 套接字将广播到所有连接的 SUB 套接字。只有当您想要桥接不同的网络时才需要转发器(代理),例如
几天前我才开始使用zeromq。我的目标是设计一个具有多个代理(代理网络)的发布订阅系统。我已经阅读了 zeromq 指南的相关部分,并为简单的发布子系统编写了代码。如果有人可以帮助我解决以下问题:
我需要编写一个订单管理器,将客户(股票、外汇等)订单发送到适当的交易所。客户想要发送订单,但对 FIX 或其他专有协议(protocol)一无所知,只知道发送订单的内部(规范化)格式。我有应用程序(服
我正在尝试从示例 wuclient/wuserver 在 zeromq 上实现一个惰性订阅者。 客户端比服务器慢得多,因此它必须只获取服务器最后发送的消息。 到目前为止,我发现这样做的唯一方法是连接/
我是 ZeroMQ 的新手并试图找出设计问题。我的情况是我有一个或多个客户端向单个服务器发送请求。服务器将处理请求,做一些事情,并向客户端发送回复。有两个条件: 回复必须发送到发送请求的客户端。 如果
如 docs 中所述在 3.x 版本的 zeromq 中,PUB/SUB 场景中的消息正在被过滤 出版商侧(而不是在订阅者方面,这是微不足道的)。 对我来说,这听起来像是发布者必须持有所有连接的套接字
引自 ZeroMQ 指南 However, with a little extra work, this humble pattern becomes a good basis for real wo
假设我有一个带有 ZeroMQ 接口(interface)的节点(进程、线程等),比方说一个 REP 套接字。这意味着我有一个无限主循环,它在 zmq_recv 或 zmq_poll 函数中休眠。 现
我想以某种方式比较 grpc 与 Zeromq 及其模式的功能:并且我想创建一些比较(功能集) - 不知何故 - 0mq 是“更好”的套接字 - 但无论如何 - 如果我应用 0mq模式 - 我认为我得
我正在试验 ZeroMQ。我发现在 ZeroMQ 中非常有趣,connect 或 bind 先发生并不重要。我试着查看 ZeroMQ 的源代码,但它太大了,找不到任何东西。 代码如下。 # clien
我是一名优秀的程序员,十分优秀!