wx.connectSocket不同步导致undefined?
请教一下,wx.connectSocket还没有成功返回SocketTask,紧接着就调用socket.onOpen()、socket.onMessage(),
此时会报socket undefined的错误。这个问题应该怎么解决呢?
代码如下
var socket = wx.connectSocket({
//url: "adsasada",
url: 'ws://127.0.0.1:8080/test',
success: function (resConnect) {
console.log("resConnect: ", resConnect);
},
fail: function (resConnectError) {
console.log("resConnectError: ", resConnectError);
}
});
socket.onOpen(function (resOpen) { // 此时连接未成功建立,会提示socket undefined
console.log("连接已打开:", resOpen)
})
socket.onMessage(function (resMessage) {
console.log("socket接收到消息:", resMessage)
})