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/LuckUserIMClientTest.java | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 96 insertions(+), 0 deletions(-) diff --git a/system-im-client/src/main/java/com/yami/shop/im/client/LuckUserIMClientTest.java b/system-im-client/src/main/java/com/yami/shop/im/client/LuckUserIMClientTest.java new file mode 100644 index 0000000..9e7cfa7 --- /dev/null +++ b/system-im-client/src/main/java/com/yami/shop/im/client/LuckUserIMClientTest.java @@ -0,0 +1,96 @@ +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 +@ClientEndpoint +public class LuckUserIMClientTest { + + 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_USER_URL + .replace("{token}", "VTN5ZTlwZWo0anh1QnBlZjZGNzVKVXlweU1vU0c5OE8zN0x6OUhRVzFTL25mRFdEYjJtRmRtZ2JZamtuVUF6Zw==") + .replace("{userId}", "1808419187023753218"); + WebSocketContainer container = javax.websocket.ContainerProvider.getWebSocketContainer(); + Session session = container.connectToServer(LuckUserIMClientTest.class, URI.create(url)); + + session.getBasicRemote().sendText(buildGroupMsg()); + + + // 等待一段时间,以便接收服务器发送的消息 + Thread.sleep(1000000); + + // 关闭连接 + session.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static String buildMsg() { + MsgBody msg = new MsgBody(); + msg.setAction(MsgAction.SEND_MSG.name()); + msg.setContent(RandomUtil.randomString(100)); + msg.setSystemType(SystemMsgType.USER.getValue()); + msg.setFromId("0"); + msg.setToId("1803696537288138754"); + msg.setMsgType(MsgType.TEXT.name()); + msg.setRecipientType(RecipientType.USER.name()); + return msg.toString(); + } + + private static String buildGroupMsg() { + MsgBody msg = new MsgBody(); + msg.setAction(MsgAction.SEND_MSG.name()); + msg.setContent(RandomUtil.randomString(100)); + msg.setSystemType(SystemMsgType.USER.getValue()); + msg.setFromId("1808419187023753218"); + msg.setToId("1809485947580796928"); + msg.setMsgType(MsgType.TEXT.name()); + msg.setRecipientType(RecipientType.GROUP.name()); + return msg.toString(); + } +} -- Gitblit v1.9.3