controller 由其他微服务使用feign调用接口,实现后端主动推送
@RestController
@RequestMapping("/notify")
public class NotifyController {
/**
* 获取指定房间的数据
* @param jsonObject
* @return
*/
@RequestMapping(value = "/getRoomSize",method = RequestMethod.POST)
public int getRoomSize(@RequestBody JSONObject jsonObject){
Long companyId = jsonObject.getLong("companyId");
Long systemId = jsonObject.getLong("companySystemId");
String roomType = jsonObject.getString("roomType");
WebSocketMessage webSocketMessage = new WebSocketMessage();
webSocketMessage.setCompanyId(companyId);
webSocketMessage.setCompanySystemId(systemId);
webSocketMessage.setRoomType(roomType);
CompanyWebSocketRoom instance = CompanyWebSocketRoom.getInstance();
int roomSize = instance.getRoomSize(webSocketMessage);
return roomSize;
}
@RequestMapping(value = "/notifyWebByUserId",method = RequestMethod.POST)
public void notifyByUserId(@RequestBody JSONObject jsonObject){
Long companyId = jsonObject.getLong("companyId");
Long systemId = jsonObject.getLong("companySystemId");
Long userId = jsonObject.getLong("userId");
String roomType = jsonObject.getString("roomType");
JSONObject message = jsonObject.getJSONObject("message");
CompanyWebSocketRoom instance = CompanyWebSocketRoom.getInstance();
WebSocketMessage webSocketMessage = new WebSocketMessage();
webSocketMessage.setCompanyId(companyId);
webSocketMessage.setCompanySystemId(systemId);
webSocketMessage.setUserId(userId);
webSocketMessage.setRoomType(roomType);
List<WebSocketRoomEntity> webSocketRoomEntitys = instance.getWebSocketRoomEntitys(webSocketMessage);
if (webSocketRoomEntitys.size() > 0){
for (int i = 0 ; i < webSocketRoomEntitys.size() ; i ++){
WebSocketRoomEntity webSocketRoomEntity = webSocketRoomEntitys.get(i);
Session session = webSocketRoomEntity.getSession();
if (session.isOpen()){
session.getAsyncRemote().sendText(JSON.toJSONString(new ReturnMessage(MessageCode.NORMAL, message.toJSONString())));
}
}
}
}
@RequestMapping(value = "/notifyWebByUserIds",method = RequestMethod.POST)
public void notifyWebByUserIds(@RequestBody JSONObject jsonObject){
CompanyWebSocketRoom instance = CompanyWebSocketRoom.getInstance();
Long companyId = jsonObject.getLong("companyId");
Long systemId = jsonObject.getLong("companySystemId");
JSONArray userIds = jsonObject.getJSONArray("userIds");
JSONObject message = jsonObject.getJSONObject("message");
String roomType = jsonObject.getString("roomType");
WebSocketMessage webSocketMessage = new WebSocketMessage();
webSocketMessage.setCompanyId(companyId);
webSocketMessage.setCompanySystemId(systemId);
webSocketMessage.setRoomType(roomType);
webSocketMessage.setRoomType(roomType);
for (int i = 0 ; i < userIds.size() ; i ++){
Integer userId = userIds.getInteger(i);
webSocketMessage.setUserId(userId);
List<WebSocketRoomEntity> webSocketRoomEntitys = instance.getWebSocketRoomEntitys(webSocketMessage);
if (webSocketRoomEntitys.size() > 0){
for (int j = 0 ; i < webSocketRoomEntitys.size() ; j ++){
WebSocketRoomEntity webSocketRoomEntity = webSocketRoomEntitys.get(j);
Session session = webSocketRoomEntity.getSession();
if (session.isOpen()){
session.getAsyncRemote().sendText(JSON.toJSONString(new ReturnMessage(MessageCode.NORMAL, message.toJSONString())));
}
}
}
}
}
@RequestMapping(value = "/notifyWebByCompanySystemId",method = RequestMethod.POST)
public void notifyWebByCompanySystemId(@RequestBody JSONObject jsonObject){
Long companyId = jsonObject.getLong("companyId");
Long systemId = jsonObject.getLong("companySystemId");
JSONObject message = jsonObject.getJSONObject("message");
String roomType = jsonObject.getString("roomType");
CompanyWebSocketRoom instance = CompanyWebSocketRoom.getInstance();
WebSocketMessage webSocketMessage = new WebSocketMessage();
webSocketMessage.setCompanyId(companyId);
webSocketMessage.setCompanySystemId(systemId);
webSocketMessage.setRoomType(roomType);
List<WebSocketRoomEntity> webSocketRoomEntitys = instance.getWebSocketRoomEntitys(webSocketMessage);
if (webSocketRoomEntitys.size() > 0){
for (int i = 0 ; i < webSocketRoomEntitys.size() ; i ++){
WebSocketRoomEntity webSocketRoomEntity = webSocketRoomEntitys.get(i);
Session session = webSocketRoomEntity.getSession();
if (session.isOpen()){
session.getAsyncRemote().sendText(JSON.toJSONString(new ReturnMessage(MessageCode.NORMAL, message.toJSONString())));
}
}
}
}
}
至此,相关所有已经开发完成 ,后面笔者会将整体代码上传,有需要的小伙伴可以下载,但最主要代码已经全部在本次所有文章中。
public class WebSocketRoomEntity {
/**
* sesstion
*/
private Session session;
/**
* 系统标识
*/
private Long companySystemId;
/**
* 公司标识
*/
private Long companyId;
/**
* 用户标识
*/
private long userId;
/**
* @param session
*/
private String roomType;
/**
* 信息类型 注册消息,队列消息,主题消息
*/
private String messageType;
/**
* 退出登录时,是否将连接断开 1 强制退出,前端弹出消息 2 不关闭连接,仅清除后端list 数据 3 强制退出,前端不弹出消息
*/
private String flag;
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
public Long getCompanySystemId() {
return companySystemId;
}
public void setCompanySystemId(Long companySystemId) {
this.companySystemId = companySystemId;
}
public Long getCompanyId() {
return companyId;
}
public void setCompanyId(Long companyId) {
this.companyId = companyId;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getRoomType() {
return roomType;
}
public void setRoomType(String roomType) {
this.roomType = roomType;
}
public String getMessageType() {
return messageType;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WebSocketRoomEntity that = (WebSocketRoomEntity) o;
return userId == that.userId && Objects.equals(session, that.session) && Objects.equals(companySystemId, that.companySystemId) && Objects.equals(companyId, that.companyId) && Objects.equals(roomType, that.roomType) && Objects.equals(messageType, that.messageType) && Objects.equals(flag, that.flag);
}
@Override
public int hashCode() {
return Objects.hash(session, companySystemId, companyId, userId, roomType, messageType, flag);
}
@Override
public String toString() {
return "WebSocketRoomEntity{" + "session=" + session + ", companySystemId=" + companySystemId + ", companyId=" + companyId + ", userId=" + userId + '\'' + ", roomType='" + roomType + '\'' + ", messageType='" + messageType + '\'' + ", flag='" + flag + '\'' + '}';
}
}
下载地址如下:
https://download.csdn.net/download/qq_40022312/19403714