我还是 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
任何帮助,将不胜感激:)