集成websocket,先暂时可用,逻辑后续再改
This commit is contained in:
parent
ee1f6d9d4f
commit
7125e9c49d
7
pom.xml
7
pom.xml
@ -274,6 +274,12 @@
|
||||
<artifactId>ruoyi-minio</artifactId>
|
||||
<version>${ruoyi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-websocket</artifactId>
|
||||
<version>${ruoyi.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
@ -289,6 +295,7 @@
|
||||
<module>ruoyi-online</module>
|
||||
<module>ruoyi-mybatis-jpa</module>
|
||||
<module>ruoyi-middleware</module>
|
||||
<module>ruoyi-websocket</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
@ -81,6 +81,12 @@
|
||||
<artifactId>ruoyi-middleware-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- websocket -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- knife4j -->
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
|
@ -120,6 +120,7 @@ public class SecurityConfig {
|
||||
.requestMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs",
|
||||
"/druid/**", "/*/api-docs/**")
|
||||
.permitAll()
|
||||
.requestMatchers("/websocket/**").permitAll()
|
||||
// 除上面外的所有请求全部需要鉴权认证
|
||||
.anyRequest().authenticated();
|
||||
})
|
||||
|
31
ruoyi-websocket/pom.xml
Normal file
31
ruoyi-websocket/pom.xml
Normal file
@ -0,0 +1,31 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>ruoyi</artifactId>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<version>3.8.7.3.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ruoyi-websocket</artifactId>
|
||||
|
||||
<description>
|
||||
websocket系统模块
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot Websocket -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,59 @@
|
||||
package com.ruoyi.websocket;
|
||||
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 信号量相关处理
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class SemaphoreUtils
|
||||
{
|
||||
/**
|
||||
* SemaphoreUtils 日志控制器
|
||||
*/
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SemaphoreUtils.class);
|
||||
|
||||
/**
|
||||
* 获取信号量
|
||||
*
|
||||
* @param semaphore
|
||||
* @return
|
||||
*/
|
||||
public static boolean tryAcquire(Semaphore semaphore)
|
||||
{
|
||||
boolean flag = false;
|
||||
|
||||
try
|
||||
{
|
||||
flag = semaphore.tryAcquire();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.error("获取信号量异常", e);
|
||||
}
|
||||
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放信号量
|
||||
*
|
||||
* @param semaphore
|
||||
*/
|
||||
public static void release(Semaphore semaphore)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
semaphore.release();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.error("释放信号量异常", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.ruoyi.websocket;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
/**
|
||||
* websocket 配置
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSocketConfig
|
||||
{
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter()
|
||||
{
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
package com.ruoyi.websocket;
|
||||
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import jakarta.websocket.OnClose;
|
||||
import jakarta.websocket.OnError;
|
||||
import jakarta.websocket.OnMessage;
|
||||
import jakarta.websocket.OnOpen;
|
||||
import jakarta.websocket.Session;
|
||||
import jakarta.websocket.server.ServerEndpoint;
|
||||
|
||||
/**
|
||||
* websocket 消息处理
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Component
|
||||
@ServerEndpoint("/websocket/message")
|
||||
public class WebSocketServer
|
||||
{
|
||||
/**
|
||||
* WebSocketServer 日志控制器
|
||||
*/
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketServer.class);
|
||||
|
||||
/**
|
||||
* 默认最多允许同时在线人数100
|
||||
*/
|
||||
public static int socketMaxOnlineCount = 100;
|
||||
|
||||
private static Semaphore socketSemaphore = new Semaphore(socketMaxOnlineCount);
|
||||
|
||||
/**
|
||||
* 连接建立成功调用的方法
|
||||
*/
|
||||
@OnOpen
|
||||
public void onOpen(Session session) throws Exception
|
||||
{
|
||||
boolean semaphoreFlag = false;
|
||||
// 尝试获取信号量
|
||||
semaphoreFlag = SemaphoreUtils.tryAcquire(socketSemaphore);
|
||||
if (!semaphoreFlag)
|
||||
{
|
||||
// 未获取到信号量
|
||||
LOGGER.error("\n 当前在线人数超过限制数- {}", socketMaxOnlineCount);
|
||||
WebSocketUsers.sendMessageToUserByText(session, "当前在线人数超过限制数:" + socketMaxOnlineCount);
|
||||
session.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 添加用户
|
||||
WebSocketUsers.put(session.getId(), session);
|
||||
LOGGER.info("\n 建立连接 - {}", session);
|
||||
LOGGER.info("\n 当前人数 - {}", WebSocketUsers.getUsers().size());
|
||||
WebSocketUsers.sendMessageToUserByText(session, "连接成功");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接关闭时处理
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose(Session session)
|
||||
{
|
||||
LOGGER.info("\n 关闭连接 - {}", session);
|
||||
// 移除用户
|
||||
WebSocketUsers.remove(session.getId());
|
||||
// 获取到信号量则需释放
|
||||
SemaphoreUtils.release(socketSemaphore);
|
||||
}
|
||||
|
||||
/**
|
||||
* 抛出异常时处理
|
||||
*/
|
||||
@OnError
|
||||
public void onError(Session session, Throwable exception) throws Exception
|
||||
{
|
||||
if (session.isOpen())
|
||||
{
|
||||
// 关闭连接
|
||||
session.close();
|
||||
}
|
||||
String sessionId = session.getId();
|
||||
LOGGER.info("\n 连接异常 - {}", sessionId);
|
||||
LOGGER.info("\n 异常信息 - {}", exception);
|
||||
// 移出用户
|
||||
WebSocketUsers.remove(sessionId);
|
||||
// 获取到信号量则需释放
|
||||
SemaphoreUtils.release(socketSemaphore);
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务器接收到客户端消息时调用的方法
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(String message, Session session)
|
||||
{
|
||||
String msg = message.replace("你", "我").replace("吗", "");
|
||||
WebSocketUsers.sendMessageToUserByText(session, msg);
|
||||
}
|
||||
}
|
@ -0,0 +1,142 @@
|
||||
package com.ruoyi.websocket;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.websocket.Session;
|
||||
|
||||
/**
|
||||
* websocket 客户端用户集
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class WebSocketUsers
|
||||
{
|
||||
/**
|
||||
* WebSocketUsers 日志控制器
|
||||
*/
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketUsers.class);
|
||||
|
||||
/**
|
||||
* 用户集
|
||||
*/
|
||||
private static Map<String, Session> USERS = new ConcurrentHashMap<String, Session>();
|
||||
|
||||
/**
|
||||
* 存储用户
|
||||
*
|
||||
* @param key 唯一键
|
||||
* @param session 用户信息
|
||||
*/
|
||||
public static void put(String key, Session session)
|
||||
{
|
||||
USERS.put(key, session);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除用户
|
||||
*
|
||||
* @param session 用户信息
|
||||
*
|
||||
* @return 移除结果
|
||||
*/
|
||||
public static boolean remove(Session session)
|
||||
{
|
||||
String key = null;
|
||||
boolean flag = USERS.containsValue(session);
|
||||
if (flag)
|
||||
{
|
||||
Set<Map.Entry<String, Session>> entries = USERS.entrySet();
|
||||
for (Map.Entry<String, Session> entry : entries)
|
||||
{
|
||||
Session value = entry.getValue();
|
||||
if (value.equals(session))
|
||||
{
|
||||
key = entry.getKey();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移出用户
|
||||
*
|
||||
* @param key 键
|
||||
*/
|
||||
public static boolean remove(String key)
|
||||
{
|
||||
LOGGER.info("\n 正在移出用户 - {}", key);
|
||||
Session remove = USERS.remove(key);
|
||||
if (remove != null)
|
||||
{
|
||||
boolean containsValue = USERS.containsValue(remove);
|
||||
LOGGER.info("\n 移出结果 - {}", containsValue ? "失败" : "成功");
|
||||
return containsValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取在线用户列表
|
||||
*
|
||||
* @return 返回用户集合
|
||||
*/
|
||||
public static Map<String, Session> getUsers()
|
||||
{
|
||||
return USERS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 群发消息文本消息
|
||||
*
|
||||
* @param message 消息内容
|
||||
*/
|
||||
public static void sendMessageToUsersByText(String message)
|
||||
{
|
||||
Collection<Session> values = USERS.values();
|
||||
for (Session value : values)
|
||||
{
|
||||
sendMessageToUserByText(value, message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送文本消息
|
||||
*
|
||||
* @param userName 自己的用户名
|
||||
* @param message 消息内容
|
||||
*/
|
||||
public static void sendMessageToUserByText(Session session, String message)
|
||||
{
|
||||
if (session != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
session.getBasicRemote().sendText(message);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
LOGGER.error("\n[发送消息异常]", e);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGGER.info("\n[你已离线]");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user