JavaWeb SpringMVC整合WebSocket(注解实现)
2018-02-09 16:07
701 查看
Spring4.0以后引入了对websocket的支持 。相信你已经搭建了SpringMVC的开发框架。
所以这里只给出websocket的相关依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
Java代码如下:
所以这里只给出websocket的相关依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
Java代码如下:
import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.CopyOnWriteArraySet; import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; import org.apache.log4j.Logger; /** * @ServerEndpoint 注解是一个类层次的注解,它的功能主要是将目前的类定义成一个websocket服务器端, * 注解的值将被用于监听用户连接的终端访问URL地址,客户端可以通过这个URL来连接到WebSocket服务器端 * @author */ @ServerEndpoint(value="/websocket") public class WebSocketTest { private static Logger logger = Logger.getLogger(WebSocketTest.class); //线程安全的静态变量,表示在线连接数 private static volatile int onlineCount = 0; //用来存放每个客户端对应的WebSocketTest对象,适用于同时与多个客户端通信 public static CopyOnWriteArraySet<WebSocketTest> webSocketSet = new CopyOnWriteArraySet<WebSocketTest>(); //若要实现服务端与指定客户端通信的话,可以使用Map来存放,其中Key可以为用户标识 public static ConcurrentHashMap<Session,Object> webSocketMap = new ConcurrentHashMap<Session,Object>(); //与某个客户端的连接会话,通过它实现定向推送(只推送给某个用户) private Session session; /** * 连接建立成功调用的方法 * * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据 */ @OnOpen public void onOpen(Session session){ this.session = session; webSocketSet.add(this); //加入set中 webSocketMap.put(session,this); //加入map中 addOnlineCount(); //在线数加1 System.out.println("有新连接加入!当前在线人数为" + getOnlineCount()); } /** * 连接关闭调用的方法 */ @OnClose public void onClose(Session closeSession) { webSocketSet.remove(this); //从set中删除 webSocketMap.remove(closeSession); //从map中删除 subOnlineCount(); //在线数减1 System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount()); } /** * 收到客户端消息后调用的方法 * * @param message 客户端发送过来的消息 * @param session 可选的参数 * @throws Exception */ @OnMessage public void onMessage(String message,Session mySession) throws Exception { logger.info("来自客户端的消息:" + message); //--------------群发消息(多用于聊天室场景) /* for (WebSocketTest item : webSocketSet) { try { item.sendAllMessage(message); } catch (IOException e) { e.printStackTrace(); continue; } }*/ //推送给单个客户端 for (Session session : webSocketMap.keySet()) { if (session.equals(mySession)) { WebSocketTest item = (WebSocketTest) webSocketMap.get(mySession); try { String msg="嗨,这是返回的信息"; item.sendMessage(mySession,msg); } catch (IOException e) { e.printStackTrace(); } } } } /** * 发生错误时调用 * * @param session * @param error */ @OnError public void onError(Session session, Throwable error) { logger.info("发生错误"); // error.printStackTrace(); } //给所有客户端发送信息 public void sendAllMessage(String message) throws IOException { this.session.getBasicRemote().sendText(message); } //定向发送信息 public void sendMessage(Session mySession,String message) throws IOException { synchronized(this) {try { if(mySession.isOpen()){//该session如果已被删除,则不执行发送请求,防止报错 //this.session.getBasicRemote().sendText(message); mySession.getBasicRemote().sendText(message); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static synchronized int getOnlineCount() { return onlineCount; } public static synchronized void addOnlineCount() { WebSocketTest.onlineCount++; } public static synchronized void subOnlineCount() { WebSocketTest.onlineCount--; } }值得注意的一点是,如果想要在如上的websocket中直接注入service或者dao来进行数据库操作是无法注入的。我的解决办法是新建一个专门操作数据库的类并在applicationContext.xml中进行配置。比如我的这个类叫GetDataServiceImpl,首先要在 applicationContext.xml 中加入<bean id="getDataServiceImpl" class="com.cn.service.impl.GetDataServiceImpl"></bean>在websocket中通过beanid获取该类
private GetDataServiceImpl getDataServiceImpl = (GetDataServiceImpl) ContextLoader.getCurrentWebApplicationContext().getBean("getDataServiceImpl");以上就是我参考网上相关资料后根据自己的理解在SSM框架的基础上整合的websocket代码,并且在项目中已经在使用,目前为止还没有发现什么问题。可以看到,代码其实并不多,而且使用注解方式也比较简单,容易理解。如有不妥之处,欢迎交流指正。
相关文章推荐
- java web Spring mvc 分页实现
- java注解方式(不用xml)配置web框架spring+SpringMVC
- JAVA Spring web mvc 学习 之 4:注解
- 练习搭建spring+springmvc+mybatis实现java web登陆
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
- dwz4j企业级Java Web快速开发框架(Mybatis + SpringMVC) + jUI整合应用
- java WebSocket的实现以及Spring WebSocket
- eclipse 中创建 整合springmvc+mybatis+maven 的java web项目步骤,以及maven仓库配置
- JAVA WEB搭建Spring mvc+Spring+MyBatis整合框架
- 2.1、Spring Web MVC是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职
- javaweb—SpringMVC+Spring+MyBatis整合
- MyBatis代码实例系列-05:Mybatis初步整合Spring + Spring MVC框架,实现Web请求实例
- spring springmvc hibernate整合之java web 项目
- Spring + Spring MVC + Mybatis + Swagger + javaConfig 整合项目 全注解方式
- java web Spring mvc 分页实现
- websocket的两种实现方式:1基于注解,2与spring整合
- 手把手教你搭建自己的Java Web(Android)项目(SpringMVC + Mybatis服务端,Html5 Web端, Android客户端实现)
- Java Web SpringMVC AJAX,实现页面懒加载数据
- spring+websocket整合(springMVC+spring+MyBatis即SSM框架和websocket技术的整合)
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(二)RESTful API实战笔记(接口设计及Java后端实现)