1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <!-- <template>
- <div class="app">
- <router-view class="page-top-space"></router-view>
- </div>
- </template> -->
- <script setup>
- import { onShow, onHide, onLaunch } from '@dcloudio/uni-app'
- import websocket from '@/static/js/websocket.js'
- //定义定时器
- let globalTimer = null
- onLaunch(async()=> {
- console.log('App onLaunch')
-
- // uni.getSystemInfo({
- // success: function(e) {
- // // Vue.prototype.StatusBar = e.statusBarHeight;
- // console.log("e.statusBarHeight", e.statusBarHeight)
- // if (e.platform == 'android') {
- // // Vue.prototype.CustomBar = e.statusBarHeight + 50;
- // console.log('CustomBar', e.statusBarHeight + 50)
- // } else {
- // console.log('CustomBar', e.statusBarHeight + 45)
- // };
- // }
- // })
-
- })
- onShow(()=>{
-
- console.log('App Show')
- uni.$on('login',(data)=>{
- try {
- //建立生产环境socket连接
- websocket.connectSocket(`ws://8.139.252.178:8001/yfc-mobile/websocket/${data.userId}`,() => {
- //如果连接成功则发送心跳检测
- console.log('connect ws success')
- heartBeatTest()
- })
- //建立测试环境socket连接
- // websocket.connectSocket(`ws://47.118.22.101:3030/websocket/${data.userId}`,() => {
- // //如果连接成功则发送心跳检测
- // console.log('connect ws success')
- // heartBeatTest()
- // })
-
- } catch (error) {
- console.log('App err:' + error)
- }
- })
-
- })
- onHide(()=>{
- console.log('App Hide')
- //关闭socket
-
- uni.$off('login', (data)=>{
- websocket.closeSocket()
- })
- })
- const heartBeatTest=()=> {
- //清除定时器
- clearInterval(globalTimer)
- //开启定时器定时检测心跳
- globalTimer = setInterval(() => {
- //发送消息给服务端
- websocket.sendMessage(
- JSON.stringify({ action: 'ping'}), //与服务端约定好消息格式
- null,
- () => {
- //如果失败则清除定时器
- clearInterval(globalTimer)
- }
- )
- }, 5000)
- }
- </script>
- <style>
- /*每个页面公共css */
- /* page{
- padding-top: 0;
- } */
- /* @import "@/static/iconfont.css" */
- .main{
- background-color: #d9d9d9;
- display: flex;
- flex-direction:column;
- width: 686px;
- /* height: 2000px; */
- }
- </style>
|