mirror of https://gitee.com/stylefeng/roses
【message】 websocket发送消息
parent
c7d5392fbd
commit
d36b61f3be
|
@ -0,0 +1,29 @@
|
|||
package cn.stylefeng.roses.kernel.message.api;
|
||||
|
||||
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
|
||||
import cn.stylefeng.roses.kernel.message.api.enums.MessageReadFlagEnum;
|
||||
import cn.stylefeng.roses.kernel.message.api.pojo.MessageParam;
|
||||
import cn.stylefeng.roses.kernel.message.api.pojo.MessageResponse;
|
||||
import cn.stylefeng.roses.kernel.message.api.pojo.MessageSendParam;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 系统消息websocket相关接口
|
||||
*
|
||||
* @author liuhanqing
|
||||
* @date 2021/1/26 18:14
|
||||
*/
|
||||
public interface WebsocketApi {
|
||||
|
||||
/**
|
||||
* 发送websocket系统消息
|
||||
*
|
||||
* @param userIdList userId 集合
|
||||
* @param messageSendParam 系统消息参数
|
||||
* @author liuhanqing
|
||||
* @date 2021/1/26 18:17
|
||||
*/
|
||||
void sendWebSocketMessage(List<Long> userIdList, MessageSendParam messageSendParam);
|
||||
|
||||
}
|
|
@ -1,11 +1,13 @@
|
|||
package cn.stylefeng.roses.kernel.message.api.pojo;
|
||||
|
||||
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 发送系统消息的参数
|
||||
|
@ -56,4 +58,9 @@ public class MessageSendParam extends BaseRequest {
|
|||
@NotBlank(message = "业务类型不能为空", groups = {add.class, edit.class})
|
||||
private String businessType;
|
||||
|
||||
/**
|
||||
* 消息发送时间
|
||||
*/
|
||||
private Date messageSendTime;
|
||||
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import org.springframework.validation.annotation.Validated;
|
|||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -43,6 +44,7 @@ public class SysMessageController {
|
|||
*/
|
||||
@PostResource(name = "发送系统消息", path = "/sysMessage/sendMessage")
|
||||
public ResponseData sendMessage(@RequestBody @Validated(MessageSendParam.add.class) MessageSendParam messageSendParam) {
|
||||
messageSendParam.setMessageSendTime(new Date());
|
||||
messageApi.sendMessage(messageSendParam);
|
||||
return new SuccessResponseData();
|
||||
}
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
package cn.stylefeng.roses.kernel.message.db;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
|
||||
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
|
||||
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
|
||||
import cn.stylefeng.roses.kernel.message.api.MessageApi;
|
||||
import cn.stylefeng.roses.kernel.message.api.WebsocketApi;
|
||||
import cn.stylefeng.roses.kernel.message.api.constants.MessageConstants;
|
||||
import cn.stylefeng.roses.kernel.message.api.enums.MessageReadFlagEnum;
|
||||
import cn.stylefeng.roses.kernel.message.api.exception.MessageException;
|
||||
|
@ -39,6 +41,9 @@ import java.util.stream.Collectors;
|
|||
public class MessageDbServiceImpl implements MessageApi {
|
||||
|
||||
|
||||
@Resource
|
||||
private WebsocketApi websocketApi;
|
||||
|
||||
@Resource
|
||||
private UserServiceApi userServiceApi;
|
||||
|
||||
|
@ -57,7 +62,6 @@ public class MessageDbServiceImpl implements MessageApi {
|
|||
if (MessageConstants.RECEIVE_ALL_USER_FLAG.equals(receiveUserIds)) {
|
||||
// 查询所有用户
|
||||
userIds = userServiceApi.queryAllUserIdList(new SysUserRequest());
|
||||
|
||||
} else {
|
||||
String[] userIdArr = receiveUserIds.split(",");
|
||||
userIds = Convert.toList(Long.class, userIdArr);
|
||||
|
@ -72,7 +76,6 @@ public class MessageDbServiceImpl implements MessageApi {
|
|||
// 初始化默认值
|
||||
sysMessage.setReadFlag(MessageReadFlagEnum.UNREAD.getCode());
|
||||
sysMessage.setSendUserId(loginUser.getUserId());
|
||||
sysMessage.setMessageSendTime(new Date());
|
||||
userIdSet.forEach(userId -> {
|
||||
// 判断用户是否存在
|
||||
if (userServiceApi.userExist(userId)) {
|
||||
|
@ -80,6 +83,8 @@ public class MessageDbServiceImpl implements MessageApi {
|
|||
sendMsgList.add(sysMessage);
|
||||
}
|
||||
});
|
||||
|
||||
websocketApi.sendWebSocketMessage(ListUtil.toList(userIdSet), messageSendParam);
|
||||
sysMessageService.saveBatch(sendMsgList);
|
||||
|
||||
}
|
||||
|
|
|
@ -29,6 +29,12 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>auth-api</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package cn.stylefeng.roses.kernel.message.websocket;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
|
||||
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
|
||||
import cn.stylefeng.roses.kernel.message.api.WebsocketApi;
|
||||
import cn.stylefeng.roses.kernel.message.api.enums.MessageReadFlagEnum;
|
||||
import cn.stylefeng.roses.kernel.message.api.pojo.MessageResponse;
|
||||
import cn.stylefeng.roses.kernel.message.api.pojo.MessageSendParam;
|
||||
import cn.stylefeng.roses.kernel.message.websocket.manager.WebSocketManager;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 系统消息websocket
|
||||
*
|
||||
* @author liuhanqing
|
||||
* @date 2021/1/2 22:00
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class WebSocketServiceImpl implements WebsocketApi {
|
||||
|
||||
|
||||
public final static ObjectMapper MAPPER;
|
||||
|
||||
static {
|
||||
MAPPER = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendWebSocketMessage(List<Long> userIdList, MessageSendParam messageSendParam) {
|
||||
// 获取当前登录人
|
||||
LoginUser loginUser = LoginContext.me().getLoginUser();
|
||||
try {
|
||||
MessageResponse sysMessage = new MessageResponse();
|
||||
BeanUtil.copyProperties(messageSendParam, sysMessage);
|
||||
sysMessage.setReadFlag(MessageReadFlagEnum.UNREAD.getCode());
|
||||
sysMessage.setSendUserId(loginUser.getUserId());
|
||||
String msgInfo = MAPPER.writeValueAsString(sysMessage);
|
||||
|
||||
for (Long userId : userIdList) {
|
||||
WebSocketManager.sendMessage(userId, msgInfo);
|
||||
}
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("发送websocket异常", e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -33,7 +33,7 @@ public class WebSocketEndpoint {
|
|||
// 添加到链接管理
|
||||
WebSocketManager.add(userId, session);
|
||||
// 返回消息
|
||||
session.getAsyncRemote().sendText("WebSocket连接成功");
|
||||
// session.getAsyncRemote().sendText("WebSocket连接成功");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -22,6 +22,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -56,6 +57,7 @@ public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice
|
|||
// 消息业务类型
|
||||
message.setBusinessType(MessageBusinessTypeEnum.SYS_NOTICE.getCode());
|
||||
message.setBusinessId(sysNotice.getNoticeId());
|
||||
message.setMessageSendTime(new Date());
|
||||
messageApi.sendMessage(message);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue