|
@@ -4,8 +4,44 @@ import {
|
|
|
|
|
|
App({
|
|
|
onLaunch() {},
|
|
|
+ initSocket() {
|
|
|
+ console.log('链接initSocket')
|
|
|
+ let that = this;
|
|
|
+ this.globalData.SocketTask = wx.connectSocket({
|
|
|
+ url: 'wss://www.buwanjia.com/bwj/webSocket/' + wx.getStorageSync('userData').token,
|
|
|
+ complete: (res) => {
|
|
|
+ console.log(res)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ this.globalData.SocketTask.onOpen(function (res) {
|
|
|
+ console.log('WebSocket连接已打开!readyState=' + that.globalData.SocketTask.readyState);
|
|
|
+ that.globalData.socketEstablish = true;
|
|
|
+ })
|
|
|
+ this.globalData.SocketTask.onMessage(function (res) {
|
|
|
+ that.globalData.callback(res)
|
|
|
+ })
|
|
|
+ this.globalData.SocketTask.onError(function (res) {
|
|
|
+ console.log('readyState=' + that.globalData.SocketTask.readyState)
|
|
|
+ setTimeout(() => {
|
|
|
+ that.initSocket()
|
|
|
+ }, 1000)
|
|
|
+ })
|
|
|
+ this.globalData.SocketTask.onClose(function (res) {
|
|
|
+ console.log('WebSocket连接已关闭!readyState=' + that.globalData.SocketTask.readyState)
|
|
|
+ if (that.globalData.socketEstablish == false) {
|
|
|
+ setTimeout(() => {
|
|
|
+ that.initSocket()
|
|
|
+ }, 1000)
|
|
|
+ } else {
|
|
|
+ that.globalData.socketEstablish = false;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
globalData: {
|
|
|
http: new ApiModel(), //接口文件
|
|
|
queryPer: require("./utils/queryPermissions"), //权限查询
|
|
|
+ socketEstablish: false, //是否已经建立socket
|
|
|
+ SocketTask: '',
|
|
|
+ callback: function () {},
|
|
|
},
|
|
|
})
|