最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

react native - I am sending websocket message to both sessions on different android emulators but only one phone is receiving th

programmeradmin1浏览0评论

SPRINGBOOT BACKEND:

@Transactional
private void acceptPairingRequest(JSONObject jsonObject, WebSocketSession receiverSession) throws IOException, InterruptedException {
    JSONObject finalPairing = chatService.acceptPairingRequest(jsonObject);

    String senderId = String.valueOf(jsonObject.get("senderId"));
    WebSocketSession senderSession = activeUserSessions.get(senderId);

    log.info("Receiver session is: {}", receiverSession);
    if (receiverSession != null && receiverSession.isOpen()) {
        receiverSession.sendMessage(new TextMessage(finalPairing.toString()));
    } else {
        log.warn("Receiver session is null or closed for user: {}", jsonObject.get("receiverPseudoName"));
    }

    log.info("Sender session is: {}", senderSession);
    if (senderSession != null && senderSession.isOpen()) {
        senderSession.sendMessage(new TextMessage(finalPairing.toString()));
    } else {
        log.warn("Sender session is null or closed for user: {}", jsonObject.get("senderPseudoName"));
    }

    log.info("Final chat sent to both {} and {}", jsonObject.get("senderPseudoName"), jsonObject.get("receiverPseudoName"));
}

REACT NATIVE FRONTEND:

ws.onmessage = async (event) => {
    try {
        const data = JSON.parse(event.data);
        console.log("Received WebSocket Data:", data);

        switch (data.type) {
            case "accept chat":
                console.log("received chat data: ", data);
                dispatch(addChat(data));
                console.log("dispatching chat to both users");
                break;

            case "message":
                dispatch(setMessage(data));
                break;

            case "pairing request received":
                dispatch(setNotification(data));
                break;

            default:
                console.log("Unknown message type:", data.type);
        }
    } catch (error) {
        console.error("Error handling WebSocket message:", error);
    }
};

*TERMINAL PHONE 1: Chats updated: []

*TERMINAL PHONE 2: new chat added dispatching chat to both users Chats updated: [{"chatId": "3e4f103c-8428-433d-8d05-86254e7cf5e5", "receiverId": "ec613379-389c-4f4b-99cd-593c79e7cc85", "senderId": "82a84a9e-a515-4e49-9825-24802a1760c4" "type": "accept chat"}]

ONLY ONE PHONE IS SHOWING THE NEWLY CHAT ADDED

I am expecting both will have the chat because i have dispatch() it to chatSlice in redux store. Note: I am using EXPO GO And apps are running on port exp://192.168.1.7:8081 and exp://192.168.1.7:8082

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论