我是 react native 的新手,目前,我正在研究 react native 移动应用程序 WebSocket 创建。我能够成功地在反应应用程序中成功创建 WebSocket 连接并接收到消息。但是在本机反应中,onConnect不起作用。
这是 package.json 中 stompjs 的版本
"@stomp/stompjs": "6.1.2",
这是我在 react 和 react native 中使用的代码。
import {Client} from "@stomp/stompjs";
stompClient = new Client();
stompClient.configure({
brokerURL: 'ws://192.168.xx.xx:8080/ws',
connectHeaders: {
login: 'guest',
passcode: 'guest',
accessToken: access_token, //variables
appKey: app_key //variables
},
reconnectDelay: 2000,
heartbeatIncoming: 0,
heartbeatOutgoing: 20000,
onConnect: () => {
console.log("On connect");
stompClient.subscribe('/topic/public/my_topic', (message) => {
console.log("message");
Alert.alert("Connected", "Connected");
}, {appKey: app_key});
},
onStompError: (frame) => {
console.log('Broker reported error: ' + frame.headers['message']);
console.log('Additional details: ' + frame.body);
},
onDisconnect: (frame) => {
console.log("Stomp Disconnect", frame);
},
onWebSocketClose: (frame) => {
console.log("Stomp WebSocket Closed", frame);
},
debug: (str) => {
console.log(new Date(), str);
},
onUnhandledMessage: (msg) => {
console.log(msg);
}
});
stompClient.activate();
这是我在 react-native 调试器的控制台中得到的回复。
Tue Dec 07 2021 14:23:46 GMT+0530 (India Standard Time) "Opening Web Socket..."
Tue Dec 07 2021 14:23:48 GMT+0530 (India Standard Time) "Web Socket Opened..."
Tue Dec 07 2021 14:23:48 GMT+0530 (India Standard Time) ">>> CONNECT
login:guest
passcode:guest
accessToken:eyJjdHkiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiZGlyIn0..k2fvcDcm6vOfJae6.17rvqKM6g-HD73wdn6_I8XJUX7oSltO558HNcCpmPKDTieFWDE68etFUpdUBQ9lBGvtSXOTUN7sfEhUPc74rF9fxYwQkOHc0LBcFZ0a5pTgtm-p_ogHWPaJvZwzj7L_liQRHV1hoXWTpHymopgxFT8gXaSCAg18VDqo7QAgBbIZIrrzeMMTE0zqYacC9zqS7rmt7XNT1nacwH-USuz9CFHSWI6cmKXgluGI7XjfglkjuLT2ZtG5US4R_nzW_4anUJYpXW0dPzdU-5syiGOJOAMJmN_98-Bt48ycm88yln3JlBkWw.1vxzSIbb5pdcIP3OX9kyxA
appKey:my_app
accept-version:1.0,1.1,1.2
heart-beat:20000,0
"
onConnect 的登录没有登录到控制台。并且无法订阅该主题。非常感谢您帮助解决此问题。