代码之家  ›  专栏  ›  技术社区  ›  StuartDTO

Spring boot创建一个主题并发布,以便应用程序可以接收它

  •  2
  • StuartDTO  · 技术社区  · 7 年前

    我正在创建一个应用程序,其中有一个按钮,上面写着“加入房间”,当你点击它时,你会加入一个想象中的房间,在那里你可以看到更多的用户进入。 我被困的地方是: “管理员”可以创建一个“房间”,所以每次管理员创建一个房间时,它应该是一个新主题,对吗?所以,我的问题是,一旦我进入一个房间,我想创建一个倒计时,比如说30秒,当这30秒结束时,它开始问我问题,每个人都可以回答这个问题,我需要看看有多少用户回答了,有多少没有,这是另一个话题?

    流程是:

    1. User1加入Room1,只看到您在这个房间里
    2. User2加入了这个房间,看到这个房间里有两个人(依此类推,直到User5)
    3. 然后计时器下降30到0
    4. 然后作为一个用户,我看到“迈克尔·乔丹多大了”和4复选框,每个人都可以回答
    5. 此外,还有一个字段表示已经发布了多少个答案,因此,如果问题时间为30秒,并且他们是5个用户,并且他们在不到30秒的时间内回答,则问题必须移动到下一个屏幕,即
    6. 前3名,回答更快,得分更高

    要明确的是:

    我想知道如何创建主题,然后使用Spring发布给他们。例如,创建房间不是MQTT所必需的,而是检查谁加入以及它是什么,所以我问这个问题,我如何使用MQTT创建这个房间?

    此外,MQTT将负责说出所有信息?我的意思是每个房间都有一些问题,所以有必要通过MQTT了解排名等?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Santosh Balaji    7 年前

    1) 您需要创建适合应用程序需要的数据库

    数据库名称:聊天室

    • 主题(用于存储所有mqtt文件室主题名称)
    • 文件室(每个文件室与一个主题关联)
    • 用户(每个用户与一个房间关联)

    网页:

    • 聊天室.html

    • 聊天室

    应用程序编程接口:

    • 加入聊天室(针对新用户)

    • 管理员使用chatroom.html网页创建新的聊天室。在创建时,它调用create chatroom api来创建新的聊天室。在api中,它将为新聊天室订阅一个新主题。

    对于javascript http://www.steves-internet-guide.com/using-javascript-mqtt-client-websockets/

    对于java https://www.eclipse.org/paho/clients/java/

    public final class MessageQueueClient implements MqttCallback
    {
        private MqttClient mqttClient;
    
        private MessageQueueClient() 
        {
        }
    
        public static MessageQueueClient getInstance() 
        {
            return messageQueueClient;
        }
    
        @Override
        public void connectionLost(Throwable cause) 
        {
        }
    
        @Override
        public void messageArrived(String topic, MqttMessage message) 
        {
        }
    
        @Override
        public void deliveryComplete(IMqttDeliveryToken token) 
        {
        }
    
        //Call this method on server startup to connect to mqtt server(spring boot app start)
        public boolean connect(String hostname, String clientuniqueid) 
        {
            try 
            {
                if (mqttCredentialsDTO != null) 
                {
                    MqttConnectOptions options = new MqttConnectOptions();
                    options.setAutomaticReconnect(true);
                    options.setCleanSession(true);
                    mqttClient = new MqttClient(hostname, clientuniqueid);
                    mqttClient.connect(options);
                    return true;
                }
            } 
            catch (Exception e) 
            {
                e.printStacktrace();
            }
            return false;
        }
    
        //Call this method on server shutdown to disconnect from mqtt server
        public boolean disconnect() 
        {
            try 
            {
                if (mqttClient != null) 
                {
                    mqttClient.disconnect();
                    mqttClient.close();
                    return true;
                }
            } 
            catch (MqttException e) 
            {
                e.printStacktrace();
            }
            return false;
        }
    
        //call this method after mqtt connection established to subscribe to any topic
        public boolean subscribe(String topicName, int qos) 
        {
            try 
            {
                if (topicName != null) 
                {
                    mqttClient.subscribe(topicName, qos);
                    return true;
                }
            } 
            catch (MqttException e) 
            {
                e.printStacktrace();
            }
            return false;
        }
    
    
        //call this method after mqtt connection established to publish to any topic
        public boolean publish(String topicName, String message, int qos) 
        {
            try 
            {
                if (topicName != null) 
                {
                    MqttMessage mqttMessage = new MqttMessage();
                    mqttMessage.setPayload(message.getBytes());
                    mqttMessage.setQos(qos);
                    mqttClient.publish(topicName, mqttMessage);
                    return true;
                }
            } 
            catch (MqttException e) 
            {
                e.printStacktrace();
            }
            return false;
        }
    }
    
        2
  •  1
  •   Angelo Immediata    7 年前

    在我以前的项目中,我创建了一些类似于您需要的东西。 我仍然相信谷歌(和苹果)的通知系统更好。无论如何,这里是你需要的。

    你可以用 Eclipse Paho 在我的android应用程序build.gradle文件中,我添加了:

     compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
    

    该库为您提供了在Android设备中使用和生成MQTT消息所需的所有API。

    在文档部分,您可以找到一个示例应用程序。你可以从那里开始

    Apache ActiveMQ

    我希望它有用

    安杰洛

    假设您希望在服务器端使用ActiveMQ。

    ${activemq_home}/conf 您将找到mqtt配置。是这样的:

    <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    

    这意味着activemq在端口1883(mqtt默认TCP/IP端口)上处理mqtt协议消息。

    请注意,默认情况下activemq使用内存数据库。我建议YOU对其进行配置,以便使用普通的RDBMS甚至NoSQL DB。最重要的是,为了存储所有不在内存中的消息,您必须对其进行配置,否则可能会丢失消息。

    此外,如果您在internet上公开activemq,我强烈建议您通过安全凭据或使用SSL证书来保护它。