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/LuckSystemIMClientTest.java | 81 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 81 insertions(+), 0 deletions(-) diff --git a/system-im-client/src/main/java/com/yami/shop/im/client/LuckSystemIMClientTest.java b/system-im-client/src/main/java/com/yami/shop/im/client/LuckSystemIMClientTest.java new file mode 100644 index 0000000..17df10f --- /dev/null +++ b/system-im-client/src/main/java/com/yami/shop/im/client/LuckSystemIMClientTest.java @@ -0,0 +1,81 @@ +package com.yami.shop.im.client; + +import java.net.URI; + +import javax.websocket.ClientEndpoint; +import javax.websocket.OnClose; +import javax.websocket.OnMessage; +import javax.websocket.Session; +import javax.websocket.WebSocketContainer; + +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.util.IdUtil; +import cn.hutool.core.util.RandomUtil; +import lombok.extern.slf4j.Slf4j; + +/** + * 幸运集社im客户端 后台系统测试样例 + */ +@Slf4j +@ClientEndpoint +public class LuckSystemIMClientTest { + + public void onOpen(Session session) { + log.info("Luck Im Server connected."); + } + + @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"); + } + + + public static void main(String[] args) { + try { + // WebSocket服务器的URL + String url = LuckIMClientConstant.LUCK_IM_MSG_URL.replace("{auth}", MsgAuth.genTodaySystemAuthCode()).replace("{clientId}", IdUtil.fastSimpleUUID()); + WebSocketContainer container = javax.websocket.ContainerProvider.getWebSocketContainer(); + Session session = container.connectToServer(LuckSystemIMClientTest.class, URI.create(url)); + + session.getBasicRemote().sendText(buildMsg()); + session.getBasicRemote().sendText(buildMsg()); + session.getBasicRemote().sendText(buildMsg()); + session.getBasicRemote().sendText(buildMsg()); + session.getBasicRemote().sendText(buildMsg()); + session.getBasicRemote().sendText(buildMsg()); + + + // 等待一段时间,以便接收服务器发送的消息 + Thread.sleep(1000000); + + // 关闭连接 + session.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static String buildMsg() { + MsgBody msg = new MsgBody(); + msg.setAction(MsgAction.SEND_SYSTEM_MSG.name()); + msg.setContent(RandomUtil.randomString(50)); + msg.setSystemType(SystemMsgType.GROUP_MEMBER_JOIN.getValue()); + msg.setFromId("0"); + msg.setToId("1803696537288138754"); + msg.setMsgType(MsgType.TEXT.name()); + msg.setRecipientType(RecipientType.USER.name()); + return msg.toString(); + } +} -- Gitblit v1.9.3