feat: init websocket模块

This commit is contained in:
weihongyang 2022-06-29 16:29:25 +08:00
parent 8d6cda8f90
commit 6d792317dd
4 changed files with 168 additions and 0 deletions

32
ym-websocket/pom.xml Normal file
View File

@ -0,0 +1,32 @@
<?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>ym-pass</artifactId>
<groupId>com.cnbm</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ym-websocket</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.cnbm</groupId>
<artifactId>ym-schedule-task</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -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();
}
}

View File

@ -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<String,WebSocketServer> 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--;
}
}

View File

@ -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<Map.Entry<String, WebSocketServer>> socketIt = WebSocketServer.webSocketMap.entrySet().iterator();
while (socketIt.hasNext()) {
Map.Entry<String, WebSocketServer> socketServerEntry = socketIt.next();
try {
socketServerEntry.getValue().sendMsg("定时发送:"+new Date().toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
log.info("sendMessageTask定时任务正在执行参数为{}", params);
}
}