===============================================
环境介绍:
Jdk 1.7
Tomcat7.0.52 (支持Websocket协议)
Spring4.0.26 (支持Websocket)
web.xml(配置了前端自动优化HtmlCompressor和Druid监控),自动优化会影响Websocket js脚本,后面会讲
=================================================
配置步骤:
1. 引入Spring相关Jar,特别需要下面这两个
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.0.6.RELEASE</version>
</dependency>
2. 编写WebSocketConfig implements WebSocketConfigurer
WebSocketConfig.java
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.EnableWebMvc;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
- 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.handler.TextWebSocketHandler;
- import cn.com.ship.message.handler.ChatMessageHandler;
- import cn.com.ship.message.handler.TextMessageHandler;
- @Configuration
- <strong><span style="color: #ff0000;">//@EnableWebMvc//这个标注可以不加,如果有加,要</span></strong><strong><span style="color: #ff0000;"><strong><span style="color: #ff0000;">extends WebMvcConfigurerAdapter</span></strong></span></strong>
- @EnableWebSocket
- public class WebSocketConfig implements WebSocketConfigurer {
- @Override
- public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
- registry.addHandler(chatMessageHandler(),"/websocket/chatMessageServer.do").addInterceptors(new ChatHandshakeInterceptor());
- registry.addHandler(chatMessageHandler(), "/sockjs/chatMessageServer.do").addInterceptors(new ChatHandshakeInterceptor()).withSockJS();
- }
- @Bean
- public TextWebSocketHandler chatMessageHandler(){
- return new ChatMessageHandler();
- }
- }
3. 编写ChatMessageHandler extends TextWebSocketHandler
ChatMessageHandler.java
- import java.io.IOException;
- import java.util.ArrayList;
- import org.apache.log4j.Logger;
- 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 cn.com.ship.message.common.Constants;
- import cn.com.ship.message.common.MessageCriteria;
- public class ChatMessageHandler extends TextWebSocketHandler{
- private static final ArrayList<WebSocketSession> users;//这个会出现性能问题,最好用Map来存储,key用userid
- private static Logger logger = Logger.getLogger(ChatMessageHandler.class);
- static {
- users = new ArrayList<WebSocketSession>();
- }
- public ChatMessageHandler() {
- // TODO Auto-generated constructor stub
- }
- /**
- * 连接成功时候,会触发UI上onopen方法
- */
- @Override
- public void afterConnectionEstablished(WebSocketSession session) throws Exception {
- System.out.println("connect to the websocket success......");
- users.add(session);
- //这块会实现自己业务,比如,当用户登录后,会把离线消息推送给用户
- //TextMessage returnMessage = new TextMessage("你将收到的离线");
- //session.sendMessage(returnMessage);
- }
- /**
- * 在UI在用js调用websocket.send()时候,会调用该方法
- */
- @Override
- protected void handleTextMessage(WebSocketSession session,
- TextMessage message) throws Exception {
- super.handleTextMessage(session, message);
- }
- /**
- * 给某个用户发送消息
- *
- * @param userName
- * @param message
- */
- public void sendMessageToUser(String userName, TextMessage message) {
- for (WebSocketSession user : users) {
- if (user.getAttributes().get(Constants.WEBSOCKET_USERNAME).equals(userName)) {
- try {
- if (user.isOpen()) {
- user.sendMessage(message);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- }
- }
- }
- /**
- * 给所有在线用户发送消息
- *
- * @param message
- */
- public void sendMessageToUsers(TextMessage message) {
- for (WebSocketSession user : users) {
- try {
- if (user.isOpen()) {
- user.sendMessage(message);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
- if(session.isOpen()){
- session.close();
- }
- logger.debug("websocket connection closed......");
- users.remove(session);
- }
- @Override
- public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
- logger.debug("websocket connection closed......");
- users.remove(session);
- }
- @Override
- public boolean supportsPartialMessages() {
- return false;
- }
- }
4. 编写websocket握手拦截器ChatHandshakeInterceptor extends HttpSessionHandshakeInterceptor
ChatHandshakeInterceptor.java
- package cn.com.ship.message.websocket;
- import java.util.Map;
- import javax.servlet.http.HttpSession;
- import org.springframework.http.server.ServerHttpRequest;
- import org.springframework.http.server.ServerHttpResponse;
- import org.springframework.http.server.ServletServerHttpRequest;
- import org.springframework.web.socket.WebSocketHandler;
- import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
- import cn.com.ship.message.common.Constants;
- public class ChatHandshakeInterceptor extends HttpSessionHandshakeInterceptor {
- @Override
- public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
- System.out.println("Before Handshake");
- if (request instanceof ServletServerHttpRequest) {
- ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
- HttpSession session = servletRequest.getServletRequest().getSession(false);
- if (session != null) {
- //使用userName区分WebSocketHandler,以便定向发送消息
- String userName = (String) session.getAttribute(Constants.SESSION_USERNAME);
- if (userName==null) {
- userName="default-system";
- }
- attributes.put(Constants.WEBSOCKET_USERNAME,userName);
- }
- }
- return super.beforeHandshake(request, response, wsHandler, attributes);
- }
- @Override
- public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) {
- System.out.println("After Handshake");
- super.afterHandshake(request, response, wsHandler, ex);
- }
- }
4. 重点在Spring mvc相关配置(经常出现问题就是:中文乱码,如果是用ajax交互)
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
- xmlns:tool="http://www.springframework.org/schema/tool" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:task="http://www.springframework.org/schema/task"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:websocket="http://www.springframework.org/schema/websocket"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/jee
- http://www.springframework.org/schema/jee/spring-jee.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util.xsd
- http://www.springframework.org/schema/tool
- http://www.springframework.org/schema/tool/spring-tool.xsd
- http://www.springframework.org/schema/task
- http://www.springframework.org/schema/task/spring-task.xsd
- http://www.springframework.org/schema/websocket
- http://www.springframework.org/schema/websocket/spring-websocket-4.1.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
- <!-- 这个bean要放在context:component-scan这个前面,不然会出现中文乱码 -->
- <bean
- class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
- <property name="messageConverters">
- <list>
- <ref bean="stringHttpMessageConverter" />
- <ref bean="byteArrayHttpMessageConverter" />
- <ref bean="jsonHttpMessageConverter" />
- <ref bean="jsonHttpMessageConverter4JS" />
- </list>
- </property>
- </bean>
- <!-- 启动SpringMVC Controller的注解功能,完成请求和注解POJO的映射 -->
- <context:component-scan base-package="cn.com.ship.*.**.controller" />
- <!-- websocket相关扫描,主要扫描:WebSocketConfig.java 这个类路径 -->
- <context:component-scan base-package="cn.com.ship.message.websocket"/>
- <!-- 下面标签可以不加 等价于所有component-scan-->
- <context:annotation-config />
- <!-- 这个重点,标注必须加,websocket用到-->
- <mvc:annotation-driven/>
- <bean id="byteArrayHttpMessageConverter"
- class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
- <bean id="stringHttpMessageConverter"
- class="org.springframework.http.converter.StringHttpMessageConverter">
- <property name="supportedMediaTypes">
- <list>
- <value>text/plain;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- <bean id="jsonHttpMessageConverter"
- class="cn.com.ship.external.spring.converter.json.MappingJackson2HttpMessageConverter">
- <property name="objectMapper">
- <bean class="com.fasterxml.jackson.databind.ObjectMapper">
- <!--对属性值为null的不序列化反序列化-->
- <property name="serializationInclusion">
- <util:constant static-field="com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL"/>
- </property>
- </bean>
- </property>
- <property name="supportedMediaTypes">
- <list>
- <value>application/json</value>
- </list>
- </property>
- </bean>
- <bean id="jsonHttpMessageConverter4JS"
- class="cn.com.ship.external.spring.converter.json.MappingJackson2HttpMessageConverter">
- <property name="objectMapper">
- <bean class="com.fasterxml.jackson.databind.ObjectMapper">
- <property name="dateFormat">
- <bean class="java.text.SimpleDateFormat">
- <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
- </bean>
- </property>
- <!--对属性值为null的不序列化反序列化-->
- <property name="serializationInclusion">
- <util:constant static-field="com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL"/>
- </property>
- </bean>
- </property>
- <property name="supportedMediaTypes">
- <list>
- <value>text/json</value>
- </list>
- </property>
- </bean>
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.UrlBasedViewResolver">
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView" />
- <property name="prefix" value="/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <!-- 文件上传配置 -->
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
- p:defaultEncoding="utf-8">
- <property name="maxUploadSize" value="1024000000" />
- <property name="resolveLazily" value="true" />
- </bean>
- </beans>
注意:MappingJackson2HttpMessageConverter.java,来自Spring代码,并且修改了一点点,这个找到附件位置下载
5. jsp相关Websocket脚本编写
ws.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <html>
- <head>
- <title>Java API for WebSocket (JSR-356)</title>
- </head>
- <body>
- <script type="text/javascript" src="http://localhost:8080/ship/js/jquery/jquery.min.js"></script>
- <script type="text/javascript" src="http://localhost:8080/ship/js/sockjs-0.3.4.min.js"></script>
- <script type="text/javascript">
- var websocket = null;
- if ('WebSocket' in window) {
- websocket = new WebSocket("ws://localhost:8080/ship/webSocketServer.do");
- }
- else if ('MozWebSocket' in window) {
- websocket = new MozWebSocket("ws://localhost:8080/ship/webSocketServer.do");
- }
- else {
- websocket = new SockJS("http://localhost:8080/ship/sockjs/webSocketServer.do");
- }
- websocket.onopen = onOpen;
- websocket.onmessage = onMessage;
- websocket.onerror = onError;
- websocket.onclose = onClose;
- function onOpen(openEvt) {
- //alert(openEvt.Data);
- }
- function onMessage(evt) {
- alert(evt.data);
- }
- function onError() {}
- function onClose() {}
- function doSend() {
- if (websocket.readyState == websocket.OPEN) {
- var msg = document.getElementById("inputMsg").value;
- websocket.send(msg);//调用后台handleTextMessage方法
- alert("发送成功!");
- } else {
- alert("连接失败!");
- }
- }
- </script>
- 请输入:<textarea rows="5" cols="10" id="inputMsg" name="inputMsg"></textarea>
- <button onclick="doSend();">发送</button>
- </body>
- </html>
login.jsp
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Java API for WebSocket (JSR-356)</title>
- </head>
- <body>
- <!-ship是我的项目名-->
- <form action="/ship/websocket/login.do">
- 登录名:<input type="text" name="username"/>
- <input type="submit" value="登录"/>
- </form>
- </body>
- </html>
5. 调用端Controller编写 WebsocketController.java
- package cn.com.ship.websocket.controller;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- @Controller
- public class WebsocketController {
- @Bean//这个注解会从Spring容器拿出Bean
- public InfoHandler infoHandler() {
- return new InfoHandler();
- }
- @RequestMapping("/websocket/login")
- public void login(HttpServletRequest request, HttpServletResponse response) throws Exception {
- String username = request.getParameter("username");
- HttpSession session = request.getSession(false);
- session.setAttribute(Constants.SESSION_USERNAME, username);
- response.sendRedirect("/ship/websocket/ws.jsp");
- }
- @RequestMapping("/websocket/send")
- @ResponseBody
- public String send(HttpServletRequest request) {
- String username = request.getParameter("username");
- infoHandler().sendMessageToUser(username, new TextMessage("你好,测试!!!!"));
- return null;
- }
- }
6. 测试Websocket是否连接成功
先在login.jsp上随便输入用户名,然后WebsocketController处理完请求,会重定向到ws.jsp
如果后台打印出现消息,证明Websocket连接成功(前两名消息是在ChatHandshakeInterceptor.java,最后一句是在ChatMessageHandler.java)
Before Handshake
After Handshake
connect to the websocket success......
相关推荐
6. **Spring Boot**:虽然Spring 4.0.6本身不包含Spring Boot,但这个版本仍可以与Spring Boot 1.x版本很好地配合,简化Spring应用程序的启动和配置。 7. **文档与资源**:在提供的压缩包中,除了jar文件,还有...
本文将详细解析"spring4.0.6+mybatis3.2.7整合包"的整合过程及其相关知识点。 首先,Spring 4.0.6是Spring框架的一个稳定版本,它提供了强大的依赖注入(DI)和面向切面编程(AOP)功能,使得开发者能够更好地管理...
自己参考了官方的Spring4集成WebSocket的Demo。编写的一个Spring4集成WebSocket的例子(no-maven).环境Eclipse(indigo)+jdk7+tomcat7+Spring4.0.6+SpringMVC. 希望对初次接触WebSocket的同学有所帮助。
采用hibernate 4.3.6,spring 4.0.6 ,实现jpa配置,简单易懂明了。压缩文件中包含jpa依赖的hibernate包,如果下载者希望运行,需要自己写实体类。压缩包中含有精简的配置。
"Spring4.0.6+Struts2.3.16.3整合架包下载" 提到的是这两个框架的特定版本,Spring 4.0.6 是 Spring 框架的一个稳定版本,它带来了许多改进和优化,比如对 Java 8 的支持、更强大的类型安全的配置以及对 Servlet 3.0...
spring4.x中的jar包下载,spring4.0.6下载,spring最新稳定版jar包下载 http://maven.springframework.org/release/org/springframework/spring/ 这个链接中有各种稳定版的jar包下载 目前官网上大部分都要maven下载
这个压缩包"hibernate4.3.5+spring4.0.6+struts2"提供了这三个框架的最新版本,适用于构建基于Java的企业级应用。以下是关于这三个框架及其整合的知识点详解: **Spring框架**(4.0.6版): 1. **依赖注入...
本项目采用的是Spring MVC 4.0.6、Spring 4.0.6和Hibernate 4.3.6的版本组合,这在当时是一个较为稳定的配置,适合构建中大型企业级应用。 Spring MVC是Spring框架的一部分,专门用于处理Web请求。它通过...
这个项目使用了Maven进行模块化管理,版本分别对应于Spring 4.0.6、SpringMVC 4.0.6和Mybatis 3.2.7,日志处理则采用了log4j 2.1。下面将详细讲解这些技术及其在项目中的应用。 **Maven多模块项目** Maven是一个...
Spring Framework,作为Java领域最广泛应用的轻量级框架之一,自诞生以来就以其模块化、易用性和强大的功能赢得了开发者们的青睐。4.0.6.RELEASE是Spring框架的一个稳定版本,它在4.0系列中提供了一系列增强和改进,...
1. 添加依赖:在项目中引入Spring、Hibernate、JPA和DBCP2的相关库文件,例如lib-spring 4.0.6、Hibernate 4.3.5和JPA的依赖。 2. 配置数据源:在Spring的配置文件中,使用DBCP2的数据源bean,设置数据库连接参数如...
4.0.6.RELEASE 版本是 Spring 的一个重要里程碑,它在之前版本的基础上进行了多方面的改进和增强,为开发者带来了更稳定、更高效的功能。 Spring 框架的核心特性包括依赖注入(Dependency Injection,DI)、面向切...
本文将详细讲解如何结合Spring 4.0.6版本和Quartz 2.2.3版本来实现一个集群示例,帮助你理解和掌握相关知识点。 首先,让我们了解Spring 4.0.6。这是Spring框架的一个稳定版本,提供了许多增强功能和性能优化。...
struts2.3.24,hibernate4.3.6,spring4.0.6,spring security3.2.4jar包的整合,如果不需要spring security,可以将带有spring secuirity的包全删掉
spring framework 4.0.6的完整API参考手册。制作成chm方便离线查阅,是开发过程中必不可少的东西。
包括spring-aop、spring-bean、springcontext等65个springboot必备的jar包,可以在Web项目下的lib中直接粘贴,即可在项目中使用SpringBoot,版本4.0.6。
本篇文章将详细探讨Spring Framework 4.0.6.RELEASE版本的源码,帮助开发者更好地理解和运用这一版本的核心功能。 1. **模块架构** Spring Framework 4.0.6由多个模块组成,包括Core Container(核心容器)、Data ...
spring-tx-4.0.6.RELEASE.jar spring-tx-4.0.6.RELEASE.jar
本文将详细介绍如何利用Spring 4.0.6及以上版本和WebSocket技术来构建一个消息推送系统。\n\n首先,我们需要了解WebSocket的基本概念。WebSocket是一种在客户端和服务器之间建立长连接的协议,允许双方进行全双工...