当前位置: 首页 > article >正文

springboot websocket 服务端

在Spring Boot中使用WebSocket实现服务端和Java客户端的实时通信,可以分为几个步骤来完成。这里将详细介绍服务端和Java客户端的具体实现。
服务端设置
添加依赖: 在pom.xml文件中添加Spring WebSocket的依赖。

   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-websocket</artifactId>
   </dependency>
   

配置WebSocket处理器: 创建一个配置类来注册WebSocket的Endpoint。

   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;
   import org.springframework.web.socket.WebSocketHandler;
   import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

   @Configuration
   @EnableWebSocket
   public class WebSocketConfig implements WebSocketConfigurer {

       private final WebSocketHandler webSocketHandler;

       public WebSocketConfig(WebSocketHandler webSocketHandler) {
           this.webSocketHandler = webSocketHandler;
       }

       @Override
       public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
           registry.addHandler(webSocketHandler, "/ws/chat")
                   .addInterceptors(new HttpSessionHandshakeInterceptor())
                   .setAllowedOrigins("*");
       }
   }
   
   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;
   import org.springframework.web.socket.WebSocketHandler;
   import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

   @Configuration
   @EnableWebSocket
   public class WebSocketConfig implements WebSocketConfigurer {

       private final WebSocketHandler webSocketHandler;

       public WebSocketConfig(WebSocketHandler webSocketHandler) {
           this.webSocketHandler = webSocketHandler;
       }

       @Override
       public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
           registry.addHandler(webSocketHandler, "/ws/chat")
                   .addInterceptors(new HttpSessionHandshakeInterceptor())
                   .setAllowedOrigins("*");
       }
   }
   


http://www.kler.cn/news/294573.html

相关文章:

  • 用 Python 编写桌面时钟程序
  • MATLAB绘图基础5:MATLAB数据导入
  • Redis总结,是什么,干什么,怎么利用?
  • element table 表格 span-method 某一列进行相同合并 支持树结构表格
  • 数据结构基础之《(2)—对数器》
  • 海外直播对网速、带宽、安全的要求
  • Docker容器创建时,无法访问镜像源:Could not connect to archive.ubuntu.com:80
  • C语言操作符详解1(含进制转换,原反补码)
  • 消息队列 think-queue tp5.0
  • 使用Docker启动Redis容器并映射端口
  • ChatGPT3.5/4.0新手使用手册,国内中文版使用教程
  • 记录 vue-router 跳转到第一个有权限的菜单的实现方式
  • 【大模型开发】传统向量模型 vs 重排序模型:原理、实现与应用
  • 欺诈文本分类检测(十二):模型导出与部署
  • 八大排序原来也不过如此
  • 【软件逆向】第27课,软件逆向安全工程师之(二)寄存器寻址,每天5分钟学习逆向吧!
  • 【MIT 6.5840/6.824】In Search of an Understandable Consensus Algorithm 学习笔记
  • 如何使用useMemo来优化React组件的性能?
  • 7、关于LoFTR
  • 三维布尔运算对不规范几何数据的兼容处理
  • Linux 中常用的 Vim 命令大全
  • [OpenCV] 数字图像处理 C++ 学习——13Canny边缘检测 附完整代码
  • 828华为云征文 | Flexus X 实例服务器网络性能深度评测
  • 使用PowerShell导出Exchange邮箱大小
  • docker-network
  • GatewayWorker框架的详解和应用
  • [建模已更新]2024数学建模国赛高教社杯A题:“板凳龙” 闹元宵 思路代码文章助攻手把手保姆级
  • Hive整合MySQL
  • tabBar设置底部菜单选项以及iconfont图标
  • Java学习第七天