0

我还是 React Native 的新手,我目前正在研究需要在 React Native 应用程序中实现 websocket 连接并订阅主题以接收来自 websocket 的消息的项目。当我尝试使用@stomp/stompjs实现它时,我无法连接到 websocket,onConnect 函数不起作用。

下面是我的代码

import { Client, Message } from '@stomp/stompjs';
const stompConfig = {
    connectHeaders: {},
    brokerURL: "ws://203xxxxxxxx/xxx/connectSocket",
    debug: function (str) {
        console.log('STOMP: ' + str);
    },
    reconnectDelay: 200,
    onConnect: function (frame) {
        console.log("connected")
        const subscription = stompClient.subscribe("/topic/public/" + userId, function (message) {
            console.log(JSON.parse(message.body));
            
        });
    },
    onStompError: (frame) => {
        console.log('Additional details: ' + frame.body);
    },
}
stompClient = new Client(stompConfig);
useEffect(() => {
    stompClient.activate();
}, [])

这是我得到的输出日志

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

任何帮助,将不胜感激:)

4

1 回答 1

0

伙计们,在做了一些研究后,我发现解决方案显然在@stomp/stompjs中存在某种类型的错误,用于反应原生你可以在这里查看问题 我通过将这行代码添加到我的stompConfig来解决我的问题

stompConfig:{
...,
forceBinaryWSFrames: true,
appendMissingNULLonIncoming: true,
}
于 2021-12-31T05:09:22.883 回答