| | |
| | | public void connect() { |
| | | try { |
| | | //创建MQTT客户端对象 |
| | | client = new MqttClient(mqttProperties.getHost(), mqttProperties.getClientInId(), new MemoryPersistence()); |
| | | client = new MqttClient(mqttProperties.getHost(), mqttProperties.getClientId(), new MemoryPersistence()); |
| | | //连接设置 |
| | | MqttConnectOptions options = new MqttConnectOptions(); |
| | | //是否清空session,设置为false表示服务器会保留客户端的连接记录,客户端重连之后能获取到服务器在客户端断开连接期间推送的消息 |
| | |
| | | //设置心跳时间 单位为秒,表示服务器每隔1.5*20秒的时间向客户端发送心跳判断客户端是否在线 |
| | | options.setKeepAliveInterval(20); |
| | | //设置遗嘱消息的话题,若客户端和服务器之间的连接意外断开,服务器将发布客户端的遗嘱信息 |
| | | options.setWill("willTopic", (mqttProperties.getClientInId() + "与服务器断开连接").getBytes(), 0, false); |
| | | options.setWill("willTopic", (mqttProperties.getClientId() + "与服务器断开连接").getBytes(), 0, false); |
| | | //设置回调 |
| | | // client.setCallback(new MqttConsumerCallBack()); |
| | | client.setCallback(mqttConsumerCallBack); |