简体   繁体   English

React / ZMQ / Ratchet-Websocket服务器响应

[英]React/ZMQ/Ratchet - Websocket server response

I've currently got a web socket server running and working with Ratchet PHP. 我目前有一个运行和与Ratchet PHP一起使用的Web套接字服务器。 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: 我可以使用ZMQ成功将数据推送到它:

push.php push.php

$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));

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?): 然后,在我的网络服务器脚本中,我可以将其发送到方法( onNewPush )上,以便在运行push.php文件(运行?)时进行处理:

...
$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: 我正在尝试类似:

pull.php pull.php

$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'));

My $pusher variable loads my file which implements a few Ratchet Interfaces. 我的$pusher变量加载了实现一些棘轮接口的文件。 But essentially I'm just returning a string: 但本质上我只是返回一个字符串:

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

When running pull.php I get the error: 运行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

Does anyone know why? 有人知道为什么吗?

Also what importance does the second parameter on getSocket() have? 还有getSocket()上的第二个参数有什么重要性? Just seems like a string which is never used again. 似乎就像一个不再使用的字符串。

Cheers 干杯

UPDATED 更新

In your pull.php file, you've got a REQ socket connecting to a PULL socket. 在您的pull.php文件中,您有一个连接到PULL套接字的REQ套接字。 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. 特别是,您似乎需要一个REQ-REP对,以便您的客户端可以请求数据,并且服务器用响应进行回复。 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. 如果服务器将数据排队等待下一个客户端使用,则您将使用PUSH-PULL ,然后客户端将从队列中提取下一个内容。

In either event, you cannot connect a REQ socket to a PULL socket or a PUSH socket. 无论哪种情况,都不能将REQ插座连接到PULL插座或PUSH插座。

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. 我没有从列出的代码或命名方案中完全了解您的用例或通信体系结构,所以我不知道我能提供的详细信息还多,请随时澄清发生了什么,我可能会建议更确切地说,您应该使用哪种套接字策略。

You have this line in your pull.php: 您在pull.php中有以下一行:

echo $socket->recv();

Push socket is for sending messages, not receiving them. 推套接字用于发送消息,而不接收消息。 That's probably where the exception comes from. 那可能就是异常的来源。

Also: The first parameter of method on() on pull sockets should be 'message' . 另外:拉式插座on()方法的第一个参数应该是'message'

$pull->on('message', array($pusher, 'onPull'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM