微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 SockJS 连接到 Web 套接字时,显示连接尝试失败那是什么

如何解决使用 SockJS 连接到 Web 套接字时,显示连接尝试失败那是什么

当我尝试从 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 举报,一经查实,本站将立刻删除。