先附代码:
uni.connectSocket({
url: 'ws://192.168.4.1:8888',
success: function() {
console.log("WebSocket连接成功");
// that.sleep(2000);
uni.onSocketOpen(function(res) {
console.log('WebSocket连接已打开!');
uni.sendSocketMessage({
data: that.bssid,
// data: "a6:12:12:12:12:12",
success: function() {
console.log("数据发送成功,数据为:" + that.bssid);
//-----------------------------------------
console.log("数据发送结束,开始接收");
// that.sleep(2000);
uni.onSocketMessage(function(res) {
isReceive = true;
console.log('收到服务器内容:' + res.data);
uni.closeSocket({
success: function() {
console.log("websocket关闭成功");
},
fail: function() {
console.log("websocket关闭失败");
}
})
})
//--------------------------------------
},
fail: function() {
console.log("数据发送失败");
}
});
uni.onSocketClose(function(res){
console.log("websocket已关闭");
if (isReceive == false) {
uni.hideLoading();
uni.showToast({
title: "通信失败",
icon:'none'
})
}
else {
// uni.hideLoading();
}
})
})
},
fail: function(){
uni.hideLoading();
uni.showToast({
title: "设备初始化失败",
icon:'none'
})
},
// )};
})
uni.onSocketError(function(res) {
uni.hideLoading();
console.log('WebSocket连接打开失败,请检查!');
uni.showToast({
title: "设备初始化失败",
icon: "none"
})
return false;
});
},
在connect前已经确认服务端连接成功,运行结果:
33859776
192.168.4.2
IP地址分配成功,开始通信
开始connect.........
WebSocket连接成功
WebSocket connection to 'ws://192.168.4.1:8888/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
WebSocket连接打开失败,请检查!
websocket已关闭
xc_9418_
大佬可以留下联系方式请教一下吗?
2020-12-18 16:51
Spring_CTH
回复 xc_9418_: 2625987756
2021-04-23 13:54
z***@163.com
卧槽 和你相隔三年 偶然搜到你这个回答 我才反应过来 我他么和你一样的问题
2023-08-29 16:11