【message】消息模块增加websocket

pull/3/head
liuhanqing 2021-01-24 23:25:46 +08:00
parent 291607886b
commit 58d6f15cc4
7 changed files with 230 additions and 1 deletions

View File

@ -0,0 +1 @@
系统消息websocket的sdk用于将消息发送给在线用户并提供相应接口

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>kernel-s-message</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>message-sdk-websocket</artifactId>
<packaging>jar</packaging>
<dependencies>
<!--消息模块的api-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>message-api</artifactId>
<version>1.0.0</version>
</dependency>
<!--websocket 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,95 @@
package cn.stylefeng.roses.kernel.message.websocket.manager;
import org.springframework.util.CollectionUtils;
import javax.websocket.Session;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author liuhq
*/
public class WebSocketManager {
private static final ConcurrentHashMap<Long, List<Session>> userIdSessionMap = new ConcurrentHashMap<>();
/**
* IDSession
*
* @param userId id
* @param session websocketSession
* @author liuhanqing
* @date 2021/1/24 22:08
*/
public static void add(Long userId, Session session) {
userIdSessionMap.computeIfAbsent(userId, v -> new ArrayList<>()).add(session);
}
/**
* IDSession
*
* @param userId id
* @return List<Session> websocketSession
* @author liuhanqing
* @date 2021/1/24 22:10
*/
public static List<Session> getSessionByUserId(Long userId) {
return userIdSessionMap.get(userId);
}
/**
* Session
*
* @param userId id
* @param session websocketSession
* @author liuhanqing
* @date 2021/1/24 22:11
*/
public static void removeSession(Long userId, Session session) {
if (session == null) {
return;
}
List<Session> webSessoin = userIdSessionMap.get(userId);
if (webSessoin == null || CollectionUtils.isEmpty(webSessoin)) {
return;
}
webSessoin.remove(session);
}
/**
*
*
* @author liuhanqing
* @date 2021/1/24 22:11
*/
public static Set<Long> getUserList() {
return userIdSessionMap.keySet();
}
/**
*
*
* @param userId id
* @param message
* @author liuhanqing
* @date 2021/1/24 22:11
*/
public static void sendMessage(Long userId, String message){
for(Session userSession: getSessionByUserId(userId)){
userSession.getAsyncRemote().sendText(message);
}
}
/**
*
*
* @param message
* @author liuhanqing
* @date 2021/1/24 22:11
*/
public static void sendMessageToAll(String message){
for (Long userId : WebSocketManager.getUserList()) {
sendMessage(userId, message);
}
}
}

View File

@ -0,0 +1,78 @@
package cn.stylefeng.roses.kernel.message.websocket.server;
import cn.stylefeng.roses.kernel.message.websocket.manager.WebSocketManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
/**
* websocket
*
* @author liuhanqing
* @date 2021/1/24 22:26
*/
@Slf4j
@Component
@ServerEndpoint("/message/websocket/{userId}")
public class WebSocketEndpoint {
/**
*
*
* @param userId id
* @param session websocketSession
* @author liuhanqing
* @date 2021/1/24 22:27
*/
@OnOpen
public void onOpen(@PathParam(value = "userId") Long userId, Session session) {
// 添加到链接管理
WebSocketManager.add(userId, session);
// 返回消息
session.getAsyncRemote().sendText("WebSocket连接成功");
}
/**
*
*
* @author liuhanqing
* @date 2021/1/24 22:29
*/
@OnClose
public void onClose(@PathParam(value = "userId") Long userId, Session session) {
// 从map中删除
WebSocketManager.removeSession(userId, session);
}
/**
*
*
* @param message
* @param session websocketSession
* @author liuhanqing
* @date 2021/1/24 22:29
*/
@OnMessage
public void onMessage(String message, Session session) {
log.info("来自客户端的消息:" + message);
}
/**
*
*
* @param session
* @param error
* @author liuhanqing
* @date 2021/1/24 22:29
*/
@OnError
public void onError(Session session, Throwable error) {
log.error("WebSocket发生错误");
error.printStackTrace();
}
}

View File

@ -32,6 +32,11 @@
<version>1.0.0</version> <version>1.0.0</version>
</dependency> </dependency>
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>message-sdk-websocket</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies> </dependencies>

View File

@ -1,6 +1,9 @@
package cn.stylefeng.roses.kernel.message.starter; package cn.stylefeng.roses.kernel.message.starter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/** /**
* *
@ -11,6 +14,17 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
public class GunsMessageAutoConfiguration { public class GunsMessageAutoConfiguration {
/**
* WebSocket
*
* @return serverEndpointExporter
* @author liuhanqing
* @date 2021/01/24 22:09
*/
@Bean
@ConditionalOnMissingBean(ServerEndpointExporter.class)
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
} }

View File

@ -19,6 +19,7 @@
<module>message-api</module> <module>message-api</module>
<module>message-business</module> <module>message-business</module>
<module>message-sdk-db</module> <module>message-sdk-db</module>
<module>message-sdk-websocket</module>
<module>message-spring-boot-starter</module> <module>message-spring-boot-starter</module>
</modules> </modules>