EMQ X 之 emqx_web_hook


前言

       该文仅为个人学习记录

       EMQ版本:3.1

       个人仅用于处理设备离线。


配置

       软件根目录/etc/emqx/plugins/emqx_web_hook.conf

#设置访问地址
web.hook.api.url = http://192.168.100.151:8080/webHook

#这里设置了各种事件
web.hook.rule.client.connected.1     = {"action": "on_client_connected"}
web.hook.rule.client.disconnected.1  = {"action": "on_client_disconnected"}
web.hook.rule.client.subscribe.1     = {"action": "on_client_subscribe"}
web.hook.rule.client.unsubscribe.1   = {"action": "on_client_unsubscribe"}
web.hook.rule.session.created.1      = {"action": "on_session_created"}
web.hook.rule.session.subscribed.1   = {"action": "on_session_subscribed"}
web.hook.rule.session.unsubscribed.1 = {"action": "on_session_unsubscribed"}
web.hook.rule.session.terminated.1   = {"action": "on_session_terminated"}
web.hook.rule.message.publish.1      = {"action": "on_message_publish"}
web.hook.rule.message.deliver.1    = {"action": "on_message_deliver"}
web.hook.rule.message.acked.1        = {"action": "on_message_acked"}

接口

/**
 * @Description emqx_web_hook插件相关 此处主要用于监听设备连接情况
 * @Author 姚句
 * @Date 2019/6/10 11:42
 **/
@Controller
public class WebHookController {


    @PostMapping("/webHook")
    public void onWebHook(@RequestBody Map<String,String> param){
        String action = param.get(WebHookConstant.ACTION);
        String username = param.get(WebHookConstant.USERNAME);
        //String reson = param.get(WebHookConstant.REASON);
        //String clientId = param.get(WebHookConstant.CLINET_ID);
        if (WebHookConstant.ACTION_DISCONNECTED.equals(action)){
            onClientConnected(username);
        }
    }

    /**
     * 设备连接中断监听
     */
    private void onClientConnected(String username){
        //...
    }

}
/**
 * @Description emqx_web_hook插件相关常量类
 * @Author 姚句
 * @Date 2019/6/10 14:22
 **/
public class WebHookConstant {

    //固定的属性名
    public static final String ACTION = "action";
    public static final String CLINET_ID= "client_id";
    public static final String USERNAME = "username";
    public static final String REASON = "reason";

    //ACTION
    public static final String ACTION_DISCONNECTED = "client_disconnected"; //断开连接

}

说明

       当设备触发了对应的事件后,消息服务都会去调用web.hook.api.url配置的地址,随后在接口中使用action值来判断对应哪个事件,而后进行对应的操作。

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值