diff --git a/.gitignore b/.gitignore index 2da1a73..23646f8 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ manifest.yml overridedb.* settings.xml target +classes out transaction-logs .flattened-pom.xml diff --git a/README.md b/README.md index f113b22..f1035cb 100644 --- a/README.md +++ b/README.md @@ -31,15 +31,15 @@ spring-cloud:Finchley.SR2 | [spring-jdbc](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-jdbc)
[spring-jdbc-annotation](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-jdbc-annotation) | spring jdbc-template 的使用 | [Using JdbcTemplat](https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/data-access.html#jdbc-JdbcTemplate) | | [spring-mybatis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-mybatis)
[spring-mybatis-annotation](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-mybatis-annotation) | spring 整合 mybatis | [mybatis-spring中文文档](http://www.mybatis.org/spring/zh/index.html) | | [spring-druid-mybatis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-druid-mybatis)
[spring-druid-mybatis-annotation](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-druid-mybatis-annotation) | spring 整合druid、mybatis | [alibaba druid](https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) | -| spring-redis
spring-redis-annotation | spring 整合 redis 单机、集群(jedis客户端)
spring 整合 redis 单机、集群(redisson客户端) | [redisson中文文档](https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95) | +| spring-redis
spring-redis-annotation | spring 整合 redis 单机、集群(jedis客户端)
spring 整合 redis 单机、集群(redisson客户端) | [redisson官方中文文档](https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95) | | spring-mongodb
spring-mongodb-annotation | spring 整合 mongodb | [Spring Data MongoDB](https://docs.spring.io/spring-data/mongodb/docs/2.1.3.RELEASE/reference/html/#mongo.mongo-db-factory-java) | -| spring-memcached
spring-memcached-annotation | spring 整合 memcached(单机、集群) | [Xmemcached中文文档](https://github.com/killme2008/xmemcached/wiki/Xmemcached%20%E4%B8%AD%E6%96%87%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97) | -| spring-rabbitmq
spring-rabbitmq-annotation | spring 整合 rabbitmq | | +| spring-memcached
spring-memcached-annotation | spring 整合 memcached(单机、集群) | [Xmemcached官方中文文档](https://github.com/killme2008/xmemcached/wiki/Xmemcached%20%E4%B8%AD%E6%96%87%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97) | +| spring-rabbitmq
spring-rabbitmq-annotation | spring 整合 rabbitmq、消息序列化与反序列化 | [rabbitmq 官方文档](http://www.rabbitmq.com/getstarted.html)
[spring amqp](https://docs.spring.io/spring-amqp/docs/2.1.3.BUILD-SNAPSHOT/reference/html/) | | spring-kafka
spring-kafka-annotation | spring 整合 kafka | | -| spring-dubbo
spring-dubbo-annotation | spring 整合 dubbo | | -| spring-websocket
spring-websocket-annotation | spring 整合 websocket | | -| spring-netty
spring-netty-annotation | spring 整合 netty | | -| spring-scheduled
spring-scheduled-annotation | spring 定时任务 | | +| spring-dubbo
spring-dubbo-annotation | spring 整合 dubbo | [duboo 官方中文文档](http://dubbo.apache.org/zh-cn/docs/user/quick-start.html) | +| spring-websocket
spring-websocket-annotation | spring 整合 websocket | [spring websocket](https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/web.html#websocket) | +| spring-mail
spring-mail-annotation | spring 普通文本邮件、附件邮件、模板邮件 | [spring email](https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/integration.html#mail) | +| spring-scheduled
spring-scheduled-annotation | spring 定时任务 | [Task Execution and Scheduling](https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/integration.html#scheduling) |
@@ -83,8 +83,6 @@ spring-cloud:Finchley.SR2 | spring-cloud-zuul | spring cloud 网关限流、权限验证 | | | spring-cloud-sleuth-Zipkin | spring cloud 服务追踪 | | -推荐课程:[Spring Cloud微服务实战](https://coding.imooc.com/class/187.html) 廖大神结合实战,很详细的介绍spring cloud各个组件的使用。 -
## 4.参考资料 diff --git a/spring/spring-websocket-annotation/pom.xml b/spring/spring-websocket-annotation/pom.xml new file mode 100644 index 0000000..1a4c08d --- /dev/null +++ b/spring/spring-websocket-annotation/pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + + com.heibaiying + spring-websocket + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + 5.1.3.RELEASE + + + + + org.springframework + spring-context + ${spring-base-version} + + + org.springframework + spring-beans + ${spring-base-version} + + + org.springframework + spring-core + ${spring-base-version} + + + org.springframework + spring-web + ${spring-base-version} + + + org.springframework + spring-webmvc + ${spring-base-version} + + + javax.servlet + javax.servlet-api + 4.0.1 + provided + + + commons-codec + commons-codec + 1.9 + + + + org.springframework + spring-websocket + 5.1.3.RELEASE + + + + \ No newline at end of file diff --git a/spring/spring-websocket-annotation/src/main/java/com/heibaiying/constant/Constant.java b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/constant/Constant.java new file mode 100644 index 0000000..a3417e6 --- /dev/null +++ b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/constant/Constant.java @@ -0,0 +1,11 @@ +package com.heibaiying.constant; + +/** + * @author : 罗祥 + * @description : + * @date :create in 2018/12/27 + */ +public interface Constant { + + String USER_NAME="username"; +} diff --git a/spring/spring-websocket-annotation/src/main/java/com/heibaiying/controller/LoginController.java b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/controller/LoginController.java new file mode 100644 index 0000000..f7c2276 --- /dev/null +++ b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/controller/LoginController.java @@ -0,0 +1,23 @@ +package com.heibaiying.controller; + +import com.heibaiying.constant.Constant; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +import javax.servlet.http.HttpSession; + +/** + * @author : 罗祥 + * @description : 简单登录 + * @date :create in 2018/12/27 + */ +@Controller +public class LoginController { + + @PostMapping("login") + public String login(String username, HttpSession session){ + session.setAttribute(Constant.USER_NAME,username); + return "chat"; + } +} diff --git a/spring/spring-websocket-annotation/src/main/java/com/heibaiying/webconfig/DispatcherServletInitializer.java b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/webconfig/DispatcherServletInitializer.java new file mode 100644 index 0000000..000b86f --- /dev/null +++ b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/webconfig/DispatcherServletInitializer.java @@ -0,0 +1,23 @@ +package com.heibaiying.webconfig; + +import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; + +/** + * @author : 罗祥 + * @description : 等价于 web.xml 中配置前端控制器 + * @date :create in 2018/12/27 + */ +public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { + + protected Class[] getRootConfigClasses() { + return new Class[0]; + } + + protected Class[] getServletConfigClasses() { + return new Class[]{ServletConfig.class}; + } + + protected String[] getServletMappings() { + return new String[]{"/"}; + } +} diff --git a/spring/spring-websocket-annotation/src/main/java/com/heibaiying/webconfig/ServletConfig.java b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/webconfig/ServletConfig.java new file mode 100644 index 0000000..af45804 --- /dev/null +++ b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/webconfig/ServletConfig.java @@ -0,0 +1,42 @@ +package com.heibaiying.webconfig; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +/** + * @author : 罗祥 + * @description : 主配置类 + * @date :create in 2018/12/27 + */ +@Configuration +@EnableWebMvc +@ComponentScan(basePackages = {"com.heibaiying.*"}) +public class ServletConfig implements WebMvcConfigurer { + + + /** + * 配置视图解析器 + */ + @Bean + public ViewResolver viewResolver() { + InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); + internalResourceViewResolver.setPrefix("/WEB-INF/jsp/"); + internalResourceViewResolver.setSuffix(".jsp"); + internalResourceViewResolver.setExposeContextBeansAsAttributes(true); + return internalResourceViewResolver; + } + + /** + * 配置静态资源处理器 + */ + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + configurer.enable(); + } + +} \ No newline at end of file diff --git a/spring/spring-websocket-annotation/src/main/java/com/heibaiying/webconfig/characterEncodingFilter.java b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/webconfig/characterEncodingFilter.java new file mode 100644 index 0000000..0958a33 --- /dev/null +++ b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/webconfig/characterEncodingFilter.java @@ -0,0 +1,21 @@ +package com.heibaiying.webconfig; + +import org.springframework.web.filter.CharacterEncodingFilter; + +import javax.servlet.annotation.WebFilter; +import javax.servlet.annotation.WebInitParam; + +/** + * @author : 罗祥 + * @description : 编码过滤器 防止乱码 + * @date :create in 2018/12/27 + */ +@WebFilter(filterName = "characterEncodingFilter", urlPatterns = "/*", + initParams = { + @WebInitParam(name = "encoding", value = "UTF-8"), + @WebInitParam(name = "forceEncoding", value = "true") + } +) +public class characterEncodingFilter extends CharacterEncodingFilter { + +} diff --git a/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/CustomHandshakeInterceptor.java b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/CustomHandshakeInterceptor.java new file mode 100644 index 0000000..e845c54 --- /dev/null +++ b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/CustomHandshakeInterceptor.java @@ -0,0 +1,32 @@ +package com.heibaiying.websocket; + +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.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.Map; + +/** + * @author : heibaiying + * @description : 可以按照需求实现权限拦截等功能 + */ +public class CustomHandshakeInterceptor extends HttpSessionHandshakeInterceptor { + + @Override + public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception { + InetSocketAddress remoteAddress = request.getRemoteAddress(); + InetAddress address = remoteAddress.getAddress(); + System.out.println(address); + /* + * 最后需要要显示调用父类方法,父类的beforeHandshake方法 + * 把ServerHttpRequest 中session中对应的值拷贝到WebSocketSession中。 + * 如果我们没有实现这个方法,我们在最后的handler处理中 是拿不到 session中的值 + * 作为测试 可以注释掉下面这一行 可以发现自定义处理器中session的username总是为空 + */ + return super.beforeHandshake(request, response, wsHandler, attributes); + } +} + diff --git a/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/CustomerHandler.java b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/CustomerHandler.java new file mode 100644 index 0000000..4dca73a --- /dev/null +++ b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/CustomerHandler.java @@ -0,0 +1,60 @@ +package com.heibaiying.websocket; + +import com.heibaiying.constant.Constant; +import org.springframework.web.socket.CloseStatus; +import org.springframework.web.socket.TextMessage; +import org.springframework.web.socket.WebSocketSession; +import org.springframework.web.socket.handler.TextWebSocketHandler; + +import java.io.IOException; +import java.net.URLDecoder; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author : heibaiying + * @description : 自定义消息处理类 + */ +public class CustomerHandler extends TextWebSocketHandler { + + private Map nameAndSession = new ConcurrentHashMap<>(); + + // 建立连接时候触发 + @Override + public void afterConnectionEstablished(WebSocketSession session) { + String username = getNameFromSession(session); + nameAndSession.putIfAbsent(username, session); + } + + + // 关闭连接时候触发 + @Override + public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { + String username = getNameFromSession(session); + nameAndSession.remove(username); + } + + // 处理消息 + @Override + protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { + // 防止中文乱码 + String msg = URLDecoder.decode(message.getPayload(), "utf-8"); + String username = getNameFromSession(session); + // 简单模拟群发消息 + TextMessage reply = new TextMessage(username + " : " + msg); + nameAndSession.forEach((s, webSocketSession) + -> { + try { + webSocketSession.sendMessage(reply); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + + + private String getNameFromSession(WebSocketSession session) { + Map attributes = session.getAttributes(); + return (String) attributes.get(Constant.USER_NAME); + } +} diff --git a/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/WebSocketConfig.java b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/WebSocketConfig.java new file mode 100644 index 0000000..7854d56 --- /dev/null +++ b/spring/spring-websocket-annotation/src/main/java/com/heibaiying/websocket/WebSocketConfig.java @@ -0,0 +1,21 @@ +package com.heibaiying.websocket; + +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; + +/** + * @author : 罗祥 + * @description :websocket 配置 + * @date :create in 2018/12/27 + */ +@Configuration +@EnableWebSocket +public class WebSocketConfig implements WebSocketConfigurer { + + @Override + public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { + registry.addHandler(new CustomerHandler(), "/socket").addInterceptors(new CustomHandshakeInterceptor()); + } +} diff --git a/spring/spring-websocket-annotation/src/main/webapp/WEB-INF/jsp/chat.jsp b/spring/spring-websocket-annotation/src/main/webapp/WEB-INF/jsp/chat.jsp new file mode 100644 index 0000000..a479696 --- /dev/null +++ b/spring/spring-websocket-annotation/src/main/webapp/WEB-INF/jsp/chat.jsp @@ -0,0 +1,38 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + ${sessionScope.get("username")}您好!欢迎进入群聊大厅! + + + + +
+ +
+ + + diff --git a/spring/spring-websocket-annotation/src/main/webapp/index.jsp b/spring/spring-websocket-annotation/src/main/webapp/index.jsp new file mode 100644 index 0000000..507abb5 --- /dev/null +++ b/spring/spring-websocket-annotation/src/main/webapp/index.jsp @@ -0,0 +1,12 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + +
+ + diff --git a/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/com/heibaiying/constant/Constant.class b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/com/heibaiying/constant/Constant.class new file mode 100644 index 0000000..a67e95d Binary files /dev/null and b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/com/heibaiying/constant/Constant.class differ diff --git a/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/com/heibaiying/controller/LoginController.class b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/com/heibaiying/controller/LoginController.class new file mode 100644 index 0000000..018a140 Binary files /dev/null and b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/com/heibaiying/controller/LoginController.class differ diff --git a/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/com/heibaiying/websocket/CustomHandshakeInterceptor.class b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/com/heibaiying/websocket/CustomHandshakeInterceptor.class new file mode 100644 index 0000000..31cf9bf Binary files /dev/null and b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/com/heibaiying/websocket/CustomHandshakeInterceptor.class differ diff --git a/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/com/heibaiying/websocket/CustomerHandler.class b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/com/heibaiying/websocket/CustomerHandler.class new file mode 100644 index 0000000..e80f9c7 Binary files /dev/null and b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/com/heibaiying/websocket/CustomerHandler.class differ diff --git a/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/springApplication.xml b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/springApplication.xml new file mode 100644 index 0000000..80023cc --- /dev/null +++ b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/classes/springApplication.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/jsp/chat.jsp b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/jsp/chat.jsp new file mode 100644 index 0000000..a479696 --- /dev/null +++ b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/jsp/chat.jsp @@ -0,0 +1,38 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + ${sessionScope.get("username")}您好!欢迎进入群聊大厅! + + + + +
+ +
+ + + diff --git a/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/web.xml b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/web.xml new file mode 100644 index 0000000..7bb4250 --- /dev/null +++ b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/WEB-INF/web.xml @@ -0,0 +1,41 @@ + + + + + + springMvc + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + classpath:springApplication.xml + + 1 + + + + springMvc + / + + + + characterEncodingFilter + org.springframework.web.filter.CharacterEncodingFilter + + encoding + UTF-8 + + + forceEncoding + true + + + + characterEncodingFilter + /* + + + \ No newline at end of file diff --git a/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/index.jsp b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/index.jsp new file mode 100644 index 0000000..507abb5 --- /dev/null +++ b/spring/spring-websocket/classes/artifacts/spring_websocket_Web_exploded/index.jsp @@ -0,0 +1,12 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +
+ + +
+ + diff --git a/spring/spring-websocket/pom.xml b/spring/spring-websocket/pom.xml index efadbc9..1a4c08d 100644 --- a/spring/spring-websocket/pom.xml +++ b/spring/spring-websocket/pom.xml @@ -57,6 +57,11 @@ 4.0.1 provided + + commons-codec + commons-codec + 1.9 + org.springframework diff --git a/spring/spring-websocket/src/main/java/com/heibaiying/constant/Constant.java b/spring/spring-websocket/src/main/java/com/heibaiying/constant/Constant.java new file mode 100644 index 0000000..a3417e6 --- /dev/null +++ b/spring/spring-websocket/src/main/java/com/heibaiying/constant/Constant.java @@ -0,0 +1,11 @@ +package com.heibaiying.constant; + +/** + * @author : 罗祥 + * @description : + * @date :create in 2018/12/27 + */ +public interface Constant { + + String USER_NAME="username"; +} diff --git a/spring/spring-websocket/src/main/java/com/heibaiying/controller/LoginController.java b/spring/spring-websocket/src/main/java/com/heibaiying/controller/LoginController.java new file mode 100644 index 0000000..f7c2276 --- /dev/null +++ b/spring/spring-websocket/src/main/java/com/heibaiying/controller/LoginController.java @@ -0,0 +1,23 @@ +package com.heibaiying.controller; + +import com.heibaiying.constant.Constant; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +import javax.servlet.http.HttpSession; + +/** + * @author : 罗祥 + * @description : 简单登录 + * @date :create in 2018/12/27 + */ +@Controller +public class LoginController { + + @PostMapping("login") + public String login(String username, HttpSession session){ + session.setAttribute(Constant.USER_NAME,username); + return "chat"; + } +} diff --git a/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomHandshakeInterceptor.java b/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomHandshakeInterceptor.java index 5dae0f5..e845c54 100644 --- a/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomHandshakeInterceptor.java +++ b/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomHandshakeInterceptor.java @@ -5,24 +5,27 @@ import org.springframework.http.server.ServerHttpResponse; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.Map; /** * @author : heibaiying - * @description : 握手拦截器 + * @description : 可以按照需求实现权限拦截等功能 */ public class CustomHandshakeInterceptor extends HttpSessionHandshakeInterceptor { - /*@Override - public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception { - InetSocketAddress remoteAddress = request.getRemoteAddress(); - System.out.println(remoteAddress); - return true; - }*/ - @Override public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map attributes) throws Exception { + InetSocketAddress remoteAddress = request.getRemoteAddress(); + InetAddress address = remoteAddress.getAddress(); + System.out.println(address); + /* + * 最后需要要显示调用父类方法,父类的beforeHandshake方法 + * 把ServerHttpRequest 中session中对应的值拷贝到WebSocketSession中。 + * 如果我们没有实现这个方法,我们在最后的handler处理中 是拿不到 session中的值 + * 作为测试 可以注释掉下面这一行 可以发现自定义处理器中session的username总是为空 + */ return super.beforeHandshake(request, response, wsHandler, attributes); } } diff --git a/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomerHandler.java b/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomerHandler.java index 5b52723..6ee4e2e 100644 --- a/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomerHandler.java +++ b/spring/spring-websocket/src/main/java/com/heibaiying/websocket/CustomerHandler.java @@ -1,21 +1,61 @@ package com.heibaiying.websocket; +import com.heibaiying.constant.Constant; +import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.TextMessage; +import org.springframework.web.socket.WebSocketMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; -import java.time.LocalDateTime; +import java.io.IOException; +import java.net.URLDecoder; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * @author : heibaiying - * @description : 自定义消息处理器 + * @description : 自定义消息处理类 */ public class CustomerHandler extends TextWebSocketHandler { + private Map nameAndSession = new ConcurrentHashMap<>(); + + // 建立连接时候触发 + @Override + public void afterConnectionEstablished(WebSocketSession session) { + String username = getNameFromSession(session); + nameAndSession.putIfAbsent(username, session); + } + + + // 关闭连接时候触发 + @Override + public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { + String username = getNameFromSession(session); + nameAndSession.remove(username); + } + + // 处理消息 @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { - String stringMessage = new String(message.asBytes()); - System.out.println("服务端收到消息:" + stringMessage); - session.sendMessage(new TextMessage(stringMessage+LocalDateTime.now())); + // 防止中文乱码 + String msg = URLDecoder.decode(message.getPayload(), "utf-8"); + String username = getNameFromSession(session); + // 简单模拟群发消息 + TextMessage reply = new TextMessage(username + " : " + msg); + nameAndSession.forEach((s, webSocketSession) + -> { + try { + webSocketSession.sendMessage(reply); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + + + private String getNameFromSession(WebSocketSession session) { + Map attributes = session.getAttributes(); + return (String) attributes.get(Constant.USER_NAME); } } diff --git a/spring/spring-websocket/src/main/webapp/WEB-INF/jsp/chat.jsp b/spring/spring-websocket/src/main/webapp/WEB-INF/jsp/chat.jsp new file mode 100644 index 0000000..a479696 --- /dev/null +++ b/spring/spring-websocket/src/main/webapp/WEB-INF/jsp/chat.jsp @@ -0,0 +1,38 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + ${sessionScope.get("username")}您好!欢迎进入群聊大厅! + + + + +
+ +
+ + + diff --git a/spring/spring-websocket/src/main/webapp/WEB-INF/web.xml b/spring/spring-websocket/src/main/webapp/WEB-INF/web.xml index c5e8f38..d64fe9d 100644 --- a/spring/spring-websocket/src/main/webapp/WEB-INF/web.xml +++ b/spring/spring-websocket/src/main/webapp/WEB-INF/web.xml @@ -21,4 +21,22 @@ / + + + characterEncodingFilter + org.springframework.web.filter.CharacterEncodingFilter + + encoding + UTF-8 + + + forceEncoding + true + + + + characterEncodingFilter + /* + + \ No newline at end of file diff --git a/spring/spring-websocket/src/main/webapp/index.jsp b/spring/spring-websocket/src/main/webapp/index.jsp index 1f75321..507abb5 100644 --- a/spring/spring-websocket/src/main/webapp/index.jsp +++ b/spring/spring-websocket/src/main/webapp/index.jsp @@ -4,34 +4,9 @@ Title - - -
- -
- +
+ + +