电脑端开TCP 端口号为60000的端口 ,server端口。微信小程序 可以连接
端口 ,但不能发数据?请问问什么?
var app = getApp();
var SocketTask = false;
var socketOpen = false;
var socketMsgQueue = []
Page({
data: {
inputValue: '123',
returnValue: '1',
},
onLoad: function (options) {
},
onReady: function () {
wx.connectSocket({
url = "ws://172.20.20.21:60000"
})
wx.onSocketOpen(function (res) {
socketOpen = true;
for (var i = 0; i < socketMsgQueue.length; i++) {
sendSocketMessage(socketMsgQueue[i])
}
socketMsgQueue = [];
})
function sendSocketMessage(msg) {
if (socketOpen) {
wx.sendSocketMessage({
data: msg
});
} else {
socketMsgQueue.push(msg)
}
}
}
})
//通过 WebSocket 连接发送数据,需要先 wx.connectSocket,并在 wx.onSocketOpen 回调之后才能发送。
function sendSocketMessage(data) {
console.log('通过 WebSocket 连接发送数据')
if (socketOpen) {
SocketTask.send({
//data: JSON.stringify(data)
data: data
}, function (res) {
console.log('已发送', res)
})
} else {
socketMsgQueue.push(msg)
}
}