From 9e09eeb592e40df2ae51ed8c125405153bb61061 Mon Sep 17 00:00:00 2001 From: guolanren Date: Sat, 27 Jun 2020 08:12:17 +0800 Subject: [PATCH] =?UTF-8?q?web-socket=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web-socket/README.md | 5 -- web-socket/web-socket-handler/README.md | 5 ++ web-socket/{ => web-socket-handler}/pom.xml | 4 +- .../WebSocketHandlerApplication.java | 15 ++++ .../MyHttpSessionHandshakeInterceptor.java | 24 +++++++ .../config/WebSocketHandlerConfiguration.java | 38 +++++++++++ .../controller/ChattingController.java | 67 ++++++++++++++++++ web-socket/web-socket-javax/README.md | 4 ++ web-socket/web-socket-javax/pom.xml | 68 +++++++++++++++++++ .../guolanren/WebSocketJavaxApplication.java} | 4 +- .../guolanren/config/HttpRequestListener.java | 16 +++++ .../guolanren/config/WebMvcConfiguration.java | 21 ++++++ .../config/WebSocketJavaxConfiguration.java | 32 +++++++++ .../guolanren/endpoint/ChattingEndpoint.java | 48 +++++++++++++ .../web-socket-message-broker/README.md | 5 ++ web-socket/web-socket-message-broker/pom.xml | 68 +++++++++++++++++++ .../WebSocketMessageBrokerApplication.java | 15 ++++ .../WebSocketMessageBrokerConfiguration.java} | 2 +- .../controller/ChattingController.java | 0 .../src/main/resources/static/app.js | 0 .../src/main/resources/static/index.html | 0 .../src/main/resources/static/main.css | 0 22 files changed, 430 insertions(+), 11 deletions(-) delete mode 100644 web-socket/README.md create mode 100644 web-socket/web-socket-handler/README.md rename web-socket/{ => web-socket-handler}/pom.xml (95%) create mode 100644 web-socket/web-socket-handler/src/main/java/name/guolanren/WebSocketHandlerApplication.java create mode 100644 web-socket/web-socket-handler/src/main/java/name/guolanren/config/MyHttpSessionHandshakeInterceptor.java create mode 100644 web-socket/web-socket-handler/src/main/java/name/guolanren/config/WebSocketHandlerConfiguration.java create mode 100644 web-socket/web-socket-handler/src/main/java/name/guolanren/controller/ChattingController.java create mode 100644 web-socket/web-socket-javax/README.md create mode 100644 web-socket/web-socket-javax/pom.xml rename web-socket/{src/main/java/name/guolanren/WebSocketApplication.java => web-socket-javax/src/main/java/name/guolanren/WebSocketJavaxApplication.java} (69%) create mode 100644 web-socket/web-socket-javax/src/main/java/name/guolanren/config/HttpRequestListener.java create mode 100644 web-socket/web-socket-javax/src/main/java/name/guolanren/config/WebMvcConfiguration.java create mode 100644 web-socket/web-socket-javax/src/main/java/name/guolanren/config/WebSocketJavaxConfiguration.java create mode 100644 web-socket/web-socket-javax/src/main/java/name/guolanren/endpoint/ChattingEndpoint.java create mode 100644 web-socket/web-socket-message-broker/README.md create mode 100644 web-socket/web-socket-message-broker/pom.xml create mode 100644 web-socket/web-socket-message-broker/src/main/java/name/guolanren/WebSocketMessageBrokerApplication.java rename web-socket/{src/main/java/name/guolanren/config/WebSocketConfiguration.java => web-socket-message-broker/src/main/java/name/guolanren/config/WebSocketMessageBrokerConfiguration.java} (89%) rename web-socket/{ => web-socket-message-broker}/src/main/java/name/guolanren/controller/ChattingController.java (100%) rename web-socket/{ => web-socket-message-broker}/src/main/resources/static/app.js (100%) rename web-socket/{ => web-socket-message-broker}/src/main/resources/static/index.html (100%) rename web-socket/{ => web-socket-message-broker}/src/main/resources/static/main.css (100%) diff --git a/web-socket/README.md b/web-socket/README.md deleted file mode 100644 index 848adf5..0000000 --- a/web-socket/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# web socket - -## 概述 - -web socket 概述... \ No newline at end of file diff --git a/web-socket/web-socket-handler/README.md b/web-socket/web-socket-handler/README.md new file mode 100644 index 0000000..5996e5f --- /dev/null +++ b/web-socket/web-socket-handler/README.md @@ -0,0 +1,5 @@ +# web socket handler + +## 概述 + +web socket handler 概述... \ No newline at end of file diff --git a/web-socket/pom.xml b/web-socket/web-socket-handler/pom.xml similarity index 95% rename from web-socket/pom.xml rename to web-socket/web-socket-handler/pom.xml index 609e1b2..12c1ed3 100644 --- a/web-socket/pom.xml +++ b/web-socket/web-socket-handler/pom.xml @@ -12,10 +12,8 @@ name.guolanren - web-socket + web-socket-handler 1.0.0 - web-socket - web socket 1.8 diff --git a/web-socket/web-socket-handler/src/main/java/name/guolanren/WebSocketHandlerApplication.java b/web-socket/web-socket-handler/src/main/java/name/guolanren/WebSocketHandlerApplication.java new file mode 100644 index 0000000..50c3759 --- /dev/null +++ b/web-socket/web-socket-handler/src/main/java/name/guolanren/WebSocketHandlerApplication.java @@ -0,0 +1,15 @@ +package name.guolanren; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author guolanren + */ +@SpringBootApplication +public class WebSocketHandlerApplication { + + public static void main(String[] args) { + SpringApplication.run(WebSocketHandlerApplication.class, args); + } +} diff --git a/web-socket/web-socket-handler/src/main/java/name/guolanren/config/MyHttpSessionHandshakeInterceptor.java b/web-socket/web-socket-handler/src/main/java/name/guolanren/config/MyHttpSessionHandshakeInterceptor.java new file mode 100644 index 0000000..cc8e5e8 --- /dev/null +++ b/web-socket/web-socket-handler/src/main/java/name/guolanren/config/MyHttpSessionHandshakeInterceptor.java @@ -0,0 +1,24 @@ +package name.guolanren.config; + +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.web.socket.WebSocketHandler; +import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor; + +import java.util.Map; + +/** + * @author guolanren + */ +public class MyHttpSessionHandshakeInterceptor extends HttpSessionHandshakeInterceptor { + + @Override + public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception { + return super.beforeHandshake(request, response, wsHandler, attributes); + } + + @Override + public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) { + super.afterHandshake(request, response, wsHandler, ex); + } +} diff --git a/web-socket/web-socket-handler/src/main/java/name/guolanren/config/WebSocketHandlerConfiguration.java b/web-socket/web-socket-handler/src/main/java/name/guolanren/config/WebSocketHandlerConfiguration.java new file mode 100644 index 0000000..ad11d2b --- /dev/null +++ b/web-socket/web-socket-handler/src/main/java/name/guolanren/config/WebSocketHandlerConfiguration.java @@ -0,0 +1,38 @@ +package name.guolanren.config; + +import name.guolanren.controller.ChattingController; +import org.springframework.beans.factory.annotation.Autowired; +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.config.annotation.WebSocketConfigurer; +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; +import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor; + +/** + * @author guolanren + */ +@Configuration +@EnableWebSocket +public class WebSocketHandlerConfiguration implements WebSocketConfigurer { + + @Autowired + private ChattingController webSocketHandler; + + @Override + public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) { + webSocketHandlerRegistry.addHandler(webSocketHandler, "/web-socket") + .addInterceptors(handshakeInterceptor()) + .setAllowedOrigins("*"); + +// webSocketHandlerRegistry.addHandler(webSocketHandler(), "/web-socket") +// .addInterceptors(handshakeInterceptor()) +// .setAllowedOrigins("*") +// .withSockJS(); + } + + @Bean + public HttpSessionHandshakeInterceptor handshakeInterceptor() { + return new MyHttpSessionHandshakeInterceptor(); + } +} \ No newline at end of file diff --git a/web-socket/web-socket-handler/src/main/java/name/guolanren/controller/ChattingController.java b/web-socket/web-socket-handler/src/main/java/name/guolanren/controller/ChattingController.java new file mode 100644 index 0000000..cb4ea42 --- /dev/null +++ b/web-socket/web-socket-handler/src/main/java/name/guolanren/controller/ChattingController.java @@ -0,0 +1,67 @@ +package name.guolanren.controller; + +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.socket.*; +import org.springframework.web.socket.handler.TextWebSocketHandler; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * @author guolanren + */ +@RestController +public class ChattingController extends TextWebSocketHandler { + + private List sessions = new ArrayList<>(); + + @PostMapping("/chat") + public String chatting(@RequestBody String message) throws IOException { + for (WebSocketSession session : sessions) { + session.sendMessage(new TextMessage(message.toUpperCase())); + } + return null; + } + + @Override + public void afterConnectionEstablished(WebSocketSession session) throws Exception { + super.afterConnectionEstablished(session); + sessions.add(session); + } + + @Override + public void handleMessage(WebSocketSession session, WebSocketMessage message) throws Exception { + String receiveMsg = (String) message.getPayload(); + String sendMsg = receiveMsg.toUpperCase(); + session.sendMessage(new TextMessage(sendMsg)); + } + + @Override + protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { + super.handleTextMessage(session, message); + } + + @Override + protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception { + super.handlePongMessage(session, message); + } + + @Override + public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { + super.handleTransportError(session, exception); + } + + @Override + public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { + sessions.remove(session); + super.afterConnectionClosed(session, status); + } + + @Override + public boolean supportsPartialMessages() { + return super.supportsPartialMessages(); + } +} diff --git a/web-socket/web-socket-javax/README.md b/web-socket/web-socket-javax/README.md new file mode 100644 index 0000000..83e0ac9 --- /dev/null +++ b/web-socket/web-socket-javax/README.md @@ -0,0 +1,4 @@ +# web socket javax +## 概述 + +web socket javax 概述... \ No newline at end of file diff --git a/web-socket/web-socket-javax/pom.xml b/web-socket/web-socket-javax/pom.xml new file mode 100644 index 0000000..593eb06 --- /dev/null +++ b/web-socket/web-socket-javax/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.1.6.RELEASE + + + + name.guolanren + web-socket-javax + 1.0.0 + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-websocket + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.webjars + webjars-locator-core + + + org.webjars + sockjs-client + 1.0.2 + + + org.webjars + stomp-websocket + 2.3.3 + + + org.webjars + bootstrap + 3.3.7 + + + org.webjars + jquery + 3.1.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/web-socket/src/main/java/name/guolanren/WebSocketApplication.java b/web-socket/web-socket-javax/src/main/java/name/guolanren/WebSocketJavaxApplication.java similarity index 69% rename from web-socket/src/main/java/name/guolanren/WebSocketApplication.java rename to web-socket/web-socket-javax/src/main/java/name/guolanren/WebSocketJavaxApplication.java index 0aa9aa2..e403727 100644 --- a/web-socket/src/main/java/name/guolanren/WebSocketApplication.java +++ b/web-socket/web-socket-javax/src/main/java/name/guolanren/WebSocketJavaxApplication.java @@ -7,9 +7,9 @@ * @author guolanren */ @SpringBootApplication -public class WebSocketApplication { +public class WebSocketJavaxApplication { public static void main(String[] args) { - SpringApplication.run(WebSocketApplication.class, args); + SpringApplication.run(WebSocketJavaxApplication.class, args); } } diff --git a/web-socket/web-socket-javax/src/main/java/name/guolanren/config/HttpRequestListener.java b/web-socket/web-socket-javax/src/main/java/name/guolanren/config/HttpRequestListener.java new file mode 100644 index 0000000..24fdf80 --- /dev/null +++ b/web-socket/web-socket-javax/src/main/java/name/guolanren/config/HttpRequestListener.java @@ -0,0 +1,16 @@ +package name.guolanren.config; + +import javax.servlet.ServletRequestEvent; +import javax.servlet.ServletRequestListener; +import javax.servlet.http.HttpServletRequest; + +/** + * @author guolanren + */ +public class HttpRequestListener implements ServletRequestListener { + + @Override + public void requestInitialized(ServletRequestEvent sre) { + ((HttpServletRequest) sre.getServletRequest()).getSession(); + } +} diff --git a/web-socket/web-socket-javax/src/main/java/name/guolanren/config/WebMvcConfiguration.java b/web-socket/web-socket-javax/src/main/java/name/guolanren/config/WebMvcConfiguration.java new file mode 100644 index 0000000..8f24a00 --- /dev/null +++ b/web-socket/web-socket-javax/src/main/java/name/guolanren/config/WebMvcConfiguration.java @@ -0,0 +1,21 @@ +package name.guolanren.config; + +import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; + +/** + * @author guolanren + */ +@Configuration +public class WebMvcConfiguration extends WebMvcConfigurationSupport { + + @Bean + public ServletListenerRegistrationBean httpRequestListener() { + ServletListenerRegistrationBean httpRequestListener = new ServletListenerRegistrationBean<>(); + httpRequestListener.setListener(new HttpRequestListener()); + return httpRequestListener; + } + +} diff --git a/web-socket/web-socket-javax/src/main/java/name/guolanren/config/WebSocketJavaxConfiguration.java b/web-socket/web-socket-javax/src/main/java/name/guolanren/config/WebSocketJavaxConfiguration.java new file mode 100644 index 0000000..82d49a4 --- /dev/null +++ b/web-socket/web-socket-javax/src/main/java/name/guolanren/config/WebSocketJavaxConfiguration.java @@ -0,0 +1,32 @@ +package name.guolanren.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.socket.server.standard.ServerEndpointExporter; + +import javax.servlet.http.HttpSession; +import javax.websocket.HandshakeResponse; +import javax.websocket.server.HandshakeRequest; +import javax.websocket.server.ServerEndpointConfig; + +/** + * @author guolanren + */ +@Configuration +public class WebSocketJavaxConfiguration { + + @Bean + public ServerEndpointExporter serverEndpointExporter() { + return new ServerEndpointExporter(); + } + + public static class HttpSessionConfigurator extends ServerEndpointConfig.Configurator { + + @Override + public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { + super.modifyHandshake(sec, request, response); + HttpSession httpSession = (HttpSession) request.getHttpSession(); + sec.getUserProperties().put(HttpSession.class.getName(), httpSession); + } + } +} \ No newline at end of file diff --git a/web-socket/web-socket-javax/src/main/java/name/guolanren/endpoint/ChattingEndpoint.java b/web-socket/web-socket-javax/src/main/java/name/guolanren/endpoint/ChattingEndpoint.java new file mode 100644 index 0000000..7b57db0 --- /dev/null +++ b/web-socket/web-socket-javax/src/main/java/name/guolanren/endpoint/ChattingEndpoint.java @@ -0,0 +1,48 @@ +package name.guolanren.endpoint; + +import name.guolanren.config.WebSocketJavaxConfiguration; +import org.springframework.stereotype.Component; +import org.springframework.web.socket.*; + +import javax.websocket.*; +import javax.websocket.server.ServerEndpoint; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * @author guolanren + */ +@ServerEndpoint(value = "/chat", configurator = WebSocketJavaxConfiguration.HttpSessionConfigurator.class) +@Component +public class ChattingEndpoint { + + private List sessions = new ArrayList<>(); + + @OnOpen + public void onOpen(Session session, EndpointConfig config) throws IOException { + System.out.println("open..."); + +// HttpSession httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName()); +// if (httpSession == null) { +// session.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, "找不到 HTTP 会话")); +// } + + } + + @OnClose + public void onClose() { + System.out.println("close..."); + } + + @OnMessage + public void onMessage(String message, Session session) throws IOException { + session.getBasicRemote().sendText(message); + } + + @OnError + public void onError(Session session, Throwable error) { + error.printStackTrace(); + } + +} diff --git a/web-socket/web-socket-message-broker/README.md b/web-socket/web-socket-message-broker/README.md new file mode 100644 index 0000000..a73cf55 --- /dev/null +++ b/web-socket/web-socket-message-broker/README.md @@ -0,0 +1,5 @@ +# web socket message broker + +## 概述 + +web socket message broker 概述... \ No newline at end of file diff --git a/web-socket/web-socket-message-broker/pom.xml b/web-socket/web-socket-message-broker/pom.xml new file mode 100644 index 0000000..3f60a85 --- /dev/null +++ b/web-socket/web-socket-message-broker/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.1.6.RELEASE + + + + name.guolanren + web-socket-message-broker + 1.0.0 + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-websocket + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.webjars + webjars-locator-core + + + org.webjars + sockjs-client + 1.0.2 + + + org.webjars + stomp-websocket + 2.3.3 + + + org.webjars + bootstrap + 3.3.7 + + + org.webjars + jquery + 3.1.0 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/web-socket/web-socket-message-broker/src/main/java/name/guolanren/WebSocketMessageBrokerApplication.java b/web-socket/web-socket-message-broker/src/main/java/name/guolanren/WebSocketMessageBrokerApplication.java new file mode 100644 index 0000000..d4058b5 --- /dev/null +++ b/web-socket/web-socket-message-broker/src/main/java/name/guolanren/WebSocketMessageBrokerApplication.java @@ -0,0 +1,15 @@ +package name.guolanren; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author guolanren + */ +@SpringBootApplication +public class WebSocketMessageBrokerApplication { + + public static void main(String[] args) { + SpringApplication.run(WebSocketMessageBrokerApplication.class, args); + } +} diff --git a/web-socket/src/main/java/name/guolanren/config/WebSocketConfiguration.java b/web-socket/web-socket-message-broker/src/main/java/name/guolanren/config/WebSocketMessageBrokerConfiguration.java similarity index 89% rename from web-socket/src/main/java/name/guolanren/config/WebSocketConfiguration.java rename to web-socket/web-socket-message-broker/src/main/java/name/guolanren/config/WebSocketMessageBrokerConfiguration.java index 438ee78..e827f80 100644 --- a/web-socket/src/main/java/name/guolanren/config/WebSocketConfiguration.java +++ b/web-socket/web-socket-message-broker/src/main/java/name/guolanren/config/WebSocketMessageBrokerConfiguration.java @@ -11,7 +11,7 @@ */ @Configuration @EnableWebSocketMessageBroker -public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer { +public class WebSocketMessageBrokerConfiguration implements WebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { diff --git a/web-socket/src/main/java/name/guolanren/controller/ChattingController.java b/web-socket/web-socket-message-broker/src/main/java/name/guolanren/controller/ChattingController.java similarity index 100% rename from web-socket/src/main/java/name/guolanren/controller/ChattingController.java rename to web-socket/web-socket-message-broker/src/main/java/name/guolanren/controller/ChattingController.java diff --git a/web-socket/src/main/resources/static/app.js b/web-socket/web-socket-message-broker/src/main/resources/static/app.js similarity index 100% rename from web-socket/src/main/resources/static/app.js rename to web-socket/web-socket-message-broker/src/main/resources/static/app.js diff --git a/web-socket/src/main/resources/static/index.html b/web-socket/web-socket-message-broker/src/main/resources/static/index.html similarity index 100% rename from web-socket/src/main/resources/static/index.html rename to web-socket/web-socket-message-broker/src/main/resources/static/index.html diff --git a/web-socket/src/main/resources/static/main.css b/web-socket/web-socket-message-broker/src/main/resources/static/main.css similarity index 100% rename from web-socket/src/main/resources/static/main.css rename to web-socket/web-socket-message-broker/src/main/resources/static/main.css