一、背景

  我们都知道 http 协议只能浏览器单方面向服务器发起请求获得响应,服务器不能主动向浏览器推送消息。想要实现浏览器的主动推送有两种主流实现方式:

  • 轮询:缺点很多,但是实现简单
  • websocket:在浏览器和服务器之间建立 tcp 连接,实现全双工通信

  springboot 使用 websocket 有两种方式,一种是实现简单的 websocket,另外一种是实现STOMP协议。这一篇实现简单的 websocket,STOMP 下一篇在讲。

注意:如下都是针对使用 springboot 内置容器

二、实现

1、依赖引入

  要使用 websocket 关键是@ServerEndpoint这个注解,该注解是 javaee 标准中的注解,tomcat7 及以上已经实现了,如果使用传统方法将 war 包部署到 tomcat 中,只需要引入如下 javaee 标准依赖即可:

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>

如使用 springboot 内置容器,无需引入,springboot 已经做了包含。我们只需引入如下依赖即可:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>1.5.3.RELEASE</version>
<type>pom</type>
</dependency>

2、注入 Bean

  首先注入一个ServerEndpointExporterBean,该 Bean 会自动注册使用@ServerEndpoint 注解申明的 websocket endpoint。代码如下:

@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
}

3、申明 endpoint

  建立MyWebSocket.java类,在该类中处理 websocket 逻辑

@ServerEndpoint(value = "/websocket") //接受websocket请求路径
@Component //注册到spring容器中
public class MyWebSocket { //保存所有在线socket连接
private static Map<String,MyWebSocket> webSocketMap = new LinkedHashMap<>(); //记录当前在线数目
private static int count=0; //当前连接(每个websocket连入都会创建一个MyWebSocket实例
private Session session; private Logger log = LoggerFactory.getLogger(this.getClass());
//处理连接建立
@OnOpen
public void onOpen(Session session){
this.session=session;
webSocketMap.put(session.getId(),this);
addCount();
log.info("新的连接加入:{}",session.getId());
} //接受消息
@OnMessage
public void onMessage(String message,Session session){
log.info("收到客户端{}消息:{}",session.getId(),message);
try{
this.sendMessage("收到消息:"+message);
}catch (Exception e){
e.printStackTrace();
}
} //处理错误
@OnError
public void onError(Throwable error,Session session){
log.info("发生错误{},{}",session.getId(),error.getMessage());
} //处理连接关闭
@OnClose
public void onClose(){
webSocketMap.remove(this.session.getId());
reduceCount();
log.info("连接关闭:{}",this.session.getId());
} //群发消息 //发送消息
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
} //广播消息
public static void broadcast(){
MyWebSocket.webSocketMap.forEach((k,v)->{
try{
v.sendMessage("这是一条测试广播");
}catch (Exception e){
}
});
} //获取在线连接数目
public static int getCount(){
return count;
} //操作count,使用synchronized确保线程安全
public static synchronized void addCount(){
MyWebSocket.count++;
} public static synchronized void reduceCount(){
MyWebSocket.count--;
}
}

4、客户的实现

  客户端使用 h5 原生 websocket,部分浏览器可能不支持。代码如下:

<html>
<head>
<title>websocket测试</title>
<meta charset="utf-8" />
</head> <body>
<button onclick="sendMessage()">测试</button>
<script>
let socket = new WebSocket("ws://localhost:8080/websocket");
socket.onerror = err => {
console.log(err);
};
socket.onopen = event => {
console.log(event);
};
socket.onmessage = mess => {
console.log(mess);
};
socket.onclose = () => {
console.log("连接关闭");
}; function sendMessage() {
if (socket.readyState === 1) socket.send("这是一个测试数据");
else alert("尚未建立websocket连接");
}
</script>
</body>
</html>

三、测试

  建立一个 controller 测试群发,代码如下:

@RestController
public class HomeController { @GetMapping("/broadcast")
public void broadcast(){
MyWebSocket.broadcast();
}
}

然后打开上面的 html,可以看到浏览器和服务器都输出连接成功的信息:

浏览器:
Event {isTrusted: true, type: "open", target: WebSocket, currentTarget: WebSocket, eventPhase: 2, …} 服务端:
2018-08-01 14:05:34.727 INFO 12708 --- [nio-8080-exec-1] com.fxb.h5websocket.MyWebSocket : 新的连接加入:0

点击测试按钮,可在服务端看到如下输出:

2018-08-01 15:00:34.644  INFO 12708 --- [nio-8080-exec-6] com.fxb.h5websocket.MyWebSocket          : 收到客户端2消息:这是一个测试数据

再次打开 html 页面,这样就有两个 websocket 客户端,然后在浏览器访问localhost:8080/broadcast测试群发功能,每个客户端都会输出如下信息:

MessageEvent {isTrusted: true, data: "这是一条测试广播", origin: "ws://localhost:8080", lastEventId: "", source: null, …}

  源码可在 github 下载 上下载,记得点赞,star 哦

本文原创发布于:https://www.tapme.top/blog/detail/2018-08-25-10-38

你知道在springboot中如何使用WebSocket吗的更多相关文章

  1. Springboot中的事件Event

    事件Event作为一种常用的线程通讯工具,在Springboot中可以方便地提供开发者进行线程交互. 1.事件定义 1 import org.springframework.context.Appli ...

  2. springBoot于tomcat7搭建websocket服务

    最近在项目中需要服务端进行推送服务,于是决定使用webSocket进行双向通讯.刚开始觉得应该没问题,参考网上的博客进行开发配置.我们的开发环境使用的是springBoot.tomcat7.servl ...

  3. springboot中redis取缓存类型转换异常

    异常如下: [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested ...

  4. SpringBoot中yaml配置对象

    转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...

  5. 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧

    做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...

  6. springboot中swaggerUI的使用

    demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...

  7. spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架

    前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...

  8. 由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

  9. Springboot中使用AOP统一处理Web请求日志

    title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...

随机推荐

  1. 低性能3张图片轮播React组件

    import React from 'react'; import {getSwipeWay} from '../utils/swipe'; class Carousel extends React. ...

  2. 使用iptables缓解DDOS及CC攻击

    使用iptables缓解DDOS及CC攻击 LINUX  追马  7个月前 (02-09)  465浏览  0评论 缓解DDOS攻击 防止SYN攻击,轻量级预防 iptables -N syn-flo ...

  3. 洛谷P1000 超级玛丽游戏

    这道题很简单,就是原样输出,只不过写起来有点恶心!!! code: #include<stdio.h> int main() { printf( " ********\n&quo ...

  4. 十:MYSQL中的事务

    前言: 因为没有多少时间和精力,目前无法深入研究数据库中的事务,比如 但是,对于事务的一些基本知识,还是需要牢牢掌握的,做到了解事务的基本常识,在实际开发中能够理解各个持久层框架对事务的处理 一:是么 ...

  5. 初中级PHP面试基础汇总

    这是我整理的一套面试题,老铁们看看就当复习了哦 相关PHP面试题 搞定PHP面试 - 函数知识点整理 php 面试题目整理 PHP面试整理 PHP面试 概述 感觉现在发面试题有些冷门,就跟昨天德国那场 ...

  6. CVS update常用技巧

    常用的命令有 cvs update 全部更新 cvs update path/to/file 来更新某一个文件 cvs update -dP 意为删除空目录创建新目录 cvs -f -n update ...

  7. Codeforces Round #439 (Div. 2) E. The Untended Antiquity

    E. The Untended Antiquity 题目链接http://codeforces.com/contest/869/problem/E 解题心得: 1.1,x1,y1,x2,y2 以(x1 ...

  8. 【02】你是如何理解 HTML 语义化的,有什么好处

    [02]你是如何理解 HTML 语义化的   01,语义化,就是通过HTML标签来表示页面包含的信息. 02,其中有HTML标签的语义化和CSS命名的语义化. 03,HTML标签语义化的的含义是:   ...

  9. selenium 切换窗口的几种方法

    第一种方法: 使用场景: 打开多个窗口,需要定位到新打开的窗口 使用方法: # 获取打开的多个窗口句柄 windows = driver.window_handles # 切换到当前最新打开的窗口 d ...

  10. python接口自动化测试二十七:密码MD5加密

    # MD5加密 # 由于MD5模块在python3中被移除# 在python3中使用hashlib模块进行md5操作import hashlib def MD5(str): # 创建md5对象 hl ...