我正在创建一个应用程序,该应用程序的按钮显示为“加入会议室”,当您单击该按钮时,您将进入一个虚拟的会议室,在那里您可以看到更多的用户进入。 我的想法是为实例nameRoom创建一个主题,并且每次用户加入会议室时,它都会自动为其订阅订阅,因此,如果有其他更新,他/她将收到例如一个已加入或剩下一个的更新。 我被困的是: “管理员”可以创建“房间”,因此,每次管理员创建房间时,它应该是一个新主题,对吗?所以,我的问题是,一旦我进入一个房间,我想像倒计时一样创建30秒,当这30秒完成后,它开始问我问题,每个人都可以回答这个问题,我需要看看有多少用户回答了,有多少没有回答,这是另一个主题吗?
流为:
要明确:
我想知道如何创建主题,然后使用Spring发布给他们。例如,创建房间不是MQTT的必要,而是检查谁加入了这个东西,所以我问这个问题,如何用MQTT创建这个房间?
此外,MQTT将负责说所有信息?我的意思是每个房间都有一些问题,所以有必要通过MQTT了解排名等信息吗?
答案 0 :(得分:2)
1)您需要创建适合您的应用程序需求的数据库
数据库名称:ChatRoom
表格:
2)设置一个mqtt服务器,该服务器允许同时在mqtt和websockets上进行连接(以支持javascript应用程序)
3)现在使用以下api和网页创建一个spring boot应用程序
网页:
chatroom.html
chatroom.js
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;
}
}
答案 1 :(得分:1)
在我的旧项目中,我创建了类似于您所需的内容。 我仍然确信Google(和Apple)通知系统会更好。无论如何,这里您需要什么。
您可以使用Eclipse Paho来生成和使用MQTT消息。 在我的Android应用程序build.gradle文件中,我添加了:
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
现在有一个较新版本的库
此库为您提供了所有必需的API,以便在Android设备中以及从Android设备使用和产生MQTT消息。
在文档部分,您可以找到一个示例应用程序。您可以从那里开始
在服务器端,我使用Apache ActiveMQ作为代理。它提供了MQTT处理程序的嵌入式实现,您可以创建主题和队列以处理MQTT消息。
我希望它有用
天使
编辑部分
假设您要在服务器端使用ActiveMQ。
您必须下载并安装activemq。在目录${activemq_home}/conf
内的activemq.xml文件中,您将找到mqtt配置。是这行:
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
这意味着activemq在端口1883(mqtt的默认TCP / IP端口)上处理mqtt协议消息。
在activemq的管理控制台上,您可以创建要用于消息的主题或队列。在应用程序中,您必须将Paho服务连接到创建的主题或队列。
请注意,默认情况下,activemq在内存DB中使用。我建议您配置它以便使用普通的RDBMS甚至NoSQL DB。最重要的是,您对其进行配置是为了将所有消息都存储在内存中,否则可能会导致消息丢失。
此外,如果您在activemq上公开互联网,我强烈建议您通过安全凭据或使用SSL证书对其进行保护。