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();
|
}
|
}
|