dqz30992 2014-05-26 17:56
浏览 85
已采纳

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

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:

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?):

...
$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

$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. But essentially I'm just returning a string:

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

When running pull.php I get the error:

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? Just seems like a string which is never used again.

Cheers

UPDATED

  • 写回答

2条回答 默认 最新

  • douyun8901 2014-05-27 19:20
    关注

    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?