App.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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(`wss://zhongyao.fscuc.cn/webSocket/sentMessage/${data.userId}`,() => {
  32. websocket.connectSocket(`ws://zy-web.fscuc.cn:8006/websocket/${data.userId}`,() => {
  33. //如果连接成功则发送心跳检测
  34. console.log('connect ws success')
  35. heartBeatTest()
  36. })
  37. } catch (error) {
  38. console.log('App err:' + error)
  39. }
  40. })
  41. })
  42. onHide(()=>{
  43. console.log('App Hide')
  44. //关闭socket
  45. uni.$off('login', (data)=>{
  46. websocket.closeSocket()
  47. })
  48. })
  49. const heartBeatTest=()=> {
  50. //清除定时器
  51. clearInterval(globalTimer)
  52. //开启定时器定时检测心跳
  53. globalTimer = setInterval(() => {
  54. //发送消息给服务端
  55. websocket.sendMessage(
  56. JSON.stringify({ action: 'ping'}), //与服务端约定好消息格式
  57. null,
  58. () => {
  59. //如果失败则清除定时器
  60. clearInterval(globalTimer)
  61. }
  62. )
  63. }, 10000)
  64. }
  65. </script>
  66. <style>
  67. /*每个页面公共css */
  68. /* page{
  69. padding-top: 0;
  70. } */
  71. /* @import "@/static/iconfont.css" */
  72. .main{
  73. background-color: #d9d9d9;
  74. display: flex;
  75. flex-direction:column;
  76. width: 686px;
  77. /* height: 2000px; */
  78. }
  79. </style>