{"id":19036165,"url":"https://github.com/tx7do/mqtt-http-auth-example","last_synced_at":"2026-05-05T21:30:15.681Z","repository":{"id":112433653,"uuid":"476753345","full_name":"tx7do/mqtt-http-auth-example","owner":"tx7do","description":null,"archived":false,"fork":false,"pushed_at":"2022-04-19T07:39:20.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-02T05:32:21.551Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tx7do.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-01T14:28:58.000Z","updated_at":"2022-04-01T14:31:14.000Z","dependencies_parsed_at":"2023-05-15T02:45:22.286Z","dependency_job_id":null,"html_url":"https://github.com/tx7do/mqtt-http-auth-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tx7do%2Fmqtt-http-auth-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tx7do%2Fmqtt-http-auth-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tx7do%2Fmqtt-http-auth-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tx7do%2Fmqtt-http-auth-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tx7do","download_url":"https://codeload.github.com/tx7do/mqtt-http-auth-example/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240093142,"owners_count":19746774,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T21:53:38.781Z","updated_at":"2026-05-05T21:30:15.309Z","avatar_url":"https://github.com/tx7do.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mqtt-http-auth-example\r\n\r\nMQTT服务器使用HTTP认证的实例代码\r\n\r\n## RabbitMQ\r\n\r\n* rabbitmq_mqtt插件，提供MQTT协议的支持；\r\n* rabbitmq_web_mqtt插件，提供Web MQTT协议的支持；\r\n* rabbitmq_auth_backend_http插件，提供HTTP认证的支持。\r\n\r\n## EMQX\r\n\r\n* emqx_auth_http插件，用于认证和权鉴\r\n* emqx_web_hook插件，用于客户端连接和断开等动作的时候感知到其行为。\r\n\r\n## 注意事项\r\n\r\n### MQTT消息丢失\r\n\r\n消息丢失有两个方面的原因，一是QoS等级配置，二是会话id配置。\r\n\r\n首先对于QoS等级来说，因为存在推送端发往服务器和服务器发往接收端两个过程，所以消息的最终QoS是取决于两者中较低的一个。比如我们项目中一开始服务器端QoS是1，但是Android端订阅的QoS等级是0，就导致了手机端经常收不到消息，而mqtt服务器显示消息已发送。这个问题在Android端更改QoS等级为1后得到了解决。\r\n\r\n其次，如果客户端断开了和MQTT服务器的连接，当Qos等级为0时，离线消息将被丢弃，当QoS等级为1时，离线消息插入队列。但是在使用过程中发现，客户端重连后旧队列连同消息被销毁，重新创建了一个新的队列。查阅文档后发现，要正确接收到离线消息，必须设置QoS等级1以上，会话id保持不变。对于paho来说，建立连接时必须指定cleanSession参数为false。\r\n\r\n### MQTT连接断开\r\n\r\n这个问题表现和上个问题一样，但只在外网发生，即无法收到MQTT消息，导致一直没有成功定位，耗费了大量的时间。\r\n\r\n考虑到耗电问题，Android客户端的MQTT心跳时间设置为20分钟。而在国内网络环境下，网络运营商大量使用NAT技术，而NAT设备会淘汰不活跃的连接。TCP是一个无状态的协议，客户端未接收到RST包，因此在发送心跳包前都无法得知连接状态。NAT设备的淘汰算法无法得知，经测试淘汰时间也不固定，因此只能通过缩减心跳时间来解决。\r\n\r\n经测试，心跳时间设置为3分钟能很好收到消息，最久的一次在20s后收到；心跳时间设置为5分钟时，连接断开较为频繁，经常在1分钟后才接收到推送消息。\r\n\r\n### clientId 要唯一\r\n\r\n在客户端connect连接的时，会有一个clientId 参数，需要每个客户端都保持唯一的。\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftx7do%2Fmqtt-http-auth-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftx7do%2Fmqtt-http-auth-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftx7do%2Fmqtt-http-auth-example/lists"}