English 中文(简体)
零MQ与PHP 用户的失散信息
原标题:ZeroMQ Lost Messages with PHP Subscriber

i m 采用0mq和php,对连接自由星座单元(VOIP软件开关)具有约束力。

Short: i m loosing Events. Long: The zmq module in Freeswitch is implemented in c++ as publisher. My PHP Code is as follows:

<?php
  $context = new ZMQContext();

  echo "connect to freeswitch zmq module...";
  $sub = new ZMQSocket($context, ZMQ::SOCKET_SUB);
  $sub->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE,"");
  $sub->connect("tcp://192.168.20.73:5556");
  $hwm = $sub->getSockOpt(ZMQ::SOCKOPT_HWM);
  echo "ok.hwm: $hwm
";

  echo "looping
";
  while(1) {
  $data = $sub->recv();
  $d = json_decode($sub->recv(),TRUE);
  $event = $d["Event-Name"];
  $date = $d["Event-Date-Local"];
  $ts = $d["Event-Date-Timestamp"];
  $msgnr = $d["ZMQ-Msg-Cnt"];
  echo "PHP: $date msg# $msgnr $ts received $event
";
}
?>

ZMQ-Msg-Cnt is a sequence number i ve build into the zmq module in freeswitch. I can see that every 2nd message is lost. tcpdump shows that the message is received by zmq.

I ve converted the PHP Code into C and now i m able to receive every message. C:

#include "zhelpers.h"
#include "cJSON.h"

int main (void)
{
    void *context = zmq_init (1);

    void *subscriber = zmq_socket (context, ZMQ_SUB);
    zmq_connect (subscriber, "tcp://192.168.20.73:5556");
    zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "", 0);

    while (1) {
        char *string = s_recv (subscriber);

        cJSON *root = cJSON_Parse(string);
        int msgcnt = cJSON_GetObjectItem(root,"ZMQ-Msg-Cnt")->valueint;

        printf("C: %s msg# %s %s received %s
",
            cJSON_GetObjectItem(root,"Event-Date-Local")->valuestring,
            cJSON_GetObjectItem(root,"ZMQ-Msg-Cnt")->valuestring,
            cJSON_GetObjectItem(root,"Event-Date-Timestamp")->valuestring,
            cJSON_GetObjectItem(root,"Event-Name")->valuestring
        );
        cJSON_Delete(root);
        free (string);
    }

zmq_close (subscriber);
zmq_term (context);
return 0;
}

Is there anything wrong with the PHP Code ? Are there any tricks/must-do/hints for PHP ?

Thanks in advance, gerald weber

最佳回答

You are calling the recv function twice, thus it is loading one message, skipping any processing on it, then loading the second one:

$data = $sub->recv();
// This is your first message, called in a blocking mode

$d = json_decode($sub->recv(),TRUE);
// and here s your second one, called in a non-blocking mode

将这两条改为一条:

$d = json_decode($sub->recv());
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签