React/ZMQ/Ratchet - Websocket 服务器响应 [英] React/ZMQ/Ratchet - Websocket server response

查看:35
本文介绍了React/ZMQ/Ratchet - Websocket 服务器响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个正在运行并使用 Ratchet PHP 的网络套接字服务器.我不希望外部脚本与我的服务器进行通信.我可以使用 ZMQ 成功地将数据推送给它:

I've currently got a web socket server running and working with Ratchet PHP. I'm not at the stage where I want external scripts to communicate with my server. I can successfully push data to it using ZMQ:

$json = ['name' => 'Joe Bloggs'];

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

$socket->send(json_encode($json));

然后在我的网络服务器脚本中,我可以将其发送到一个方法 (onNewPush) 以在 push.php 文件运行(运行?)时对其进行处理:

Then in my webserver script I can send this to a method (onNewPush) to do something with it when the push.php file is run (ran?):

...
$push = $context->getSocket(ZMQ::SOCKET_PULL);
$push->bind('tcp://127.0.0.1:5555');
$push->on('Push Notification', array($pusher, 'onNewPush'));
...

所以这很好用,但我在尝试收到回复时遇到了麻烦.我正在尝试类似的东西:

So this works fine, but I'm having trouble trying to receive a response back. I'm trying something like:

$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_REQ, 'Pull Notification');
$socket->connect("tcp://localhost:5554");

$socket->send('data');

echo $socket->recv();

然后在我的服务器脚本中:

Then in my server script:

$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:5554');
$pull->on('message', array($pusher, 'onPull'));

我的 $pusher 变量加载我的文件,它实现了一些棘轮接口.但本质上我只是返回一个字符串:

My $pusher variable loads my file which implements a few Ratchet Interfaces. But essentially I'm just returning a string:

public function onPull()
{
    return "some data";
}

运行 pull.php 时出现错误:

Fatal error: Uncaught exception 'ZMQSocketException' with message 'Failed to receive message: Not supported' in websockets\pull.php:9 Stack trace: #0 websockets\pull.php(9): ZMQSocket->recv() #1 {main} thrown in websockets\pull.php on line 9

有人知道为什么吗?

还有 getSocket() 上的第二个参数有什么重要性?就像一个永远不会再使用的字符串.

Also what importance does the second parameter on getSocket() have? Just seems like a string which is never used again.

干杯

更新

推荐答案

在你的 pull.php 文件中,你有一个 REQ 套接字连接到一个 PULL 套接字.查看文档以查看兼容的套接字对.特别是,您似乎需要一个 REQ-REP 对,以便您的客户端可以请求数据,而您的服务器以响应进行回复.如果您的服务器为下一个客户端准备好数据排队,然后您的客户端从队列中提取下一个数据,您将使用 PUSH-PULL.

In your pull.php file, you've got a REQ socket connecting to a PULL socket. Check out the docs to see compatible socket pairs. In particular, it appears that you want a REQ-REP pair so that your client can request data and your server replies with a response. You'd use PUSH-PULL if your server queues up data ready for the next client, and then your client pulls whatever is next from the queue.

在任何一种情况下,您都不能将 REQ 套接字连接到 PULL 套接字或 PUSH 套接字.

In either event, you cannot connect a REQ socket to a PULL socket or a PUSH socket.

我没有从列出的代码或命名方案中完全理解您的用例或通信架构,所以我不知道我能提供多少细节,请随时澄清发生了什么,我可能会能够更明确地建议您应该使用哪种套接字策略.

I don't fully understand your use case or communication architecture from the listed code or naming scheme, so I don't know how much more detail I can give than that, feel free to clarify what's going on and I might be able to advise more definitively what socket strategy you should be using.

这篇关于React/ZMQ/Ratchet - Websocket 服务器响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆