From 125aec00a994dbe195ef69f87788a1601cc4122d Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Tue, 14 Mar 2023 13:45:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4WebSocket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/WebSocketSecurityConfig.java | 25 ---- .../config/WebSocketBrokerConfig.java | 25 ---- .../websocket/config/WebSocketConfig.java | 16 --- .../websocket/controller/TestController.java | 18 --- .../websocket/WebSocketController.java | 24 ---- .../websocketbroker/ViewController.java | 17 --- .../WebSocketBrokerController.java | 21 --- .../websocket/service/WebSocketServer.java | 128 ------------------ .../websocket/ClientServerMessage.java | 18 --- .../websocket/ServerClientMessage.java | 21 --- 10 files changed, 313 deletions(-) delete mode 100644 src/main/java/com/xkrs/common/config/WebSocketSecurityConfig.java delete mode 100644 src/main/java/com/xkrs/websocket/config/WebSocketBrokerConfig.java delete mode 100644 src/main/java/com/xkrs/websocket/config/WebSocketConfig.java delete mode 100644 src/main/java/com/xkrs/websocket/controller/TestController.java delete mode 100644 src/main/java/com/xkrs/websocket/controller/websocket/WebSocketController.java delete mode 100644 src/main/java/com/xkrs/websocket/controller/websocketbroker/ViewController.java delete mode 100644 src/main/java/com/xkrs/websocket/controller/websocketbroker/WebSocketBrokerController.java delete mode 100644 src/main/java/com/xkrs/websocket/service/WebSocketServer.java delete mode 100644 src/main/java/com/xkrs/websocket/websocket/ClientServerMessage.java delete mode 100644 src/main/java/com/xkrs/websocket/websocket/ServerClientMessage.java diff --git a/src/main/java/com/xkrs/common/config/WebSocketSecurityConfig.java b/src/main/java/com/xkrs/common/config/WebSocketSecurityConfig.java deleted file mode 100644 index 5ea9f85..0000000 --- a/src/main/java/com/xkrs/common/config/WebSocketSecurityConfig.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.xkrs.common.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.security.config.annotation.web.messaging.MessageSecurityMetadataSourceRegistry; -import org.springframework.security.config.annotation.web.socket.AbstractSecurityWebSocketMessageBrokerConfigurer; - -import static org.springframework.messaging.simp.SimpMessageType.MESSAGE; -import static org.springframework.messaging.simp.SimpMessageType.SUBSCRIBE; - - -/** - * @author xkrs - */ -@Configuration -public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer { - @Override - protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) { - messages - .nullDestMatcher().authenticated() - .simpSubscribeDestMatchers("/user/queue/errors").permitAll() - .simpTypeMatchers(MESSAGE, SUBSCRIBE).denyAll() - .anyMessage().denyAll(); - - } -} \ No newline at end of file diff --git a/src/main/java/com/xkrs/websocket/config/WebSocketBrokerConfig.java b/src/main/java/com/xkrs/websocket/config/WebSocketBrokerConfig.java deleted file mode 100644 index bb8d377..0000000 --- a/src/main/java/com/xkrs/websocket/config/WebSocketBrokerConfig.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.xkrs.websocket.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.messaging.simp.config.MessageBrokerRegistry; -import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; -import org.springframework.web.socket.config.annotation.StompEndpointRegistry; -import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; - -/** - * websocket广播配置 - * @author xkrs - */ -@Configuration -@EnableWebSocketMessageBroker -public class WebSocketBrokerConfig implements WebSocketMessageBrokerConfigurer { - @Override - public void registerStompEndpoints(StompEndpointRegistry registry) { - registry.addEndpoint("/endPointClientServer").withSockJS(); - } - - @Override - public void configureMessageBroker(MessageBrokerRegistry registry) { - registry.enableSimpleBroker("/ws"); - } -} diff --git a/src/main/java/com/xkrs/websocket/config/WebSocketConfig.java b/src/main/java/com/xkrs/websocket/config/WebSocketConfig.java deleted file mode 100644 index 261844d..0000000 --- a/src/main/java/com/xkrs/websocket/config/WebSocketConfig.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.xkrs.websocket.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.socket.server.standard.ServerEndpointExporter; - -/** - * @author xkrs - */ -@Configuration -public class WebSocketConfig { - @Bean - public ServerEndpointExporter serverEndpointExporter(){ - return new ServerEndpointExporter(); - } -} diff --git a/src/main/java/com/xkrs/websocket/controller/TestController.java b/src/main/java/com/xkrs/websocket/controller/TestController.java deleted file mode 100644 index aad8385..0000000 --- a/src/main/java/com/xkrs/websocket/controller/TestController.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.xkrs.websocket.controller; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - * @author xkrs - */ -@RestController -@RequestMapping("/test") -public class TestController { - - @GetMapping("/hello") - public String sayHello(){ - return "asdasd"; - } -} diff --git a/src/main/java/com/xkrs/websocket/controller/websocket/WebSocketController.java b/src/main/java/com/xkrs/websocket/controller/websocket/WebSocketController.java deleted file mode 100644 index d9e689a..0000000 --- a/src/main/java/com/xkrs/websocket/controller/websocket/WebSocketController.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.xkrs.websocket.controller.websocket; - -import com.xkrs.websocket.service.WebSocketServer; -import org.springframework.web.bind.annotation.*; - -/** - * @author xkrs - */ -@RestController -@RequestMapping("/api/socket") -public class WebSocketController { - - @GetMapping("/sendAll") - public String sendAll(@RequestParam String msg) { - WebSocketServer.broadInfo(msg); - return "success"; - } - - @RequestMapping(value = "/sendOne", method = RequestMethod.GET) - public String sendOne(@RequestParam String msg, @RequestParam String session) { - WebSocketServer.sendMsg(msg, session); - return "success"; - } -} diff --git a/src/main/java/com/xkrs/websocket/controller/websocketbroker/ViewController.java b/src/main/java/com/xkrs/websocket/controller/websocketbroker/ViewController.java deleted file mode 100644 index 4bd39b1..0000000 --- a/src/main/java/com/xkrs/websocket/controller/websocketbroker/ViewController.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.xkrs.websocket.controller.websocketbroker; - -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - -/** - * @author xkrs - */ -@Configuration -public class ViewController extends WebMvcConfigurerAdapter { - - @Override - public void addViewControllers(ViewControllerRegistry registry) { - registry.addViewController("/ws").setViewName("/ws"); - } -} diff --git a/src/main/java/com/xkrs/websocket/controller/websocketbroker/WebSocketBrokerController.java b/src/main/java/com/xkrs/websocket/controller/websocketbroker/WebSocketBrokerController.java deleted file mode 100644 index 9700d44..0000000 --- a/src/main/java/com/xkrs/websocket/controller/websocketbroker/WebSocketBrokerController.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.xkrs.websocket.controller.websocketbroker; - -import com.xkrs.websocket.websocket.ClientServerMessage; -import com.xkrs.websocket.websocket.ServerClientMessage; -import org.springframework.messaging.handler.annotation.MessageMapping; -import org.springframework.messaging.handler.annotation.SendTo; -import org.springframework.stereotype.Controller; - -/** - * @author xkrs - */ -@Controller -public class WebSocketBrokerController { - - @MessageMapping("/hello") - @SendTo("/ws/getResponse") - public ServerClientMessage say(ClientServerMessage message) throws InterruptedException { - Thread.sleep(3000); - return new ServerClientMessage("hello," + message.getName() + "!"); - } -} diff --git a/src/main/java/com/xkrs/websocket/service/WebSocketServer.java b/src/main/java/com/xkrs/websocket/service/WebSocketServer.java deleted file mode 100644 index c8a3685..0000000 --- a/src/main/java/com/xkrs/websocket/service/WebSocketServer.java +++ /dev/null @@ -1,128 +0,0 @@ -package com.xkrs.websocket.service; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import javax.websocket.*; -import javax.websocket.server.ServerEndpoint; -import java.io.IOException; -import java.util.Map; -import java.util.concurrent.CopyOnWriteArraySet; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * @author xkrs - */ -@Component -@ServerEndpoint(value = "/ws/asset") -public class WebSocketServer { - - @PostConstruct - public void init() { - System.out.println("websocketBroker loading......"); - } - - private static Logger log = LoggerFactory.getLogger(WebSocketServer.class); - private static final AtomicInteger ONLINE_COUNT = new AtomicInteger(0); - private static CopyOnWriteArraySet sessionSet = new CopyOnWriteArraySet<>(); - - /** - * 连接建立 - */ - @OnOpen - public void onOpen(Session session) { - sessionSet.add(session); - //在线加1 - int cnt = ONLINE_COUNT.incrementAndGet(); - //log.info("have a new connect:{}", cnt); - //sendMsg(session, "connect success"); - } - - /** - * 连接关闭 - * - * @param session - */ - @OnClose - public void onClose(Session session) { - sessionSet.remove(session); - //在线减1 - int cnt = ONLINE_COUNT.decrementAndGet(); - log.info("have a connect close:{}", cnt); - } - - /** - * 收到客户端消息后调用的方法 - * - * @param session - * @param msg - */ - @OnMessage - public void onMessage(String msg, Session session) { - log.info("receive the msg:{}", msg); - } - - private static void sendMsg(Session session, String msg) { - try { - session.getBasicRemote().sendText(String.format(msg)); - } catch (IOException e) { - log.error("发送信息出错:{}", e.getMessage()); - e.printStackTrace(); - } - } - - private static void sendDate(Session session, Map map) { - try { - session.getBasicRemote().sendObject(map); - } catch (IOException | EncodeException e) { - log.error("发送信息出错:{}", e.getMessage()); - e.printStackTrace(); - } - } - - /** - * 指定session发送 - */ - public static void sendMsg(String msg, String sessionId) { - Session session = null; - for (Session s : sessionSet) { - if (s.getId().equals(sessionId)) { - session = s; - break; - } - } - if (session != null) { - sendMsg(session, msg); - } else { - log.warn("not find your session"); - } - } - - /** - * 群发消息 - */ - public static void broadInfo(String msg) { - for (Session s : sessionSet) { - synchronized (sessionSet){ - if (s.isOpen()) { - sendMsg(s, msg); - } - } - } - } - - /** - * 群发map消息 - * @param map - */ - public static void broadInfoMap(Map map) { - for (Session s : sessionSet) { - if (s.isOpen()) { - sendDate(s, map); - } - } - } - -} diff --git a/src/main/java/com/xkrs/websocket/websocket/ClientServerMessage.java b/src/main/java/com/xkrs/websocket/websocket/ClientServerMessage.java deleted file mode 100644 index 11c054e..0000000 --- a/src/main/java/com/xkrs/websocket/websocket/ClientServerMessage.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.xkrs.websocket.websocket; - -/** - * @author xkrs - */ -public class ClientServerMessage { - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - private String name; - -} diff --git a/src/main/java/com/xkrs/websocket/websocket/ServerClientMessage.java b/src/main/java/com/xkrs/websocket/websocket/ServerClientMessage.java deleted file mode 100644 index b803856..0000000 --- a/src/main/java/com/xkrs/websocket/websocket/ServerClientMessage.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.xkrs.websocket.websocket; - -/** - * @author xkrs - */ -public class ServerClientMessage { - - public String getResponseMsg() { - return responseMsg; - } - - public void setResponseMsg(String responseMsg) { - this.responseMsg = responseMsg; - } - - private String responseMsg; - - public ServerClientMessage(String responseMsg) { - this.responseMsg = responseMsg; - } -}