首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@ServerEndpoint在Spring boot v2.3.7上不起作用

@ServerEndpoint在Spring boot v2.3.7上不起作用
EN

Stack Overflow用户
提问于 2021-03-22 13:18:21
回答 1查看 183关注 0票数 0

服务器正在运行,但我不能使用chatendpoint.java类。所以spring不使用/user路径。我无法连接ws://localhost:8080/user。当我尝试它的时候,我得到了错误

代码语言:javascript
复制
@ServerEndpoint(value = "/user")
public class ChatEndpoint {
    private Session session;
    private static final Set<ChatEndpoint> chatEndpoints = new CopyOnWriteArraySet<>();
    private static HashMap<String, String> users = new HashMap<>();

    @OnOpen
    public void OnOpen (Session session) {
        System.out.println(" asdasd");
        System.out.println(session.toString()+" asdasd");
    }
      @OnMessage
    public void onMessage(Session session, Message message) throws 
    IOException, EncodeException {
       System.out.println("send message");

    }
}

@Configuration
@EnableWebSocket
public class WebSocketConfig  {

    @Bean
    public ServerEndpointExporter endpointExporter(){
        return new ServerEndpointExporter();
    }
}

@SpringBootApplication
public class WebsocketdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebsocketdemoApplication.class, args);
    }

}

我使用spring-boot-starter-websocket 2.3.7.RELEASE和spring-websocket 5.2.12.RELEASE

代码语言:javascript
复制
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
        <version>2.3.7.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-messaging -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
        <version>5.2.12.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-websocket -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-websocket</artifactId>
        <version>5.2.12.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.3.7.RELEASE</version>
    </dependency>
EN

回答 1

Stack Overflow用户

发布于 2021-03-23 02:24:44

我找到了解决方案。删除@EnableWebSocket,因为我使用的是@serverendpoint注释,它提供了特定的websocket句柄。第二,我有

代码语言:javascript
复制
    @OnMessage
public void onMessage(Session session, Message message) throws IOException, EncodeException {
    System.out.println("send message");
   
}

消息消息参数导致编译错误我用字符串消息参数更改了它。并将@component添加到Chatendpoint类。因为spring和tomcat可以理解它是一个组件。

代码语言:javascript
复制
       @Component
    @ServerEndpoint(value = "/user")
    public class ChatEndpoint {

    @OnOpen
    public void OnOpen (Session session) {
        
    }
   
       @OnMessage
    public void onMessage(Session session, String message) throws IOException, EncodeException {
        System.out.println("send message");
       
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66740678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档