springMVC 整合 websocket 报错 404
第一步:pom配置jar<dependency><groupId>javax.websocket</groupId><artifactId>javax.websocket...
第一步:pom 配置jar
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>${spring.version}</version>
</dependency>
第二步:注册webSocket server 握手拦截器
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
System.out.println("....注册......");
registry.addHandler(myhandler(), "/websocket").addInterceptors(myInterceptors()).setAllowedOrigins("*");
registry.addHandler(myhandler(), "/sockjs/websocket").addInterceptors(myInterceptors()).withSockJS();
}
@Bean
public WebSocketHandler myhandler() {
return new WebsocketEndPoint();
}
@Bean
public HandshakeInterceptor myInterceptors() {
return new HandshakeInterceptor();
}
}
第三步:页面 client
socket = new WebSocket('ws://localhost:9018/testSocket/websocket');
报错:
WebSocket connection to 'ws://localhost:9018/testSocket/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
这是为啥 ???? 是路径不对 还是 那里没有配对??? 展开
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>${spring.version}</version>
</dependency>
第二步:注册webSocket server 握手拦截器
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
System.out.println("....注册......");
registry.addHandler(myhandler(), "/websocket").addInterceptors(myInterceptors()).setAllowedOrigins("*");
registry.addHandler(myhandler(), "/sockjs/websocket").addInterceptors(myInterceptors()).withSockJS();
}
@Bean
public WebSocketHandler myhandler() {
return new WebsocketEndPoint();
}
@Bean
public HandshakeInterceptor myInterceptors() {
return new HandshakeInterceptor();
}
}
第三步:页面 client
socket = new WebSocket('ws://localhost:9018/testSocket/websocket');
报错:
WebSocket connection to 'ws://localhost:9018/testSocket/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
这是为啥 ???? 是路径不对 还是 那里没有配对??? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询