app.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import {
  2. ApiModel
  3. } from './utils/api';
  4. App({
  5. onLaunch() {},
  6. initSocket() {
  7. return;
  8. let that = this;
  9. this.globalData.SocketTask = wx.connectSocket({
  10. url: 'ws://121.37.152.76:8080/yos/webSocket/' + wx.getStorageSync('userMsg').token,
  11. complete: (res) => {
  12. console.log(res)
  13. }
  14. })
  15. this.globalData.SocketTask.onOpen(function (res) {
  16. console.log('WebSocket连接已打开!readyState=' + that.globalData.SocketTask.readyState);
  17. that.globalData.socketEstablish = true;
  18. })
  19. this.globalData.SocketTask.onMessage(function (res) {
  20. that.globalData.callback(res)
  21. })
  22. this.globalData.SocketTask.onError(function (res) {
  23. console.log('readyState=' + that.globalData.SocketTask.readyState)
  24. setTimeout(() => {
  25. that.initSocket()
  26. }, 1000)
  27. })
  28. this.globalData.SocketTask.onClose(function (res) {
  29. console.log('WebSocket连接已关闭!readyState=' + that.globalData.SocketTask.readyState)
  30. if (that.globalData.socketEstablish == false) {
  31. setTimeout(() => {
  32. that.initSocket()
  33. }, 1000)
  34. } else {
  35. that.globalData.socketEstablish = false;
  36. }
  37. })
  38. },
  39. globalData: {
  40. http: new ApiModel(), //接口文件
  41. queryPer: require("./utils/queryPermissions"), //权限查询
  42. socketEstablish: false, //是否已经建立socket
  43. SocketTask: '',
  44. callback: function () {},
  45. },
  46. })