dromara / mica-mqtt

java mqtt 基于 java aio 实现,开源、简单、易用、低延迟、高性能百万级 java mqtt client 组件和 java mqtt broker 服务。🔝🔝 记得右上角点个star 关注更新!
https://iot.bladex.cn
Apache License 2.0
296 stars 55 forks source link

Java 如何连接MQTT的WebSocket端口 #13

Closed litongjava closed 11 months ago

litongjava commented 11 months ago
package com.litongjava.mica.mqtt.client.config;

import java.nio.charset.StandardCharsets;
import java.util.Timer;
import java.util.TimerTask;

import org.tio.core.ChannelContext;

import com.litongjava.jfinal.aop.annotation.Bean;
import com.litongjava.jfinal.aop.annotation.Configuration;

import lombok.extern.slf4j.Slf4j;
import net.dreamlu.iot.mqtt.codec.MqttPublishMessage;
import net.dreamlu.iot.mqtt.codec.MqttQoS;
import net.dreamlu.iot.mqtt.core.client.IMqttClientMessageListener;
import net.dreamlu.iot.mqtt.core.client.MqttClient;
import net.dreamlu.iot.mqtt.core.client.MqttClientCreator;

@Configuration
@Slf4j
public class MicaMQTTClientConfig {

  @Bean
  public MqttClient MqttClient() {
    // 初始化 mqtt 客户端
    MqttClientCreator creator = MqttClient.create().ip("192.168.3.9").port(1883).username("mica").password("mica");
    // 连接监听
    MqttClient client = creator.connectListener(new MqttClientConnectListener()).willMessage(builder -> {
      builder.topic("/test/offline").messageText("down").retain(false).qos(MqttQoS.AT_MOST_ONCE); // 遗嘱消息
    })
        // 同步连接,也可以使用 connect() 异步(可以避免 broker 没启动照成启动卡住),但是下面的订阅和发布可能还没连接成功。
        .connectSync();

    // 订阅
    client.subQos0("/test/123", new IMqttClientMessageListener() {
      @Override
      public void onSubscribed(ChannelContext context, String topicFilter, MqttQoS mqttQoS) {
        // 订阅成功之后触发,可在此处做一些业务逻辑
        log.info("topicFilter:{} MqttQoS:{} Subscription successful", topicFilter, mqttQoS);
      }

      @Override
      public void onMessage(ChannelContext context, String topic, MqttPublishMessage message, byte[] payload) {
        log.info(topic + '\t' + new String(payload, StandardCharsets.UTF_8));
      }
    });

    // 发送
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        client.publish("/test/client", "hello this is mica client".getBytes(StandardCharsets.UTF_8));
      }
    }, 1000, 2000);

    return client;
  }
}

下面的代码连接是1883,mqtt-server的tcp端口,如何连接websocket端口

ChunMengLu commented 11 months ago

@litongjava 能用 java 的地方就是能直连 tcp 端口的,websocket 是为浏览器端而设计的(不能直接发起 tcp)。tcp 端口的mqtt协议报文更小,性能更好!!!所以我觉得 java websocket 连接可能是个伪需求!

litongjava commented 11 months ago

在某些情况下,服务器是不开放tcp端口出来的。我想把它部署到阿里云函数,然后使用websocket的进行通讯。

前端向web socket的发送消息,已经已经测试通了。 Java如何把消息取下来,然后再发送上去呢?

On Mon, Dec 18, 2023, 2:41 PM 如梦技术 @.***> wrote:

能用 java 的地方可能是能直连 tcp 端口的,websocket 是为浏览器端(不能直接发起 tcp)而设计的。tcp 端口的mqtt协议报文更小,性能更好!

— Reply to this email directly, view it on GitHub https://github.com/lets-mica/mica-mqtt/issues/13#issuecomment-1861918030, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHSKMPORQUYGTYZEGEGV53TYKDPBZAVCNFSM6AAAAABAWSRYESVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNRRHEYTQMBTGA . You are receiving this because you authored the thread.Message ID: @.***>

ChunMengLu commented 11 months ago

在某些情况下,服务器是不开放tcp端口出来的。我想把它部署到阿里云函数,然后使用websocket的进行通讯。 前端向web socket的发送消息,已经已经测试通了。 Java如何把消息取下来,然后再发送上去呢? On Mon, Dec 18, 2023, 2:41 PM 如梦技术 @.> wrote: 能用 java 的地方可能是能直连 tcp 端口的,websocket 是为浏览器端(不能直接发起 tcp)而设计的。tcp 端口的mqtt协议报文更小,性能更好! — Reply to this email directly, view it on GitHub <#13 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHSKMPORQUYGTYZEGEGV53TYKDPBZAVCNFSM6AAAAABAWSRYESVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNRRHEYTQMBTGA . You are receiving this because you authored the thread.Message ID: @.>

试试 https://github.com/hivemq/hivemq-mqtt-client 我看了下它支持,而且是比较靠谱。俺也是个打工仔,目前是没时间没精力去支持。