From efdb99f8cecc4afb592afad79c761081d5d5cf22 Mon Sep 17 00:00:00 2001
From: lee <4766465@qq.com>
Date: Wed, 18 Dec 2024 13:27:00 +0800
Subject: [PATCH] init

---
 system-im-client/src/main/java/com/yami/shop/im/client/LuckIMClient.java |  113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 113 insertions(+), 0 deletions(-)

diff --git a/system-im-client/src/main/java/com/yami/shop/im/client/LuckIMClient.java b/system-im-client/src/main/java/com/yami/shop/im/client/LuckIMClient.java
new file mode 100644
index 0000000..8fc027c
--- /dev/null
+++ b/system-im-client/src/main/java/com/yami/shop/im/client/LuckIMClient.java
@@ -0,0 +1,113 @@
+package com.yami.shop.im.client;
+
+import java.net.URI;
+
+import javax.annotation.PostConstruct;
+import javax.websocket.ClientEndpoint;
+import javax.websocket.OnClose;
+import javax.websocket.OnMessage;
+import javax.websocket.OnOpen;
+import javax.websocket.Session;
+import javax.websocket.WebSocketContainer;
+
+import org.springframework.stereotype.Component;
+
+import com.yami.shop.bean.im.MsgAction;
+import com.yami.shop.bean.im.MsgBody;
+import com.yami.shop.bean.im.MsgType;
+import com.yami.shop.bean.im.RecipientType;
+import com.yami.shop.bean.im.SystemMsgType;
+import com.yami.shop.bean.im.security.MsgAuth;
+import com.yami.shop.im.client.constant.LuckIMClientConstant;
+
+import cn.hutool.core.exceptions.ExceptionUtil;
+import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.ObjUtil;
+import cn.hutool.core.util.RandomUtil;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * 幸运集社im客户端
+ */
+@Slf4j
+@Component
+@ClientEndpoint
+public class LuckIMClient {
+	
+	private Session session;
+	
+	@PostConstruct
+	public void init() {
+		try {
+			String url = LuckIMClientConstant.LUCK_IM_MSG_URL.replace("{auth}", MsgAuth.genTodaySystemAuthCode()).replace("{clientId}", IdUtil.fastSimpleUUID());
+			WebSocketContainer container = javax.websocket.ContainerProvider.getWebSocketContainer();
+	        session = container.connectToServer(LuckIMClient.class, URI.create(url));
+		} catch (Exception e) {
+			log.error("LuckIMClient init Exception: " + ExceptionUtil.stacktraceToOneLineString(e));
+		}
+	}
+	
+	@OnOpen
+    public void onOpen(Session session) {
+        log.info("Luck Im Server connected.");
+        heartBeat();
+    }
+
+	@OnMessage
+    public void onMessage(String message) {
+    	log.info("Luck Im Server onMessage: " + message);
+    }
+    
+    @OnClose
+    public void onClose(Session session) {
+    	log.info("Luck Im Server closed");
+    }
+    
+    /**
+     * 心跳处理
+     */
+    private void heartBeat() {
+		while(session != null && session.isOpen()) {
+			try {
+				session.getBasicRemote().sendText(MsgBody.getHeartBeatMsgBody().toString());
+				Thread.sleep(1000 * 30);//每30秒发送心跳
+			} catch (Exception e) {
+				log.error("heartBeat Exception: " + ExceptionUtil.stacktraceToString(e));
+			}
+		}
+	}
+
+    
+    /**
+     * 发送系统消息给用户
+     * @param userId 用户id
+     * @param systemMsgType 系统消息类型
+     * @param content 内容
+     */
+    public void sendSystemMsgToUser(String userId,SystemMsgType systemMsgType,Object content) {
+    	if(ObjUtil.isAllEmpty(userId,systemMsgType,content)) {
+    		throw new IllegalArgumentException();
+    	}
+    	if(session == null || !session.isOpen()) {
+    		log.error("系统im初始化失败或链接已断开");
+    		return;
+    	}
+    	
+    	MsgBody msgBody = new MsgBody();
+    	msgBody.setAction(MsgAction.SEND_SYSTEM_MSG.name());
+    	msgBody.setContent(content);
+    	msgBody.setRecipientType(RecipientType.USER.name());
+    	msgBody.setFromId(MsgBody.getSystemId());
+    	msgBody.setMsgType(MsgType.SYSTEM.name());
+    	msgBody.setSystemType(systemMsgType.getValue());
+    	msgBody.setToId(userId);
+    	msgBody.setMsgId(MsgBody.randomMessageId());
+    	msgBody.setTimestamp(System.currentTimeMillis());
+    	try {
+    		session.getBasicRemote().sendText(msgBody.toString());
+		} catch (Exception e) {
+			log.error("sendSystemMsgToUser Exception: " + ExceptionUtil.stacktraceToString(e));
+		}
+    }
+    
+}

--
Gitblit v1.9.3