App.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <!-- <template>
  2. <div class="app">
  3. <router-view class="page-top-space"></router-view>
  4. </div>
  5. </template> -->
  6. <script setup>
  7. import { onShow, onHide, onLaunch } from '@dcloudio/uni-app'
  8. import websocket from '@/static/js/websocket.js'
  9. //定义定时器
  10. let globalTimer = null
  11. onLaunch(async()=> {
  12. console.log('App onLaunch')
  13. // uni.getSystemInfo({
  14. // success: function(e) {
  15. // // Vue.prototype.StatusBar = e.statusBarHeight;
  16. // console.log("e.statusBarHeight", e.statusBarHeight)
  17. // if (e.platform == 'android') {
  18. // // Vue.prototype.CustomBar = e.statusBarHeight + 50;
  19. // console.log('CustomBar', e.statusBarHeight + 50)
  20. // } else {
  21. // console.log('CustomBar', e.statusBarHeight + 45)
  22. // };
  23. // }
  24. // })
  25. })
  26. onShow(()=>{
  27. console.log('App Show')
  28. uni.$on('login',(data)=>{
  29. try {
  30. //建立生产环境socket连接
  31. websocket.connectSocket(`ws://wx.hzliuzhi.com:4433/manager/yfc-mobile/websocket/${data.userId}`,() => {
  32. //如果连接成功则发送心跳检测
  33. console.log('connect ws success')
  34. heartBeatTest()
  35. })
  36. //建立测试环境socket连接
  37. // websocket.connectSocket(`ws://47.118.22.101:3030/websocket/${data.userId}`,() => {
  38. // //如果连接成功则发送心跳检测
  39. // console.log('connect ws success')
  40. // heartBeatTest()
  41. // })
  42. } catch (error) {
  43. console.log('App err:' + error)
  44. }
  45. })
  46. })
  47. onHide(()=>{
  48. console.log('App Hide')
  49. //关闭socket
  50. uni.$off('login', (data)=>{
  51. websocket.closeSocket()
  52. })
  53. })
  54. const heartBeatTest=()=> {
  55. //清除定时器
  56. clearInterval(globalTimer)
  57. //开启定时器定时检测心跳
  58. globalTimer = setInterval(() => {
  59. //发送消息给服务端
  60. websocket.sendMessage(
  61. JSON.stringify({ action: 'ping'}), //与服务端约定好消息格式
  62. null,
  63. () => {
  64. //如果失败则清除定时器
  65. clearInterval(globalTimer)
  66. }
  67. )
  68. }, 5000)
  69. }
  70. </script>
  71. <style>
  72. /*每个页面公共css */
  73. /* page{
  74. padding-top: 0;
  75. } */
  76. /* @import "@/static/iconfont.css" */
  77. .main{
  78. background-color: #d9d9d9;
  79. display: flex;
  80. flex-direction:column;
  81. width: 686px;
  82. /* height: 2000px; */
  83. }
  84. </style>