system-im-client/pom.xml | ●●●●● patch | view | raw | blame | history | |
system-im-client/src/main/java/com/yami/shop/im/client/LuckIMClient.java | ●●●●● patch | view | raw | blame | history | |
system-im-client/src/main/java/com/yami/shop/im/client/LuckSystemIMClientTest.java | ●●●●● patch | view | raw | blame | history | |
system-im-client/src/main/java/com/yami/shop/im/client/LuckUserIMClientTest.java | ●●●●● patch | view | raw | blame | history | |
system-im-client/src/main/java/com/yami/shop/im/client/constant/LuckIMClientConstant.java | ●●●●● patch | view | raw | blame | history | |
system-im-client/src/main/java/com/yami/shop/im/client/exception/LuckImException.java | ●●●●● patch | view | raw | blame | history | |
system-im-client/src/test/java/com/mita/im/client/AppTest.java | ●●●●● patch | view | raw | blame | history |
system-im-client/pom.xml
New file @@ -0,0 +1,35 @@ <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.yami.shop</groupId> <artifactId>yami-shop</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>system-im-client</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>com.yami.shop</groupId> <artifactId>yami-shop-bean</artifactId> <version>${yami.shop.version}</version> </dependency> </dependencies> </project> system-im-client/src/main/java/com/yami/shop/im/client/LuckIMClient.java
New file @@ -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)); } } } system-im-client/src/main/java/com/yami/shop/im/client/LuckSystemIMClientTest.java
New file @@ -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(); } } system-im-client/src/main/java/com/yami/shop/im/client/LuckUserIMClientTest.java
New file @@ -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(); } } system-im-client/src/main/java/com/yami/shop/im/client/constant/LuckIMClientConstant.java
New file @@ -0,0 +1,14 @@ package com.yami.shop.im.client.constant; public class LuckIMClientConstant { public static final String LUCK_IM_HOST = "ws://47.121.136.124:8089/"; // public static final String LUCK_IM_HOST = "ws://127.0.0.1:8089/"; /**im websocket 通讯 url**/ public static final String LUCK_IM_MSG_URL = LUCK_IM_HOST + "im/luck/system/{auth}/{clientId}"; /**im websocket 通讯 url**/ public static final String LUCK_IM_USER_URL = LUCK_IM_HOST + "im/luck/user/{token}/{userId}"; } system-im-client/src/main/java/com/yami/shop/im/client/exception/LuckImException.java
New file @@ -0,0 +1,17 @@ package com.yami.shop.im.client.exception; /** * IM异常类 */ public class LuckImException extends RuntimeException{ private static final long serialVersionUID = 2790327436198670240L; public LuckImException(String message) { super(message); } public LuckImException() { super(); } } system-im-client/src/test/java/com/mita/im/client/AppTest.java
New file @@ -0,0 +1,38 @@ package com.mita.im.client; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Unit test for simple App. */ public class AppTest extends TestCase { /** * Create the test case * * @param testName name of the test case */ public AppTest( String testName ) { super( testName ); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite( AppTest.class ); } /** * Rigourous Test :-) */ public void testApp() { assertTrue( true ); } }