我是偶尔能触发SocketTask.onOpen()
WebSocket能够建立连接,但是就是接不到消息,后续的接口都调用不了,怎么回事?[代码]var[代码] [代码]socketOpen = [代码][代码]false[代码][代码];[代码][代码]var[代码] [代码]frameBuffer_Data, session, SocketTask;[代码][代码]var[代码] [代码]url = [代码][代码]'wss://...'[代码][代码];[代码] [代码]Page({[代码][代码] [代码][代码]data: {[代码][代码] [代码][代码]toView: [代码][代码]'green'[代码][代码],[代码][代码] [代码][代码]windowH: [代码][代码]"1000"[代码][代码],[代码][代码] [代码][代码]user_input_text: [代码][代码]''[代码][代码],[代码][代码]//用户输入文字[代码][代码] [代码][代码]inputValue: [代码][代码]''[代码][代码],[代码][代码] [代码][代码]returnValue: [代码][代码]''[代码][代码],[代码][代码] [代码][代码]addImg: [代码][代码]false[代码][代码],[代码][代码] [代码][代码]//格式示例数据,可为空[代码][代码] [代码][代码]allContentList: [],[代码][代码] [代码][代码]num: 0,[代码][代码] [代码][代码]wo: [代码][代码]""[代码][代码],[代码][代码] [代码][代码]ta: [代码][代码]""[代码][代码],[代码][代码] [代码][代码]youImg: [代码][代码]""[代码][代码] [代码][代码]},[代码][代码] [代码][代码]//通过 WebSocket 连接发送数据,需要先 wx.connectSocket,并在 wx.onSocketOpen 回调之后才能发送。[代码][代码] [代码][代码]sendSocketMessage: [代码][代码]function[代码] [代码](msg) {[代码][代码] [代码][代码]var[代码] [代码]that = [代码][代码]this[代码][代码];[代码][代码] [代码][代码]console.log([代码][代码]'通过 WebSocket 连接发送数据'[代码][代码], JSON.stringify(msg))[代码][代码] [代码][代码]// debugger[代码][代码] [代码][代码]SocketTask.send([代码][代码] [代码][代码]{[代码][代码] [代码][代码]data: JSON.stringify(msg)[代码][代码] [代码][代码]},[代码][代码] [代码][代码]function[代码] [代码](res) {[代码][代码] [代码][代码]console.log([代码][代码]'已发送'[代码][代码], res)[代码][代码] [代码][代码]}[代码][代码] [代码][代码])[代码][代码] [代码][代码]},[代码][代码] [代码][代码]onLoad: [代码][代码]function[代码] [代码](options) {[代码][代码] [代码][代码]const that = [代码][代码]this[代码][代码];[代码][代码] [代码][代码]},[代码][代码] [代码][代码]onReady: [代码][代码]function[代码] [代码]() {[代码][代码] [代码][代码]var[代码] [代码]that = [代码][代码]this[代码][代码];[代码][代码] [代码][代码]SocketTask.onOpen(res => {[代码][代码] [代码][代码]socketOpen = [代码][代码]true[代码][代码];[代码][代码] [代码][代码]console.log([代码][代码]'监听 WebSocket 连接打开事件。'[代码][代码], res)[代码][代码] [代码][代码]//发送登陆信息[代码][代码] [代码][代码]var[代码] [代码]data = {[代码][代码] [代码][代码]// body: that.data.inputValue,[代码][代码] [代码][代码]"Name"[代码][代码]: that.data.wo,[代码][代码] [代码][代码]"content"[代码][代码]: [代码][代码]"login"[代码][代码],[代码][代码] [代码][代码]"type"[代码][代码]: 4[代码][代码] [代码][代码]}[代码][代码] [代码][代码]that.sendSocketMessage(data);[代码][代码] [代码][代码]//循环发送心跳[代码][代码] [代码][代码]setInterval([代码][代码] [代码][代码]function[代码] [代码]() {[代码][代码] [代码][代码]var[代码] [代码]ping = { [代码][代码]"type"[代码][代码]: [代码][代码]"ping"[代码] [代码]};[代码][代码] [代码][代码]that.sendSocketMessage(ping);[代码][代码] [代码][代码]}, 20000[代码][代码] [代码][代码]);[代码][代码] [代码][代码]})[代码][代码] [代码][代码]SocketTask.onClose(onClose => {[代码][代码] [代码][代码]console.log([代码][代码]'监听 WebSocket 连接关闭事件。'[代码][代码], onClose)[代码][代码] [代码][代码]socketOpen = [代码][代码]false[代码][代码];[代码][代码] [代码][代码]this[代码][代码].webSocket()[代码][代码] [代码][代码]})[代码][代码] [代码][代码]SocketTask.onError(onError => {[代码][代码] [代码][代码]console.log([代码][代码]'监听 WebSocket 错误。错误信息'[代码][代码], onError)[代码][代码] [代码][代码]socketOpen = [代码][代码]false[代码][代码] [代码][代码]})[代码][代码] [代码][代码]SocketTask.onMessage(onMessage => {[代码][代码] [代码][代码]console.log([代码][代码]"onMessage:::::"[代码] [代码]+ onMessage.data);[代码][代码] [代码][代码]if[代码] [代码](onMessage.data.indexOf([代码][代码]"上线"[代码][代码]) != -1 || onMessage.data.indexOf([代码][代码]"下线"[代码][代码]) != -1) {[代码][代码] [代码][代码]return[代码][代码];[代码][代码] [代码][代码]}[代码][代码] [代码][代码]console.log([代码][代码]'监听WebSocket接受到服务器的消息事件。服务器返回的消息'[代码][代码], JSON.parse(onMessage.data))[代码][代码] [代码][代码]var[代码] [代码]onMessage_data = JSON.parse(onMessage.data)[代码][代码] [代码][代码]if[代码] [代码](onMessage_data.toName == that.data.wo && onMessage_data.name == that.data.ta) {[代码][代码] [代码][代码]// addmsglist1(msg1.name, msg1.content)[代码][代码] [代码][代码]that.data.allContentList.push({[代码][代码] [代码][代码]"id"[代码][代码]: [代码][代码]""[代码][代码],[代码][代码] [代码][代码]"hx_id"[代码][代码]: [代码][代码]""[代码][代码],[代码][代码] [代码][代码]"wo"[代码][代码]: that.data.ta,[代码][代码] [代码][代码]"ta"[代码][代码]: that.data.wo,[代码][代码] [代码][代码]"content"[代码][代码]: onMessage_data.content,[代码][代码] [代码][代码]"voice_url"[代码][代码]: [代码][代码]""[代码][代码],[代码][代码] [代码][代码]"fileurl"[代码][代码]: [代码][代码]null[代码][代码],[代码][代码] [代码][代码]"create_date"[代码][代码]: [代码][代码]"2019-12-03"[代码][代码] [代码][代码]});[代码][代码] [代码][代码]that.setData({[代码][代码] [代码][代码]allContentList: that.data.allContentList[代码][代码] [代码][代码]})[代码][代码] [代码][代码]}[代码][代码] [代码][代码]})[代码][代码] [代码][代码]},[代码][代码] [代码][代码]onShow: [代码][代码]function[代码] [代码]() {[代码][代码] [代码][代码]if[代码] [代码](!socketOpen) {[代码][代码] [代码][代码]this[代码][代码].webSocket()[代码][代码] [代码][代码]}[代码][代码] [代码][代码]},[代码][代码] [代码][代码]webSocket: [代码][代码]function[代码] [代码]() {[代码][代码] [代码][代码]const that = [代码][代码]this[代码][代码];[代码][代码] [代码][代码]// 创建Socket[代码][代码] [代码][代码]SocketTask = wx.connectSocket({[代码][代码] [代码][代码]url: url,[代码][代码] [代码][代码]data: [代码][代码]'data'[代码][代码],[代码][代码] [代码][代码]header: {[代码][代码] [代码][代码]'content-type'[代码][代码]: [代码][代码]'application/json'[代码][代码] [代码][代码]},[代码][代码] [代码][代码]method: [代码][代码]'post'[代码][代码],[代码][代码] [代码][代码]success: [代码][代码]function[代码] [代码](res) {[代码][代码] [代码][代码]console.log([代码][代码]'WebSocket连接创建'[代码][代码], res)[代码][代码] [代码][代码]},[代码][代码] [代码][代码]fail: [代码][代码]function[代码] [代码](err) {[代码][代码] [代码][代码]wx.showToast({[代码][代码] [代码][代码]title: [代码][代码]'网络异常!'[代码][代码],[代码][代码] [代码][代码]})[代码][代码] [代码][代码]console.log(err)[代码][代码] [代码][代码]},[代码][代码] [代码][代码]})[代码][代码] [代码][代码]},[代码][代码] [代码][代码]onHide: [代码][代码]function[代码] [代码]() {[代码][代码] [代码][代码]SocketTask.close([代码][代码]function[代码] [代码](res) {[代码][代码] [代码][代码]console.log(res);[代码][代码] [代码][代码]})[代码][代码] [代码][代码]},[代码][代码] [代码][代码]onUnload: [代码][代码]function[代码] [代码]() {[代码][代码] [代码][代码]SocketTask.close([代码][代码]function[代码] [代码](res) {[代码][代码] [代码][代码]console.log(res);[代码][代码] [代码][代码]})[代码][代码] [代码][代码]}[代码][代码]})[代码][图片] 能够建立起连接,但是后续方法都没用,监听SocketTask.onOpen方法,监听不到,SocketTask.onMessage方法也监听不到
2020-04-16真机上就拿不到 但是在开发工具就行 无语了
缓存问题 wx.getStorageSync频繁调用时经常获取不到数据- 当前 Bug 的表现(可附上截图) 在请求接口时会先通过wx.getStorageSync()获取缓存中的token,打印出来经常拿不到token, 但其实缓存中的token是存在的 ,重新调用接口又能拿到token。 出现的很频繁 十次大概就会出现个一两次。 同步异步获取缓存的方法都有这个问题。 ios测试未出现这个问题 主要是安卓 并且是微信最新版本7.0.4 [图片] [图片] [图片] - 预期表现 wx.getStorageSync()每次都能正常的获取到数据而不是为空 - 复现路径 - 提供一个最简复现 Demo
2020-04-15求大佬
websocket 有时候能正常打开,有时候打开失败?[图片] [图片][图片][图片]
2020-04-12