问题描述
当我尝试从 Angular 客户端与 spring boot 后端建立 Web 套接字连接时,它会在控制台中显示这一点。连接不会打开。 589和下一部分是随机的,
WebSocket connection to 'ws://localhost:8080/ws/589/co25fys3/websocket' Failed:
public _connect() {
const username = this.authService.user.value.userName;
//console.log("Initialize WebSocket Connection for user," + username);
let ws = new SockJS('http://localhost:8080/ws');
this.stompClient = Stomp.over(ws);
const _this = this;
_this.stompClient.connect({username: username,},function (frame) {
_this.stompClient.subscribe(_this.topic,function (sdkEvent) {
_this.onMessageReceived(sdkEvent);
});
//_this.stompClient.reconnect_delay = 2000;
},this.errorCallBack);
}
错误来自连接方法。这是我的spring web socket配置类,
package com.webSocket.springbootwebsocket.config;
import com.webSocket.springbootwebsocket.handler.HttpSessionIdHandshakeInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessagebrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessagebroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessagebrokerConfigurer;
@Configuration
@EnableWebSocketMessagebroker
public class WebSocketConfig implements WebSocketMessagebrokerConfigurer {
@Override
public void configureMessagebroker(MessagebrokerRegistry config) {
config.enableSimplebroker("/topic");
config.setApplicationDestinationPrefixes("/app");
config.setUserDestinationPrefix("/users");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws").setAllowedOrigins("http://localhost:4200")
.addInterceptors(new HttpSessionIdHandshakeInterceptor())
.withSockJS();
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.interceptors(new UserInterceptor());
}
}
有时建立连接时出错。有时它不连接。似乎是什么问题?提前致谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)