我仍然是新的反应本机,我目前正在工作的项目,需要实现一个websocket连接,在new本地应用程序和订阅主题,以接收消息从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如能提供任何帮助,将不胜感激:)
发布于 2021-12-31 05:09:22
伙计们,在做了一些研究之后,我找到了解决方案,很明显,@stomp/stompjs中存在一些用于react的bug,您可以通过将这一行代码添加到我的stompConfig中来查看解决问题的这里。
stompConfig:{
...,
forceBinaryWSFrames: true,
appendMissingNULLonIncoming: true,
}https://stackoverflow.com/questions/70530384
复制相似问题