document.write("");

SpringBoot 引入 WebSocket

maven 添加websocket ,按照一篇博文引入 websocket

 https://www.cnblogs.com/likun10579/p/5450209.html

 

在浏览器控制台测试是否连通

VM386:1 WebSocket connection to 'ws://localhost:8080/hello' failed: Error during WebSocket handshake: Unexpected response code: 404

  

已知服务器其它接口能够连通,所以不存在url错误的原因,那为什么会报错?有可能是因为我用的springboot的原因?依赖引入应该跟原版有所不同

添加 

@Component

发现依旧是 404

一开始引入的依赖是 :

		<dependency>
			<groupId>javax.websocket</groupId>
			<artifactId>javax.websocket-api</artifactId>
			<version>1.1</version>
			<scope>provided</scope>
		</dependency>

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

  

后修改为

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

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

  

添加websocket配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import java.io.Serializable;

/**
 * @xxx
 * @Description //TODO websocket 配置类
 * @Date  10:56 
 **/
@Configuration
public class WebSocketConfig implements Serializable {

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {

        return new ServerEndpointExporter();
    }
}

  

重新启动 连接OK

 

posted @ 2022-02-06 11:01  人间春风意  阅读(205)  评论(0编辑  收藏  举报