123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <script>
- var wsUrl="ws://127.0.0.1:1234";
- var pingpangTimes=null;
- var initTimes=null;
- var isSocketOpen=false;
- var socket=null;
- export default {
- data() {
- return {
- };
- },
- onLaunch: function() {
- this.initSocket()
- console.log('App Launch')
- var that=this;
- uni.$on('initSocket', () => {
- that.initSocket()
- })
- uni.$on('sendMsg', (item) => {
- that.sendMsg(item)
- })
- uni.$on('closeWebSocket', () => {
- that.closeWebSocket()
- })
- this.getEWechatSdk();
- // var initTimes=setInterval(function(){
- // if(!isSocketOpen){
- // try{
- // uni.closeSocket();
- // }
- // catch(err){
-
- // }
- // clearInterval(pingpangTimes);
- // that.initSocket()
- // }
- // },5000);
-
- },
- onShow: function() {
- console.log('App Show')
- },
- onHide: function() {
- console.log('App Hide')
- },
- onUnload() {
- console.log(pingpangTimes)
- clearInterval(pingpangTimes)
- // clearInterval(initTimes)
- },
- methods:{
- getEWechatSdk() {
- let eWechatSdk = ''
- if (/(Android)/i.test(navigator.userAgent)) {
- eWechatSdk = 'jWeixin'
- } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
- eWechatSdk = 'wx'
- } else {
- eWechatSdk = 'jWeixin'
- }
- uni.setStorageSync("wxSdk",eWechatSdk)
- },
- closeWebSocket(){
- if(socket!=null){
- uni.closeSocket();
- }
- clearInterval(pingpangTimes)
- },
- reConnect(){
- var that=this;
- try{
- uni.closeSocket();
- }
- catch(e){
-
- }
- setTimeout(function(){
- that.initSocket();
- },10000);
-
- },
- initSocket(){
- var that=this;
- console.log("initSocket")
- //创建一个socket连接
- socket=uni.connectSocket({
- url:wsUrl,
- multiple:true,
- success: res=>{
- //先确保清除了之前的心跳定时器
- clearInterval(pingpangTimes)
- uni.onSocketMessage((res)=>{
- const redata = JSON.parse(res.data);
- console.log(redata);
- if(redata.cmd=='deleteId'){
- uni.$emit('deleteId');
- }
- else if(redata.cmd=='init'){
- uni.$emit('init',redata.data);
- }
- else if(redata.cmd=='reload'){
- uni.$emit('reload');
- }
- else if(redata.cmd=='sendStatus'){
- uni.$emit('sendStatus',redata.data);
- }
-
- })
- pingpangTimes=setInterval(()=>{
- var data={cmd:"heartbeat"};
- console.log(data)
- uni.sendSocketMessage({
- data:JSON.stringify(data),
- success:()=>{
- console.log('WebSocket发送心条数据!');
- },
- fail:(res)=>{
- console.log(data);
- isSocketOpen=false
- }
- });
- },2000)
- },
- error: res=>{
- uni.$emit('websocket',0);
- console.log(res)
- },
- })
- //监听socket打开
- uni.onSocketOpen(()=>{
- isSocketOpen=true
- console.log('WebSocket连接已打开!!');
- // uni.$emit('websocket',1);
- uni.showToast({
- title: "插件已打开",
- icon: 'none',
- });
- })
- //监听socket关闭
- uni.onSocketClose(()=>{
- isSocketOpen=false
- clearInterval(pingpangTimes)
- console.log('WebSocket连接已关闭!');
- // uni.$emit('websocket',0);
- uni.showToast({
- title: "插件离线",
- icon: 'none',
- });
- //重连
- that.reConnect()
- })
- //监听socket错误
- uni.onSocketError(()=>{
- isSocketOpen=false
- clearInterval(pingpangTimes)
- console.log('WebSocket连接打开失败');
- // uni.$emit('websocket',0);
- uni.showToast({
- title: "插件离线",
- icon: 'none',
- });
- that.reConnect()
- })
- },
- sendMsg(data){
- if(isSocketOpen){
- uni.sendSocketMessage({
- data: JSON.stringify(data),
- success:()=>{
-
- console.log("发送成功")
- },
- fail:()=>{
- console.log("发送失败")
- }
- });
-
- }
-
- },
-
- }
- }
- </script>
- <style lang="scss">
- /*每个页面公共css */
- @import "@/uni_modules/uview-ui/index.scss";
- view{
- box-sizing: border-box;
- }
- .ellipsis{
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .ellipsis2{
- overflow:hidden;
- text-overflow:ellipsis;
- display:-webkit-box;
- -webkit-box-orient:vertical;
- -webkit-line-clamp:2;
- }
-
-
- .no-data-box{
- height:100%;
- width: 100%;
- display: flex;
- justify-content: center;
- align-items: center;
- flex-direction: column;
- image{
- width: 264upx;
- height: 212upx;
- }
- .empty-title{
- margin-top: 20rpx;
- font-size: 28rpx;
- color: gray;
-
- }
- }
- </style>
- <style lang="less">
- </style>
|