<!-- <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(`wss://zhongyao.fscuc.cn/webSocket/sentMessage/${data.userId}`,() => {
				websocket.connectSocket(`wss://zhongyao.fscuc.cn/webSocket/sentMessage/${data.userId}`,() => {
				//如果连接成功则发送心跳检测
				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)
			}
		)
	}, 10000)
}

</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>
