簡體   English   中英

在 SockJsClient 中握手后無法發送消息(React)

[英]Cannot send message after handshake in SockJsClient (React)

我正在使用 Spring Boot-React JS 並嘗試通過 WebSocket 發送消息。握手很好,我可以毫無問題地訂閱。 當我嘗試將數據發送到主題時,不起作用。 onMessage 沒有被觸發。

WebSocket配置

   @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {

        // prefix for server to client message destination
        registry.enableSimpleBroker("/ws-push-notification"); 
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {

        // SockJS connection endpoint
        registry.addEndpoint("/ws-push-notifications").setAllowedOrigins(allowedOrigin).withSockJS();
    }

WebSocketDispatcher

 public void pushNotification(String pushToken, PushNotificationDto notificationDto) {

        SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
        headerAccessor.setSessionId(pushToken);
        headerAccessor.setLeaveMutable(true);

        template.convertAndSendToUser(pushToken, "/ws-push-notification/item/" + pushToken,
                notificationDto, headerAccessor.getMessageHeaders());
    }

我檢查了數據和令牌,它是匹配的。

反應部分:

 const Client = ({
                              props,
                              autoReconnect = true
                          }) => {
    
        const url = "http://localhost:8080/ws-push-notifications?access_token=" + localStorage.getItem(STORAGE_KEY_AT);
        
       const topic = "ws-push-notification/item/" + props.pushToken;
   
        const headers = {
            "Content-Type": CONTENT_TYPE_JSON,
            Authorization: "Bearer " + localStorage.getItem(STORAGE_KEY_AT)
        };
    
        const onConnect = () => {
            props.changeSocketConnectionStatus(true);
            console.info(`Subscribed to socket ${SUDate.toISOLocalDateTime(new Date())}`);
        };
    
        const onMessage = (data) => {
            console.log("This part not getting triggered!!!")
        };

        return (
            <SockJsClient
                url={url}
                topics={[topic]}
                headers={headers}
                subscribeHeaders={headers}
                onConnect={onConnect}
                onMessage={onMessage}
                onDisconnect={onDisconnect}
                onConnectFailure={onConnectFailure}
                autoReconnect={autoReconnect}
            />
        );
    };
    
    export default Client;

template.convertAndSendToUser(pushToken, "/ws-push-notification/item/" + pushToken, notificationDto, headerAccessor.getMessageHeaders());

這將 dto 發送到:/ws-push-notification/item/771063a4-fcea-454f-9673-dedc842290bb905557640

這部分在這里是一樣的。 const topic = "ws-push-notification/item/" + props.pushToken;

為什么不工作?

問題是沒有收到數據。 它正在發送它。

template.convertAndSend("/ws-push-notification/item/" + pushToken, notificationDto);

解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM