基于mall4j产品的二开项目后端
lee
2024-12-18 efdb99f8cecc4afb592afad79c761081d5d5cf22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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();
    }
}