values = USERS.values();
+ for (Session value : values) {
+ sendMessageToUserByText(value, message);
+ }
+ }
+
+ /**
+ * 发送文本消息
+ *
+ * @param session 自己的用户
+ * @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[你已离线]");
+ }
+ }
+
+}
diff --git a/src/main/java/com/xkrs/websocket/pay/PaySemaphoreUtils.java b/src/main/java/com/xkrs/websocket/pay/PaySemaphoreUtils.java
new file mode 100644
index 0000000..8e7e9d6
--- /dev/null
+++ b/src/main/java/com/xkrs/websocket/pay/PaySemaphoreUtils.java
@@ -0,0 +1,49 @@
+package com.xkrs.websocket.pay;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.Semaphore;
+
+/**
+ * 信号量相关处理
+ *
+ * @author ruoyi
+ */
+public class PaySemaphoreUtils {
+
+ /**
+ * SemaphoreUtils 日志控制器
+ */
+ private static final Logger LOGGER = LoggerFactory.getLogger(PaySemaphoreUtils.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);
+ }
+ }
+
+}
diff --git a/src/main/java/com/xkrs/websocket/pay/PayWebSocketServer.java b/src/main/java/com/xkrs/websocket/pay/PayWebSocketServer.java
new file mode 100644
index 0000000..876a25e
--- /dev/null
+++ b/src/main/java/com/xkrs/websocket/pay/PayWebSocketServer.java
@@ -0,0 +1,95 @@
+package com.xkrs.websocket.pay;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import javax.websocket.*;
+import javax.websocket.server.ServerEndpoint;
+import java.util.concurrent.Semaphore;
+
+/**
+ * websocket 消息处理
+ *
+ * @author ruoyi
+ *
+ * ws://8.142.26.238:7801/websocket/pay
+ */
+@Component
+@ServerEndpoint("/websocket/pay")
+public class PayWebSocketServer {
+
+ /**
+ * WebSocketServer 日志控制器
+ */
+ private static final Logger LOGGER = LoggerFactory.getLogger(PayWebSocketServer.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 = PaySemaphoreUtils.tryAcquire(socketSemaphore);
+ if (!semaphoreFlag) {
+ // 未获取到信号量
+ LOGGER.error("\n 当前在线人数超过限制数- {}", socketMaxOnlineCount);
+ PayWebSocketUsers.sendMessageToUserByText(session, "当前在线人数超过限制数:" + socketMaxOnlineCount);
+ session.close();
+ } else {
+ // 添加用户
+ PayWebSocketUsers.put(session.getId(), session);
+ LOGGER.info("\n 建立连接 - {}", session);
+ LOGGER.info("\n 当前人数 - {}", PayWebSocketUsers.getUsers().size());
+ PayWebSocketUsers.sendMessageToUserByText(session, "连接成功");
+ }
+ }
+
+ /**
+ * 连接关闭时处理
+ */
+ @OnClose
+ public void onClose(Session session) {
+ LOGGER.info("\n 关闭连接 - {}", session);
+ // 移除用户
+ PayWebSocketUsers.remove(session.getId());
+ // 获取到信号量则需释放
+ PaySemaphoreUtils.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);
+ // 移出用户
+ PayWebSocketUsers.remove(sessionId);
+ // 获取到信号量则需释放
+ PaySemaphoreUtils.release(socketSemaphore);
+ }
+
+ /**
+ * 服务器接收到客户端消息时调用的方法
+ */
+ @OnMessage
+ public void onMessage(String message, Session session) {
+ String msg = message.replace("你", "我").replace("吗", "");
+ PayWebSocketUsers.sendMessageToUserByText(session, msg);
+ }
+
+}
diff --git a/src/main/java/com/xkrs/websocket/pay/PayWebSocketUsers.java b/src/main/java/com/xkrs/websocket/pay/PayWebSocketUsers.java
new file mode 100644
index 0000000..4c47850
--- /dev/null
+++ b/src/main/java/com/xkrs/websocket/pay/PayWebSocketUsers.java
@@ -0,0 +1,120 @@
+package com.xkrs.websocket.pay;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.websocket.Session;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * websocket 客户端用户集
+ *
+ * @author ruoyi
+ */
+public class PayWebSocketUsers {
+
+ /**
+ * WebSocketUsers 日志控制器
+ */
+ private static final Logger LOGGER = LoggerFactory.getLogger(PayWebSocketUsers.class);
+
+ /**
+ * 用户集
+ */
+ private static Map USERS = new ConcurrentHashMap();
+
+ /**
+ * 存储用户
+ *
+ * @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> entries = USERS.entrySet();
+ for (Map.Entry 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 getUsers() {
+ return USERS;
+ }
+
+ /**
+ * 群发消息文本消息
+ *
+ * @param message 消息内容
+ */
+ public static void sendMessageToUsersByText(String message) {
+ Collection values = USERS.values();
+ for (Session value : values) {
+ sendMessageToUserByText(value, message);
+ }
+ }
+
+ /**
+ * 发送文本消息
+ *
+ * @param session 自己的用户
+ * @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[你已离线]");
+ }
+ }
+
+}