首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
连接MQTT订阅后,连接断开
### 问题描述 我连接mqtt后,订阅会断开连接 Client: Close. Client:Connect. array(4) { ["type"]=> int(1) ["dup"]=> int(0) ["qos"]=> int(0) ["retain"]=> int(0) } received length=63 array(4) { ["type"]=> int(12) ["dup"]=> int(0) ["qos"]=> int(0) ["retain"]=> int(0) } received length=2 Client: Close. Client:Connect. ### 相关代码 ```php function decodeValue($data) { return 256 * ord($data[0]) + ord($data[1]); } function decodeString($data) { $length = decodeValue($data); return substr($data, 2, $length); } function mqtt_get_header($data) { $byte = ord($data[0]); $header['type'] = ($byte & 0xF0) >> 4; $header['dup'] = ($byte & 0x08) >> 3; $header['qos'] = ($byte & 0x06) >> 1; $header['retain'] = $byte & 0x01; return $header; } function event_connect($header, $data) { $connect_info['protocol_name'] = decodeString($data); $offset = strlen($connect_info['protocol_name']) + 2; $connect_info['version'] = ord(substr($data, $offset, 1)); $offset += 1; $byte = ord($data[$offset]); $connect_info['willRetain'] = ($byte & 0x20 == 0x20); $connect_info['willQos'] = ($byte & 0x18 >> 3); $connect_info['willFlag'] = ($byte & 0x04 == 0x04); $connect_info['cleanStart'] = ($byte & 0x02 == 0x02); $offset += 1; $connect_info['keepalive'] = decodeValue(substr($data, $offset, 2)); $offset += 2; $connect_info['clientId'] = decodeString(substr($data, $offset)); return $connect_info; } $serv = new swoole_server("0.0.0.0", 9906, SWOOLE_BASE); $serv->set( array( 'open_mqtt_protocol' => true, 'worker_num' => 1, ) ); $serv->on('connect', function ($serv, $fd) { echo "Client:Connect.\n"; }); $serv->on('receive', function ($serv, $fd, $from_id, $data) { $header = mqtt_get_header($data); var_dump($header); if ($header['type'] == 1) { $resp = chr(32) . chr(2) . chr(0) . chr(0);//转换为二进制返回应该使用chr event_connect($header, substr($data, 2)); $serv->send($fd, $resp); } elseif ($header['type'] == 3) { $offset = 2; $topic = decodeString(substr($data, $offset)); $offset += strlen($topic) + 2; $msg = substr($data, $offset); echo "client msg: $topic\n---------------------------------\n$msg\n"; //file_put_contents(__DIR__.'/data.log', $data); } echo "received length=" . strlen($data) . "\n"; }); $serv->on('close', function ($serv, $fd) { echo "Client: Close.\n"; }); $serv->start(); ```
发布于6年前 · 4 次浏览 · 来自
提问
栀沫@
### 问题描述 我连接mqtt后,订阅会断开连接 Client: Close. Client:Connect. array(4) { ["type"]=> int(1) ["dup"]=> int(0) ["qos"]=> int(0) ["retain"]=> int(0) } received length=63 array(4) { ["type"]=> int(12) ["dup"]=> int(0) ["qos"]=> int(0) ["retain"]=> int(0) } received length=2 Client: Close. Client:Connect. ### 相关代码 ```php function decodeValue($data) { return 256 * ord($data[0]) + ord($data[1]); } function decodeString($data) { $length = decodeValue($data); return substr($data, 2, $length); } function mqtt_get_header($data) { $byte = ord($data[0]); $header['type'] = ($byte & 0xF0) >> 4; $header['dup'] = ($byte & 0x08) >> 3; $header['qos'] = ($byte & 0x06) >> 1; $header['retain'] = $byte & 0x01; return $header; } function event_connect($header, $data) { $connect_info['protocol_name'] = decodeString($data); $offset = strlen($connect_info['protocol_name']) + 2; $connect_info['version'] = ord(substr($data, $offset, 1)); $offset += 1; $byte = ord($data[$offset]); $connect_info['willRetain'] = ($byte & 0x20 == 0x20); $connect_info['willQos'] = ($byte & 0x18 >> 3); $connect_info['willFlag'] = ($byte & 0x04 == 0x04); $connect_info['cleanStart'] = ($byte & 0x02 == 0x02); $offset += 1; $connect_info['keepalive'] = decodeValue(substr($data, $offset, 2)); $offset += 2; $connect_info['clientId'] = decodeString(substr($data, $offset)); return $connect_info; } $serv = new swoole_server("0.0.0.0", 9906, SWOOLE_BASE); $serv->set( array( 'open_mqtt_protocol' => true, 'worker_num' => 1, ) ); $serv->on('connect', function ($serv, $fd) { echo "Client:Connect.\n"; }); $serv->on('receive', function ($serv, $fd, $from_id, $data) { $header = mqtt_get_header($data); var_dump($header); if ($header['type'] == 1) { $resp = chr(32) . chr(2) . chr(0) . chr(0);//转换为二进制返回应该使用chr event_connect($header, substr($data, 2)); $serv->send($fd, $resp); } elseif ($header['type'] == 3) { $offset = 2; $topic = decodeString(substr($data, $offset)); $offset += strlen($topic) + 2; $msg = substr($data, $offset); echo "client msg: $topic\n---------------------------------\n$msg\n"; //file_put_contents(__DIR__.'/data.log', $data); } echo "received length=" . strlen($data) . "\n"; }); $serv->on('close', function ($serv, $fd) { echo "Client: Close.\n"; }); $serv->start(); ```
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2020-12-22
c
ccccA1
Simps\MQTT\Client,直接用的这个也会有这样的问题,在不断的发送的时候,接收会自动断掉。
赞
0
回复
2020-12-23
鲁飞
回复
ccccA1
simps/mqtt咋了?有问题直接在群里找我反馈就行了
赞
0
回复
2020-12-23
鲁飞
这是因为mqtt需要回复相关ack以及心跳等,否则就会断开。
赞
0
回复