无法使用 stomp.js 连接到 STOMP

问题描述 投票:0回答:1
var stompClient = Stomp.over(function () {
return sock;
});

console.log("before con");

stompClient.connect({}, () => {
    setConnected;
    stompClient.subscribe("/topic/hello", (message) => {
        console.log(message);
    });
    console.log("runnable");
});
 

这是我尝试连接到 STOMP 的代码,并且我得到了以下日志

LOG  before con
LOG  Opening Web Socket...
LOG  Web Socket Opened...
LOG  >>> CONNECT
accept-version:1.2,1.1,1.0
heart-beat:10000,10000

但是没有收到主题的任何消息


@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {


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

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic");
        registry.setApplicationDestinationPrefixes("/app");
    }


}

这是我目前的springboot配置

我尝试使用 Postman 将 Connect 帧发送到端点,但服务器没有回复。(Websocket 连接正常,但只有 STOMP 连接让我头疼)

CONNECT
accept-version:1.2
host:localhost

^@

这是我用过的连接框架

spring-boot spring-security postman stomp stompjs
1个回答
0
投票

来自 STOMP 规格网站

The body is then followed by the NULL octet. The examples in this document will use ^@, control-@ in ASCII, to represent the NULL octet.


^@
是用于 NULL 八位字节的表示。根据 STOMP 规范,每个帧都应该以这个 NULL 八位字节结束,但例外的是我们可以在这之后有 CRLF 但会被跳过。因此,在邮递员中,完整的消息必须以 NULL 八位字节字符终止,我认为直接从键盘输入无法完成此操作。
但在 Mac 中,我可以通过直接使用浏览器开发工具检查 stomp 帧来复制 NULL 八位字节并将其粘贴到 Postman 中。这确实有效,Spring 解析了主体并将其处理给我的
MessageMapping
处理程序。

© www.soinside.com 2019 - 2024. All rights reserved.