|
@@ -4,7 +4,6 @@ package cn.org.bjca.trust.java.imserver.im;
|
|
|
import cn.org.bjca.trust.java.imserver.common.SpringUtilsAuTo;
|
|
|
import cn.org.bjca.trust.java.imserver.common.json.GsonImplHelp;
|
|
|
import cn.org.bjca.trust.java.imserver.entitys.GroupEntity;
|
|
|
-import cn.org.bjca.trust.java.imserver.entitys.UserInfo;
|
|
|
import cn.org.bjca.trust.java.imserver.enums.PacketType;
|
|
|
import cn.org.bjca.trust.java.imserver.im.bean.PacketMessage;
|
|
|
import cn.org.bjca.trust.java.imserver.im.msg.ConnectMessage;
|
|
@@ -18,6 +17,7 @@ import org.eclipse.paho.client.mqttv3.*;
|
|
|
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.List;
|
|
|
|
|
|
public class ImManager {
|
|
|
|
|
@@ -50,17 +50,17 @@ public class ImManager {
|
|
|
|
|
|
try {
|
|
|
mqttClient = new MqttClient("tcp://114.115.203.60:18883",
|
|
|
- "server1" + System.currentTimeMillis(), new MemoryPersistence());
|
|
|
+ "server" + System.currentTimeMillis(), new MemoryPersistence());
|
|
|
mqttClient.setCallback(new MqttCallbackExtended() {
|
|
|
@Override
|
|
|
public void connectComplete(boolean reconnect, String serverURI) {
|
|
|
System.out.println("======>connectComplete:reconnect?" + reconnect + "::" + serverURI);
|
|
|
if (reconnect) return;
|
|
|
try {
|
|
|
- mqttClient.subscribe("server1" + PacketType.CONNECT, 2);
|
|
|
- mqttClient.subscribe("server1" + PacketType.SEND, 2);
|
|
|
- mqttClient.subscribe("server1" + PacketType.PING, 2);
|
|
|
- mqttClient.subscribe("server1" + PacketType.REQUEST, 2);
|
|
|
+ mqttClient.subscribe("server" + PacketType.CONNECT, 2);
|
|
|
+ mqttClient.subscribe("server" + PacketType.SEND, 2);
|
|
|
+ mqttClient.subscribe("server" + PacketType.PING, 2);
|
|
|
+ mqttClient.subscribe("server" + PacketType.REQUEST, 2);
|
|
|
} catch (MqttException ignored) {
|
|
|
}
|
|
|
}
|
|
@@ -74,12 +74,11 @@ public class ImManager {
|
|
|
public void messageArrived(String topic, MqttMessage message) throws Exception {
|
|
|
String msg = new String(message.getPayload());
|
|
|
System.out.println("======>messageArrived:\n" + topic + "::" + msg);
|
|
|
- if (topic.equals("server1" + PacketType.CONNECT)) {
|
|
|
+ if (topic.equals("server" + PacketType.CONNECT)) {
|
|
|
sendConnectedMsg(GsonImplHelp.get().toObject(msg, ConnectMessage.class));
|
|
|
- } else if (topic.equals("server1" + PacketType.SEND)) {
|
|
|
+ } else if (topic.equals("server" + PacketType.SEND)) {
|
|
|
SZYXMessage szyxMessage = GsonImplHelp.get().toObject(msg, SZYXMessage.class);
|
|
|
szyxMessage.setStatus(0);
|
|
|
- szyxMessage.setFromClientId(szyxMessage.getFromClient().getUserId());
|
|
|
saveConversation(szyxMessage);
|
|
|
|
|
|
SZYXMessage message1 = szyxMessage.copy();
|
|
@@ -101,7 +100,7 @@ public class ImManager {
|
|
|
sendAckMsg(szyxMessage);
|
|
|
|
|
|
}// 数据请求
|
|
|
- else if (topic.equals("server1" + PacketType.REQUEST)) {
|
|
|
+ else if (topic.equals("server" + PacketType.REQUEST)) {
|
|
|
sendPacketMessage(PacketType.RESULT, RequestManager.getInstance().request(msg));
|
|
|
}
|
|
|
|
|
@@ -120,11 +119,18 @@ public class ImManager {
|
|
|
|
|
|
private void saveConversation(SZYXMessage szyxMessage) {
|
|
|
try {
|
|
|
- ConversationMessage conversation = new ConversationMessage();
|
|
|
+ List<ConversationMessage> l = conversationRepository.getConversation(szyxMessage.getAppId(), szyxMessage.getUserName(),
|
|
|
+ szyxMessage.isArrive() ? szyxMessage.getFromClientId() : szyxMessage.getToClientId(), szyxMessage.getGroupID());
|
|
|
+ ConversationMessage conversation;
|
|
|
+ if (l.size() == 0) {
|
|
|
+ conversation = new ConversationMessage();
|
|
|
+ } else {
|
|
|
+ conversation = l.get(0);
|
|
|
+ }
|
|
|
+// ConversationMessage conversation = new ConversationMessage();
|
|
|
conversation.setGroup(szyxMessage.isGroup());
|
|
|
conversation.setSender(!szyxMessage.isArrive());
|
|
|
conversation.setMessage(szyxMessage);
|
|
|
- conversation.setSelf(userRepository.findFirstByUserIdAndOsTypeAndAppId(szyxMessage.getUserName(), szyxMessage.getOsType(), szyxMessage.getAppId()));
|
|
|
|
|
|
conversation.setAppId(szyxMessage.getAppId());
|
|
|
conversation.setUserName(szyxMessage.getUserName());
|
|
@@ -134,18 +140,10 @@ public class ImManager {
|
|
|
if (szyxMessage.isGroup()) {
|
|
|
conversation.setGroupEntity(groupRepository.findFirstByAppIdAndGroupId(szyxMessage.getAppId(), szyxMessage.getGroupID()));
|
|
|
} else {
|
|
|
- UserInfo userInfo;
|
|
|
- if (szyxMessage.isArrive()) {
|
|
|
- userInfo = userRepository.findFirstByUserIdAndOsTypeAndAppId(szyxMessage.getFromClientId(), szyxMessage.getOsType(), szyxMessage.getAppId());
|
|
|
- } else {
|
|
|
- userInfo = userRepository.findFirstByUserIdAndOsTypeAndAppId(szyxMessage.getToClientId(), szyxMessage.getOsType(), szyxMessage.getAppId());
|
|
|
- }
|
|
|
- conversation.setUserInfo(userInfo);
|
|
|
+ conversation.setUserId(szyxMessage.isArrive() ? szyxMessage.getFromClientId() : szyxMessage.getToClientId());
|
|
|
}
|
|
|
|
|
|
conversationRepository.save(conversation);
|
|
|
- conversation.getSelf().setPassword("");
|
|
|
- conversation.getUserInfo().setPassword("");
|
|
|
sendPacketMessage(PacketType.CONVERSATION, conversation);
|
|
|
} catch (Exception e) {
|
|
|
System.out.println("--------------" + e.getMessage());
|
|
@@ -155,7 +153,10 @@ public class ImManager {
|
|
|
public void connect() {
|
|
|
new Thread(() -> {
|
|
|
try {
|
|
|
- if (!mqttClient.isConnected()) {
|
|
|
+ if (null != mqttClient) {
|
|
|
+ if (mqttClient.isConnected()) {
|
|
|
+ mqttClient.disconnect();
|
|
|
+ }
|
|
|
mqttClient.connect(connectOptions);
|
|
|
}
|
|
|
} catch (MqttException e) {
|