小程序链接内网电脑
电脑端开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)[代码][代码] [代码][代码]}[代码][代码]}[代码]