webSocket介绍与使用 spring boot 集成 websocket教程 java
创始人
2024-11-16 04:33:45
0

目录

一、WebSocket简介

二、WebSocket优缺点

三、spring boot集成webSocket

四、验证


一、WebSocket简介

        WebSocket建立在TCP协议上,是一种在单个TCP连接上进行全双工通信的协议。

        HTML5里面提出了WebSocket标准,目的是让服务器具有“主送”向浏览器推送信息的能力,它允许客户端和服务器之间建立持久连接,实现实时的数据传输功能。

WebSocket通讯分为两个阶段:

1.握手阶段

        客户端通过HTTP请求发起握手请求,请求头包含一些特殊字段,如pgrade:websocket和Connection:Upgrade以及其他的WebSocket相关字段。

服务器收到客户端的WebSocket握手请求后,进行协议升级,将HTTP升级为WebSocket连接。服务器返回一个WebSocket握手响应,相应头中包如:Upgrade:websocket和Connection:Upgrade等WebSocket相关字段。

客户端收到服务器的WebSocket握手响应后,WebSocket连接就建立成功,客户端和服务端都可以发送和接收WebSocket消息。

2.数据交换阶段

        WebSocket建立成功后,客户端和服务器都可以通过WebSocket会话进行实时的双向数据交换。同时,客户端和服务端可以通过发送心跳数据帧来维持连接,如果在一段时间内没有收到心跳响应,则断开WebSocket连接。

二、WebSocket优缺点

1.优点

        支持双向通讯,实时性比较高,更加灵活。

        开销少,ws建立成功后,客户端、服务端进行数据交换,协议控制的数据包头部较小。http需要完整的头部信息。

        可扩展,WebSocket协议定义了扩展,用户可以扩展协议。

2.缺点

        服务端需要支持WebSocket协议。

        客户端连接数多,服务端压力大。

三、spring boot集成webSocket

1.新建spring boot项目,再pom.xml加入以下配置

     org.springframework.boot     spring-boot-starter-parent     2.1.18.RELEASE                      org.springframework.boot         spring-boot-starter-web                        org.springframework.boot         spring-boot-starter-websocket      

2.再spring boot启动类开启websocket功能

package com.example.demo;  import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.socket.config.annotation.EnableWebSocket;  //开启WebSocket @EnableWebSocket @SpringBootApplication public class DemoApplication {     public static void main(String[] args) {         SpringApplication.run(DemoApplication.class, args);     }  }

3.新建WebSoketConfig配置类

package com.example.demo.common.config.websocket;  import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter;  /**  * WebSocket配置类  */ @Configuration public class WebSocketConfig {     @Bean     public ServerEndpointExporter serverEndpointExporter() {         return new ServerEndpointExporter();     } }

4.新建WebSocketServer服务类

package com.example.demo.common.config.websocket;  import org.springframework.stereotype.Component; import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.util.concurrent.ConcurrentHashMap;  /**  * @ServerEndpoint:该注解用于暴漏外部ws的路径,类似@RequestMapping注解。例如服务端口8080,请求地址:ws://localhost:8080/myWs  * 路径上{userId}  可在onOpen连接成功方法使用@PathParam("userId") String userId接收数据  */ @ServerEndpoint("/myWs/{userId}") @Component public class WebSocketServer {     //线程安全的map,用来保存每个客户端对应的WebSocket对象     private static ConcurrentHashMap webSocketMap = new ConcurrentHashMap<>();     //单个客户端的session,通过session与对应客户端通讯     private Session session;     //用户id     private String userId;      /**      * 连接成功      * @OnOpen注解:websocket 连接成功后,触发该注解修饰的方法      * @param session      */     @OnOpen     public void onOpen(Session session, @PathParam("userId") String userId) {         this.session = session;         this.userId = userId;         if (webSocketMap.containsKey(userId)) {             webSocketMap.remove(userId);             webSocketMap.put(userId, this);         } else {             webSocketMap.put(userId, this);         }         System.out.println("连接成功");     }      /**      * 连接关闭      * @OnClose注解:websocket断开连接后,触发该注解修饰的方法      * @param session      */     @OnClose     public void onCLose(Session session) {         if (webSocketMap.containsKey(userId)) {             webSocketMap.remove(userId);         }         System.out.println("关闭连接");     }      /**      * 接收消息      * @OnMessage注解:客户端发送消息时,触发该注解声明的方法      * @param text      * @return      */     @OnMessage     public void onMessage(String text) {         System.out.println("后端接收前端web发送数据userId:" + userId + ",接收信息:" + text);         if (webSocketMap.containsKey(userId)) {             try {                 webSocketMap.get(userId).session.getBasicRemote().sendText("返回web数据userId:" + userId + ",返回消息:" + text);             } catch (IOException e) {                 e.printStackTrace();             }         }     }      /**      * 连接异常      * @OnError注解:当建立的连接出现异常后,触发该注解修饰的方法      * @param session      * @param throwable      */     @OnError     public void onError(Session session, Throwable throwable) {         System.out.println("websocket连接异常:" + throwable.getMessage());     }      /**      * 服务器给指定WebSocket客户端发送信息      * @param userId      * @param message      */     public static void sendInfo(String userId, String message) {         System.out.println("后端发送前端web数据userId:" + userId + "发送消息:" + message);         if (webSocketMap.containsKey(userId)) {             try {                 webSocketMap.get(userId).session.getBasicRemote().sendText("后端发送前端web数据userId" + userId + ",内容:" + message);             } catch (IOException e) {                 e.printStackTrace();             }         }     } }

5.新建DemoController测试类,增加接口通过webSocket给客户端发信信息

package com.example.demo.controller;  import com.example.demo.common.config.websocket.WebSocketServer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;  @RestController public class DemoController {      @RequestMapping("/demo")     public boolean demo(@RequestParam("userId") String userId, @RequestParam("message") String mesage) {         //给前端web推送数据         WebSocketServer.sendInfo(userId, mesage);         return true;     } }

四、验证

1.启动服务,访问websocket在线测试页面,与后端建立连接

2.后端显示连接成功

3.通过websocket给后端发送数据

4.后端打印接收到的数据

5.调用后端接口,通过websocket给前端推送数据

6.websocket在线测试页面收到数据

相关内容

热门资讯

透视存在!来玩app 德州 辅... 透视存在!来玩app 德州 辅助,约局吧如何查看是否有挂,教材教程(原来真的有挂)-哔哩哔哩1、约局...
透视神器!智星德州插件怎么下载... 透视神器!智星德州插件怎么下载(透视)开挂辅助工具(确实是有挂)-哔哩哔哩1、点击下载安装,智星德州...
事发当天!榕城510k破解版安... 事发当天!榕城510k破解版安卓,朱雀开心罗松辅助器,攻略方法(一直存在有挂)-哔哩哔哩1、不需要A...
透视存在!wepokerplu... 透视存在!wepokerplus透视脚本免费,wepoker辅助插件功能,方针教程(切实真的是有挂)...
透视中牌率!aapoker发牌... 透视中牌率!aapoker发牌逻辑(透视)开挂辅助挂(果然有挂)-哔哩哔哩1、金币登录送、破产送、升...
今天上午!闲来游戏辅助器脚本,... 今天上午!闲来游戏辅助器脚本,创思维激k辅助控制器,德州教程(其实是有挂)-哔哩哔哩1、上手简单,内...
透视透视!菠萝辅助器免费版的特... 透视透视!菠萝辅助器免费版的特点,hhpoker透视脚本,方针教程(本来是有挂)-哔哩哔哩1、下载好...
透视插件!wepoker免费透... 透视插件!wepoker免费透视脚本(透视)开挂辅助工具(竟然真的是有挂)-哔哩哔哩1、每一步都需要...
有消息称!胡乐辅助脚本是真的假... 有消息称!胡乐辅助脚本是真的假的,七千在线辅助,2025版教程(都是真的是有挂)-哔哩哔哩胡乐辅助脚...
透视辅助!wepoker私人局... 您好,wepoker私人局透视插件这款游戏可以开挂的,确实是有挂的,需要了解加去威信【4852750...