某些设备的 WebSocket stomp 客户端订阅不工作

WebSocket stomp client subscribe for some devices are not working

我正在尝试使用 WebSocket stomp 客户端 实现 spring 启动聊天应用程序。如果我从一台设备向 4,5 台设备发送消息,那么有些设备会收到消息,有些则不会。有些可以发送消息但收不到任何消息,有些则工作得很好。我的应用程序在 Wildfly 服务器上 运行,URL 已结束 [​​=25=]https。

这是我的 js 文件。在我的 JSP 页面中,我使用所有参数调用 sendMsg,并通过 render 方法使用 Handlebars 将响应附加到 JSP。

if (!window.location.origin) {
    window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
}
const url = window.location.origin+contextPath;
let stompClient;
let selectedUser;
let newMessages = new Map();


function connectToChat(userName, topicName) {
    console.log("connecting to chat...")
    let socket = new SockJS(url + '/chat');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        console.log("connected to: " + frame);
        stompClient.subscribe("/topic/decision-log", function (response) {
            let data = JSON.parse(response.body);
            
             var msg = data.message;
             var fromlogin = data.message;
             render(data.username, msg, fromlogin);
  
            
        });
    });
}
connectToChat("1", "decision-log");
function sendMsg(from, text, username) {

    stompClient.send("/app/chat/" + from, {}, JSON.stringify({
        fromLogin: from,
        message: text,
        topicName: topicName,
        username: username
    }));
}

function render(username, message, projectId) {

    var templateResponse = Handlebars.compile($("#message-response-template").html());
    var contextResponse = {
        username: username,
        response: message,
        date: date,
        projectId: projectId
    };

    setTimeout(function () {
        $chatHistoryList.append(templateResponse(contextResponse));
        scrollToBottom();
    }.bind(this), 1500);
}

这是我的 WebSocket 配置文件:

@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfiguration implements WebSocketMessageBrokerConfigurer{
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/chat").setAllowedOrigins("*").withSockJS();

    }

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

    }

}

这是控制器。我总是将所有通过 WebSocket 传入的消息保存在数据库中,这就是为什么我可以确保所有设备都可以发送已保存在数据库中的消息。

@控制器

@AllArgsConstructor
public class MessageController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    private final DecisionLogService decisionLogService;


    @MessageMapping("/chat/{to}")
    public void sendMessage(@DestinationVariable String to, MessageModel message, Authentication authentication ) {
        
        simpMessagingTemplate.convertAndSend("/topic/decision-log", message);

        AuthResponse userDetails = (AuthResponse) authentication.getDetails();
        DecisionLogCreateRequest decisionLogCreateRequest = new DecisionLogCreateRequest();
        decisionLogCreateRequest.setDecision(message.getMessage());
        decisionLogCreateRequest.setProjectId(to);
        ServiceResponseExtended<Boolean> response = decisionLogService.addDecisionLog(userDetails.getAccessToken(), decisionLogCreateRequest);

        
    }

}

我找不到与此问题类似的内容。请帮助我提供正确的信息和建议,如果有人遇到同样的问题请与我分享。

将 RabbitMQ Stomp Broker 配置为消息代理而不是 SimpleBroker 后问题已解决。

当前的 WebSocket 配置:

@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfiguration implements WebSocketMessageBrokerConfigurer{
    @Value("${stomp-broker-relay-host}")
    private String RELAY_HOST;

    @Value("${stomp-broker-relay-port}")
    private String RELAY_PORT;

    @Value("${stomp-broker-login-user}")
    private String USER;

    @Value("${stomp-broker-login-pass}")
    private String PASS;

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

    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {

        registry.setApplicationDestinationPrefixes("/app");
        registry.enableStompBrokerRelay("/topic").setRelayHost(RELAY_HOST).setRelayPort(Integer.parseInt(RELAY_PORT)).setClientLogin(USER)
                .setClientPasscode(PASS);
    }

} 

参考: https://medium.com/@rameez.s.shaikh/build-a-chat-application-using-spring-boot-websocket-rabbitmq-2b82c142f85a

https://www.javainuse.com/misc/rabbitmq-hello-world