Spring实现WebSocket通信
WebSocket是两个应用之间全双工的通信通道。WebSocket最常见的应用场景是实现服务器和基于浏览器的应用之间的通信。
浏览器中的javascript客户端开启一个到服务器的连接,服务器通过这个连接发送更新给浏览器。相比轮询服务端以查找更新的
方案,这种技术更加高效。
下面将通过两种方式(XML配置和Java类配置)实现WebSocket的简单应用。
需要说明的是,由于本人浏览器无法支持WebSocket(这也是WebSocket令人遗憾的地方),所以使用WebSocket的备用方案
SockJS。SockJS会优先选用WebSocket,但是如果WebSocket不可用的话,它将会从其他方案中挑选最优方案。
不论哪种方案,首先要在maven项目中添加相关依赖:
<!-- WebSocket依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<!-- 处理json数据 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
注意,后两个依赖不能忘记,不然会在通信时出错
一、通过XML配置
在之前的项目中,我们首先创建example.websocket包,用于存放我们定义的处理器类。在该包中创建我们的
WebSocket处理器类MyHandler:
package example.websocket; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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; /**
* webSocket处理器
*/
public class MyHandler extends TextWebSocketHandler {
private static final Logger log = LoggerFactory.getLogger(MyHandler.class); /**
* 处理接收的信息行为
* @param session
* @param message
* @throws Exception
*/
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
log.info("Received message: " + message.getPayload());
session.sendMessage(new TextMessage("Server has received your message"));
} /**
* 处理建立连接后的事件
* @param session
* @throws Exception
*/
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
log.info("Connection established");
} /**
* 处理连接关闭事件
* @param session
* @param status
* @throws Exception
*/
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
log.info("Connection closed. Status: " + status);
}
}
接下来我们要在DispatcherServlet中配置我们的WebSocket:
<websocket:handlers>
<websocket:mapping path="/myHandler" handler="myHandler"/>
<websocket:sockjs/>
</websocket:handlers>
<bean id="myHandler" class="example.websocket.MyHandler"/>
注意,这段代码应放入dispatcher-servlet.xml中,其中<websocket:sockjs/>标签配置了SockJS
接下来我们创建一个新的Controller类用于加载一个独立的视图:
package example.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping(value = "websocket")
public class CommunicationController { @RequestMapping(method = RequestMethod.GET)
public ModelAndView webSocketView() {
ModelAndView modelAndView = new ModelAndView("webSocketClient");
return modelAndView;
}
}
下面我们来编写一个简易的前端代码,创建webSocketClient.jsp文件,需要注意,在使用SockJS之前,需要引入相应的库,
具体参见https://github.com/sockjs/sockjs-client,这里借用了其他博主的代码https://blog.csdn.net/dadiyang/article/details/83715569
<%--
Created by IntelliJ IDEA.
User: asus1
Date: 2019/1/24
Time: 21:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>webSocket客户端</title>
</head>
<body>
<h1>Welcome!</h1>
<ul id="ul">
</ul>
<script type="text/javascript" src="${pageContext.request.contextPath}/statics/js/sockjs-1.0.0.min.js"></script>
<script>
var url = '/myHandler';
var sock = new SockJS(url); sock.onopen = function (ev) {
console.log("opening");
sayHey();
};
sock.onmessage = function (ev) {
console.log(ev.data);
var li = document.createElement("li");
li.innerText = ev.data;
document.getElementById("ul").appendChild(li);
setTimeout(sayHey, 2000);
};
sock.onclose = function (ev) {
console.log("closed");
};
function sayHey() {
console.log("sending 'Hey guy!'");
sock.send("Hey guy!");
};
</script>
</body>
</html>
运行项目结果:




二、通过java类配置
完全依照其他博主的方法重新创建了一个项目,整个项目通过java类进行配置
参考资料:https://blog.csdn.net/dadiyang/article/details/83715569
《Spring 实战(第4版)》
下面直接粘代码:
配置类:
RootConfig:
package com.example.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration
@ComponentScan(basePackages = {"com.example"},
excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)
})
public class RootConfig {
}
WebConfig:
package com.example.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver; /**
* WebMvcConfigurerAdapter在新版本中已被弃用
* 可以通过实现WebMvcConfigurer或者拓展WebMvcConfigurerSupport
* 替代WebMvcConfigurerAdapter的拓展
*/
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.example"})
public class WebConfig implements WebMvcConfigurer { /**
* 配置jsp视图解析器
* @param registry
*/
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/", ".jsp");
} /**
* 配置静态资源的处理
* @param configurer
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
WebSocketConfig:
package com.example.config; import com.example.websocket.MyHandler;
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; @Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
webSocketHandlerRegistry.addHandler(myHandler(), "/myHandler").withSockJS();
} @Bean
public MyHandler myHandler() {
return new MyHandler();
}
}
WebApp初始化类:
package com.example.webappinit; import com.example.config.RootConfig;
import com.example.config.WebConfig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class wsWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {RootConfig.class};
} @Override
protected String[] getServletMappings() {
return new String[] {"/"};
} @Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {WebConfig.class};
}
}
其他代码与XML配置一样
运行项目结果:




Spring实现WebSocket通信的更多相关文章
- spring整合websocket通信
		1. maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ... 
- spring boot websocket  stomp 实现广播通信和一对一通信聊天
		一.前言 玩.net的时候,在asp.net下有一个叫 SignalR 的框架,可以在ASP .NET的Web项目中实现实时通信.刚接触java寻找相关替代品,发现 java 体系中有一套基于stom ... 
- 【Java Web开发学习】Spring MVC整合WebSocket通信
		Spring MVC整合WebSocket通信 目录 ========================================================================= ... 
- Spring Boot WebSocket从入门到放弃
		在构建Spring boot项目时已经提供webSocket依赖的勾选.webSocket是TCP之上的一个非常薄的轻量级层 ,webSocket主要的应用场景离不开即时通讯与消息推送,但只要应用程序 ... 
- Spring之WebSocket网页聊天以及服务器推送
		Spring之WebSocket网页聊天以及服务器推送 转自:http://www.xdemo.org/spring-websocket-comet/ /Springframework /Spring ... 
- spring对websocket的集成和使用
		WebSocket是HTML5提出的一个用于通信的协议规范,该协议通过一个握手机制,在客户端和服务端之间建立一个类似于TCP的连接,从而方便客户端和服务端之间的通信. WebSocket协议本质上是一 ... 
- Spring整合WebSocket
		WebSocket,干什么用的?我们有了HTTP,为什么还要用WebSocket?很多同学都会有这样的疑问.我们先来看一个场景,大家的手机里都有微信,在微信中,只要有新的消息,这个联系人的前面就会有一 ... 
- C#(SuperWebSocket)与websocket通信
		原文:C#(SuperWebSocket)与websocket通信 客户端代码 点击可以查看一些关于websocket的介绍 <!DOCTYPE html> <html> &l ... 
- 玩转spring boot——websocket
		前言 QQ这类即时通讯工具多数是以桌面应用的方式存在.在没有websocket出现之前,如果开发一个网页版的即时通讯应用,则需要定时刷新页面或定时调用ajax请求,这无疑会加大服务器的负载和增加了客户 ... 
随机推荐
- FairyGUI TextField
			记录一个在使用FairyGUI的TextField时遇到的坑. TextField有一个文本模板功能,可以实现类似占位符的功能,如:{ number = 0 },然后我们可以在脚本中修改number的 ... 
- win7有多条隧道适配器(isatap、teredo、6to4)的原因及关闭方法
			问题:sdp协商时,带有IPV6的信息,需要将IPV6相关信息去掉 原因:网卡启用了ipv6通道 解决:关闭IPv6数据接口 netsh interface isatap set state disa ... 
- java的lamda表达式
			Java8才支持lamda表达式 lamda是一中函数式编程语言 通过实现模式是匿名内部类 java使用内部类实现接口 首先定义一个接口 @FunctionalInterfaceinterface ... 
- style.left  offsetLeft  offsetwidth clientLeft  clientWidth  scrollLeft   scrollWidth
			<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ... 
- speech
			1.李开复:一个人的成功,15%靠专业知识,其余15%人际沟通,公众演讲,以及影响他人的能力 2.演讲是一门遗憾的艺术 3.没有准备就等于准备失败 4.追求完美,就是在追求完蛋 5.宁可千日无机会,不 ... 
- MySQL卸载安装8.0.*
			一.卸载 1. 本人安装目录在C盘Program Files文件中(删除MySQL文件夹) 2. 找到C盘Program Data隐藏文件夹中的MySQL并删除(删除提示MySQL在运行的话可以去任务 ... 
- Docker跨主机网络联通之etcd实现
			搭建ETCD集群 查看NODE1机器IP,并启动ETCD ubuntu@docker-node1:~$ ifconfig eth0 eth0: flags=4163<UP,BROADCAST,R ... 
- Win10提示“因为文件共享不安全,所以你不能连接到文件共享”如何处理
			在使用Windows10 1803版本系统连接CentOS6.5下搭建的Samba服务时,发现打开共享文件会遇到以下提示: 其实,该问题是Win10版本不兼容导致的.微软官方说明:https://go ... 
- HTML5本地存储之本地数据库篇
			<!DOCTYPE HTML> <html> <head> <meta charset="utf-8" /> <title&g ... 
- rematch:当你受不了redux繁琐写法的时候,是时候了解一波rematch了
			前言: 前段时间学习完react后,刚好就接到公司一个react项目的迭代,顺便巩固一下前段时间的学习成果.项目使用的是redux+react-router,将所有的数据都放在redux中,异步处理数 ... 
