App.vue 1.9 KB

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