首页 > 其他分享 >Spring WebSocket中关于WebSocket配置类的注意事项

Spring WebSocket中关于WebSocket配置类的注意事项

时间:2024-06-19 15:25:24浏览次数:11  
标签:WebSocket Spring 注意事项 springframework registry import org config annotation

情况1:如果只需要进行简单的通信,不需要消息代理和STOMP协议支持,那么只需要实现WebSocketConfigurer接口

注意: 实现的接口是WebSocketConfigurer,使用的注解是@EnableWebSocket

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 MyWebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(myWebSocketHandler(), "/my-websocket-endpoint")
                .setAllowedOrigins("*")
                .addInterceptors(new MyHandshakeInterceptor());
    }

    public WebSocketHandler myWebSocketHandler() {
        return new MyWebSocketHandler();
    }
}

情况2:如果需要消息代理和STOMP协议支持,那么需要实现WebSocketMessageBrokerConfigurer接口

注意: 实现的接口是WebSocketMessageBrokerConfigurer,使用的注解是@EnableWebSocketMessageBroker

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration
@EnableWebSocketMessageBroker
public class MyWebSocketMessageBrokerConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic", "/queue");
        registry.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/stomp-websocket-endpoint")
                .setAllowedOrigins("*")
                .withSockJS();
    }
}

情况三: 如果你的应用需要同时支持简单的WebSocket连接和基于STOMP的消息处理,可以同时实现这两个接口

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration
@EnableWebSocket
@EnableWebSocketMessageBroker
public class MyWebSocketConfig implements WebSocketConfigurer, WebSocketMessageBrokerConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(myWebSocketHandler(), "/my-websocket-endpoint")
                .setAllowedOrigins("*")
                .addInterceptors(new MyHandshakeInterceptor());
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic", "/queue");
        registry.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/stomp-websocket-endpoint")
                .setAllowedOrigins("*")
                .withSockJS();
    }

    public WebSocketHandler myWebSocketHandler() {
        return new MyWebSocketHandler();
    }
}

常见错误

org.springframework.context.ApplicationContextException: Failed to start bean 'subProtocolWebSocketHandler'; nested exception is java.lang.IllegalArgumentException: No handlers

image
上述问题可能就是注解使用错误,例如实现的接口是WebSocketConfigurer,结果使用的注解是@EnableWebSocketMessageBroker;或者实现的接口是WebSocketMessageBrokerConfigurer,使用的注解是@EnableWebSocket

标签:WebSocket,Spring,注意事项,springframework,registry,import,org,config,annotation
From: https://www.cnblogs.com/jarssion/p/18256318

相关文章

  • spring 使用 事件机制
    概述在编写代码的时候,比如我删除一篇文章,这个时候,如果我想做些额外的逻辑,这是就需要修改删除部分的代码。spring提供了事件机制更优雅的实现这个,用户只需要实现事件监听即可。代码实现注入发布者publicclassKnowledgeBaseServiceimplementsApplicationEventPublisherAwar......
  • [com.t.extend.SpringContextLoaderListener] - generate index.html sucess,ERROR or
    错误:2024-06-1913:23:09,873INFO[com.t.extend.SpringContextLoaderListener]-generateindex.htmlsucess13:23:10.159[RMITCPConnection(3)-127.0.0.1]ERRORorg.apache.struts2.dispatcher.Dispatcher-Dispatcherinitializationfailedcom.opensymphony.xwor......
  • Springboot 集成 Shardingsphere-JDBC
    Springboot集成Shardingsphere-JDBCShardingsphere系列目录:背景调研前提新增依赖分表策略简单分库分表策略垂直分库广播表水平分库(单表)水平分库(多表)水平分表HINT配置逻辑代码自定义分库分表(精准定位+范围查询)配置代码精准定位数据库精准定位+范围查询表代码仓......
  • SpringData初步学习-连接MySQL数据库
    1.添加mysql驱动和spring-data-jpa依赖<dependencies><!--SpringDataJPA--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId><......
  • java spring-data-jpa 使用方法
    SpringDataJPA是Spring生态系统的一部分,它提供了对JPA(JavaPersistenceAPI)的抽象,简化了数据访问层的开发。以下是使用SpringDataJPA的基本步骤和一些核心概念:1.添加依赖        在Maven项目的pom.xml文件中添加SpringDataJPA和相关数据库驱......
  • 基于SpringBoot+Vue的高校爱心捐赠系统设计与实现(源码+lw+部署+讲解)
    文章目录前言详细视频演示具体实现截图技术可行性分析技术简介后端框架SpringBoot前端框架Vue系统开发平台系统架构设计业务流程分析为什么选择我们自己的公众号(一点毕设)海量实战案例代码参考数据库参考源码及文档获取前言......
  • springboot框架怎么用?是什么?
    SpringBoot框架是一个用于简化Spring应用初始搭建以及开发过程的全新框架,旨在通过特定的配置方式,使开发人员不再需要定义样板化的配置,从而加速应用开发。以下是关于SpringBoot框架的详细使用和介绍:1.SpringBoot框架的使用安装Java开发环境:首先,确保已经正确配置了JDK。......
  • [转帖]springboot中Hikari连接池常用参数含义(一)
    <divid="content_views"class="htmledit_views"><p>yml配置<br><imgalt=""height="235"src="https://img-blog.csdnimg.cn/7724916bc5d449b48114ed52462ba48d.png"......
  • Spring容器系列-FactoryBean使用/原理
    Spring-FactoryBean使用/原理  概要  在某些情况下,实例化Bean过程比较复杂,若按照传统的方式,则需要在中提供大量的配置信息,不够灵活,这时采用编码的方式能得到一个简单的方案。  Spring为此提供了一个org.springframework.bean.factory.FactoryBean的工厂类接口,用户......
  • SpringBoot整合JWT(JSON Web Token)生成token与验证
    目录JWT什么是JWTJWT使用流程确定要传递的信息:生成JWT:JWT传输:客户端保存JWT:客户端发送JWT:服务器验证JWT:服务器响应:Token的使用示例:工具类R结果集返回一个生成的token创建拦截器JWT什么是JWTJWT(JSONWebToken)是是目前最流行的跨域认证解决方案。它通常被......