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