Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

msgpack/igbinary 序列化一个 class 对象发布消息时,订阅回调收到的数据只有部分! #39

Closed
designinlife opened this issue Sep 9, 2018 · 6 comments · Fixed by #61

Comments

@designinlife
Copy link

designinlife commented Sep 9, 2018

msgpack/igbinary 序列化一个 class 对象发布消息时,订阅回调收到的数据只有一部分!(字节不完整)
必须 base64_encode(msgpack_serialize($this)) 才行。

测试环境:
PHP: 7.2.9
MessagePack: 2.0.2

@yunnian
Copy link
Owner

yunnian commented Sep 9, 2018

php-nsq 是什么版本的,老版本有类似bug 后来已经解决

@designinlife
Copy link
Author

@yunnian 版本是 3.2.0 的

@yunnian
Copy link
Owner

yunnian commented Sep 10, 2018

好的,能够提供一些示例吗,我去复现修复下

@designinlife
Copy link
Author

<?php
class SampleMessage {
    /**
     * 创建消息的时间。
     *
     * @var int
     */
    protected $created_time = 0;

    /**
     * 消息发布时间。
     *
     * @var int
     */
    protected $triggered_timestamp = 0;

    /**
     * 消息对象。
     *
     * @var string
     */
    protected $message = NULL;

    /**
     * 自定义参数。
     *
     * @var array
     */
    protected $arguments = [];

    /**
     * AbstractNotifyMessage constructor.
     *
     * @param int            $triggered_timestamp 消息发布(触发)时间。
     * @param string         $message             消息对象。
     * @param array          $arguments           自定义参数。
     */
    function __construct(int $triggered_timestamp, string $message, array $arguments = []) {
        $this->triggered_timestamp = $triggered_timestamp;
        $this->message             = $message;
        $this->arguments           = $arguments;
        $this->created_time        = time();
    }

    /**
     * 对象转换为 MessagePack 编码字符串。
     *
     * @return string
     */
    function __toString(): string {
        return \msgpack_serialize($this);
    }
}

$nsq->publish('topic', strval(new SampleMessage(time() + 15, '测试!', [1,2,3])));

试下上面的代码看看

@designinlife
Copy link
Author

这样发过去后 $nsq->subscribe() 回调里 $msg->payload 收到的不完整,字节我看了下都少了。
估计是 msgpack 序列化之后部分乱码不能识别,你扩展里直接截断了吧

@lexand
Copy link
Contributor

lexand commented Oct 23, 2019

Salutations
Same problem

Test data

<?php
$arr = [
    'a' => 0,
    'f' => 0,
    'c' => 0,
    'p' => 0,
    'b' => 0,
    'v' => 0,
    'q' => 0,
    'o' => 0,
    'z' => 0,
    'r' => 0,
    'l' => 0,
    'w' => 2,
    'e' => 0,
    't' => 2,
    'y' => 0,
    'u' => 0,
];
$msg = msgpack_pack($arr);
echo \base64_encode($msg), "\n"; // 3gAQoWEAoWYAoWMAoXAAoWIAoXYAoXEAoW8AoXoAoXIAoWwAoXcCoWUAoXQCoXkAoXUA
// strlen($msg)== 51 
// ....
$nsq->publish('test', $msg);

after publishing on the consumer I got array from one byte only [222]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants