From 6d792317dd2b084535d00d3c0257a081e493e57b Mon Sep 17 00:00:00 2001
From: weihongyang <1075331873@qq.com>
Date: Wed, 29 Jun 2022 16:29:25 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20init=20websocket=E6=A8=A1=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
ym-websocket/pom.xml | 32 ++++++++
.../websocket/config/WebSocketConfig.java | 20 +++++
.../websocket/server/WebSocketServer.java | 79 +++++++++++++++++++
.../cnbm/websocket/task/SendMessageTask.java | 37 +++++++++
4 files changed, 168 insertions(+)
create mode 100644 ym-websocket/pom.xml
create mode 100644 ym-websocket/src/main/java/com/cnbm/websocket/config/WebSocketConfig.java
create mode 100644 ym-websocket/src/main/java/com/cnbm/websocket/server/WebSocketServer.java
create mode 100644 ym-websocket/src/main/java/com/cnbm/websocket/task/SendMessageTask.java
diff --git a/ym-websocket/pom.xml b/ym-websocket/pom.xml
new file mode 100644
index 0000000..0f30458
--- /dev/null
+++ b/ym-websocket/pom.xml
@@ -0,0 +1,32 @@
+
+
+
+ ym-pass
+ com.cnbm
+ 1.0-SNAPSHOT
+
+ 4.0.0
+
+ ym-websocket
+
+
+ 8
+ 8
+
+
+
+
+
+ com.cnbm
+ ym-schedule-task
+ 1.0-SNAPSHOT
+
+
+ org.springframework.boot
+ spring-boot-starter-websocket
+
+
+
+
\ No newline at end of file
diff --git a/ym-websocket/src/main/java/com/cnbm/websocket/config/WebSocketConfig.java b/ym-websocket/src/main/java/com/cnbm/websocket/config/WebSocketConfig.java
new file mode 100644
index 0000000..8180af6
--- /dev/null
+++ b/ym-websocket/src/main/java/com/cnbm/websocket/config/WebSocketConfig.java
@@ -0,0 +1,20 @@
+package com.cnbm.websocket.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.socket.config.annotation.EnableWebSocket;
+import org.springframework.web.socket.server.standard.ServerEndpointExporter;
+
+/**
+ * @Author weihongyang
+ * @Date 2022/6/29 8:58 AM
+ * @Version 1.0
+ */
+@Configuration
+@EnableWebSocket
+public class WebSocketConfig {
+ @Bean
+ public ServerEndpointExporter serverEndpoint(){
+ return new ServerEndpointExporter();
+ }
+}
diff --git a/ym-websocket/src/main/java/com/cnbm/websocket/server/WebSocketServer.java b/ym-websocket/src/main/java/com/cnbm/websocket/server/WebSocketServer.java
new file mode 100644
index 0000000..3f381db
--- /dev/null
+++ b/ym-websocket/src/main/java/com/cnbm/websocket/server/WebSocketServer.java
@@ -0,0 +1,79 @@
+package com.cnbm.websocket.server;
+
+import lombok.extern.log4j.Log4j2;
+import org.springframework.stereotype.Component;
+
+import javax.websocket.OnClose;
+import javax.websocket.OnMessage;
+import javax.websocket.OnOpen;
+import javax.websocket.Session;
+import javax.websocket.server.ServerEndpoint;
+import java.io.IOException;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @Author weihongyang
+ * @Date 2022/6/29 8:59 AM
+ * @Version 1.0
+ */
+@Log4j2
+@Component
+@ServerEndpoint("/websocket/info") // 指定websocket 连接的url
+public class WebSocketServer {
+
+ private static int onlineCount = 0;
+
+ public static ConcurrentHashMap webSocketMap = new ConcurrentHashMap<>();
+
+ private Session session;
+
+ private String sessionId;
+
+ @OnOpen
+ public void onOpen(Session session) {
+ log.info("客户端:{}连接成功",session.getId());
+ this.session = session;
+ this.sessionId = session.getId();
+ if (webSocketMap.containsKey(session.getId())){
+ webSocketMap.remove(sessionId);
+ webSocketMap.put(sessionId,this);
+ }else {
+ webSocketMap.put(sessionId,this);
+ addOnlineCount();
+ }
+ }
+
+ @OnClose
+ public void onClose(Session session) {
+ if (webSocketMap.containsKey(sessionId)) {
+ webSocketMap.remove(sessionId);
+ subOnlineCount();
+ }
+ log.info("客户端:{}连接断开",session.getId());
+ }
+
+ @OnMessage
+ public String onMsg(String message,Session session) {
+ log.info("从客户端:{} 收到<--:{}", session.getId(),message);
+
+ String send=message.toUpperCase();
+ String result="客户:%s您好,来自server 的消息:%s";
+ result = String.format(result, session.getId(), send);
+ return "来自server 的消息:" + result;
+ }
+
+ public void sendMsg(String message) throws IOException{
+ this.session.getBasicRemote().sendText(message);
+ }
+
+ private static synchronized void addOnlineCount() {
+ WebSocketServer.onlineCount++;
+ }
+
+ private static synchronized void subOnlineCount() {
+ WebSocketServer.onlineCount--;
+ }
+
+
+}
+
diff --git a/ym-websocket/src/main/java/com/cnbm/websocket/task/SendMessageTask.java b/ym-websocket/src/main/java/com/cnbm/websocket/task/SendMessageTask.java
new file mode 100644
index 0000000..0601de2
--- /dev/null
+++ b/ym-websocket/src/main/java/com/cnbm/websocket/task/SendMessageTask.java
@@ -0,0 +1,37 @@
+package com.cnbm.websocket.task;
+
+import com.cnbm.scheduletask.task.ITask;
+import com.cnbm.websocket.server.WebSocketServer;
+import lombok.extern.log4j.Log4j2;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @Author weihongyang
+ * @Date 2022/6/29 2:01 PM
+ * @Version 1.0
+ */
+@Log4j2
+@Component("sendMessageTask")
+public class SendMessageTask implements ITask {
+ public void run(String params) {
+
+ Iterator> socketIt = WebSocketServer.webSocketMap.entrySet().iterator();
+
+
+ while (socketIt.hasNext()) {
+ Map.Entry socketServerEntry = socketIt.next();
+ try {
+ socketServerEntry.getValue().sendMsg("定时发送:"+new Date().toString());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ log.info("sendMessageTask定时任务正在执行,参数为:{}", params);
+ }
+}