EMQX通过webhook统计设备上下线
emqx mqtt修改emqx配置文件
web.hook.url = http://127.0.0.1:8080/emqx/webhook
// 取消这两项的注释
web.hook.rule.client.connected.1 = {"action": "on_client_connected"}
web.hook.rule.client.disconnected.1 = {"action": "on_client_disconnected"}
打开web管理→ 模块→emqx_web_hook 启动
post请求接口
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* emqx的webhook通知
*/
@Slf4j
@RestController
@RequestMapping("/emqx/")
public class WebHookController {
@PostMapping("/webhook")
public void webhook(@RequestBody WebHookRequestBody body) {
System.out.println(body);
}
}
import lombok.Data;
import java.io.Serializable;
@Data
public class WebHookRequestBody implements Serializable {
/**
* 事件名称
* client.connected 成功接入 客户端认证完成并成功接入系统后
* client.disconnected 连接断开 客户端连接层在准备关闭时
*/
private String action;
/**
* 客户端 ClientId
*/
private String clientid;
/**
* 客户端 Username,不存在时该值为 "undefined"
*/
private String username;
/**
* 客户端源 IP 地址
*/
private String ipaddress;
/**
* 客户端申请的心跳保活时间
*/
private Integer keepalive;
/**
* 协议版本号
*/
private Integer proto_ver;
/**
* 时间戳(秒)
*/
private Long connected_at;
/**
* 错误原因
*/
private String reason;
}
参考资料: