---前言提要---
在百度、谷歌、CSDN、博客园...几经折腾之后,终于达到了我想要的结果。springboot+vue,前后端分离项目,网上的大多不是前后端分离,所以排错怎么也排不出,说到底我产生了依赖,只想从网上寻找答案,然后简单的一了百了,而忘记了思考,为什么出错。
其实错误很简单,我是前后端分离,前后端项目的端口都不一样,一个是8080,一个是自定义的63000,在前端发起的请求到了前端,而前端没有启动websocket这个服务,所以连接超时,无关配置无关代码,只是请求错了而已。
这个教训告诉我们,遇到事先自己想一想,想不通的时候再抱着疑问 一边想一边去查资料。
---正确配置---
后端pom:(可参考网上其他教程pom导包)
<!--ws(start)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<!--ws(end)-->
后端websock配置文件:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();