【问题标题】:Web socket connections with @stomp/stompjs in React Native在 React Native 中使用 @stomp/stompjs 的 Web 套接字连接
【发布时间】:2021-12-30 10:32:12
【问题描述】:

我还是 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

任何帮助将不胜感激:)

【问题讨论】:

    标签: react-native websocket stompjs


    【解决方案1】:

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

    解决了我的问题
    stompConfig:{
    ...,
    forceBinaryWSFrames: true,
    appendMissingNULLonIncoming: true,
    }
    

    【讨论】:

      猜你喜欢
      • 2022-07-07
      • 2017-07-13
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      相关资源
      最近更新 更多