【问题标题】:SpringBoot AngularJS webSocket integrationSpringBoot AngularJS webSocket 集成
【发布时间】:2020-02-15 06:25:07
【问题描述】:

我正在尝试在 springBoot 应用程序上创建 webSocket。 这是配置类:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/sub");
    config.setApplicationDestinationPrefixes("/send");
}

@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
}

@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
}

@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
}

@Override
public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
}

@Override
public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
    return true;
}

}

现在我正在尝试使用 SockJS 和 Stomp 从 AngularJS 连接到 websocket

    var socket = new SockJS('/api/ws');
    var stompClient = Stomp.over(socket);

    stompClient.connect({}, function (frames) {
            stompClient.subscribe('/subs/hello', function (data) {
            console.log(data);
        });
    });

当我打开控制台时,我收到以下消息:

admin-components.js:112173 WebSocket 连接到“wss://proxy.beta.corp.payment21.com/api/ws/135/hwq2yv3q/websocket”失败:WebSocket 握手期间出错:意外响应代码:502

30 秒后我得到这个:

VM333 sockjs.min.js:2 未捕获的错误:不兼容的 SockJS!主站点使用:“1.4.0”,iframe:“1.0.0”。 在 s (VM333 sockjs.min.js:2)

再过 30 秒:

它正在工作......

当我去网络查看框架中显示的详细信息时

(操作码-1)

这里有什么问题?是spring配置还是SockJS?

【问题讨论】:

  • 你有没有发现这个问题?

标签: angularjs spring-boot websocket spring-websocket sockjs


【解决方案1】:

基于Spring documentation,支持的客户端SockJS版本为1.0.x

在浏览器端,应用程序可以使用 sockjs-client(版本 1.0.x)。它模拟 W3C WebSocket API 并与服务器通信以选择最佳传输选项,具体取决于运行它的浏览器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 2021-08-31
    相关资源
    最近更新 更多