123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- <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();
- },200);
-
- },
- 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=='refresh'){
- uni.$emit('refresh');
- }
- else if(redata.cmd=='sendMsg'){
- // //发送消息
- // uni.$emit('sendMsg',redata.data);
- }
- else if(redata.cmd=='sendStatus'){
- uni.$emit('sendStatus',redata.data);
- }
-
- })
- pingpangTimes=setInterval(()=>{
- var key=uni.getStorageSync("key");
- var data={cmd:"heartbeat",data:key};
- 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',
- });
- uni.$emit('init',0);
- //重连
- that.reConnect()
- })
- //监听socket错误
- uni.onSocketError(()=>{
- isSocketOpen=false
- clearInterval(pingpangTimes)
- console.log('WebSocket连接打开失败');
- // uni.$emit('websocket',0);
- uni.$emit('init',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";
- @import '@/assets/css/theme.scss';
- @import '@/assets/css/common.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;
-
- }
- }
-
-
-
- .w-calc-30 {
- padding: 0 30rpx;
- width: calc(100% - 60rpx);
- }
-
- .hb {
- height: 100%;
- box-sizing: border-box;
- }
-
- .hidden {
- overflow: hidden;
- }
-
- .base-color {
- color: $--base-color;
- }
-
- .base-color-2 {
- color: $--base-color2;
- }
-
- .base-color-3 {
- color: $--base-color3;
- }
- .base-color-9 {
- color: $--base-color-9;
- }
- .base-color-8 {
- color: $--base-color-f8;
- }
- .base-color-6 {
- color: $--base-color-6;
- }
- .base-color-gray {
- color: $--base-color-gray;
- }
- .base-color-red{
- color: $--base-color-red;
- }
- .base-color-dark {
- color: $--base-color-dark;
- }
-
- .base-color-dark2 {
- color: $--base-color-dark2;
- }
-
- .base-price {
- color: $--base-color-price;
- }
-
- .base-success {
- color: $--base-color-success;
- }
-
- .base-bg {
- background: $--base-bg;
- }
-
- .base-bg-2 {
- background: $--base-bg2;
- }
- .base-bg-red{
- background: $--base-bg-red;
- }
- .base-bg-f{
- background-color: $--base-bg-f;
- }
- .base-bg-f8{
- background-color: $--base-color-f8;
- }
- .base-bg-f5{
- background-color: $--base-color-f5;
- }
- .base-bg-9{
- background-color: $--base-color-9;
- }
- .base-bg-blue{
- background:$--base-bg-blue;
- }
- .base-bg-sure{
- background:$--base-sure-bg;
- }
- .base-bg-orange{
- background:$--base-bg-orange;
- }
- .base-bg-false{
- background:$--base-false-bg;
- }
- .bor-blue{
- border: 2rpx solid $--base-bor-blue;
- }
- .bor-red{
- border: 2rpx solid $--base-bor-red;
- }
- .colorf {
- color: #fff;
- }
-
- .bgf {
- background: #fff;
- }
-
- .fixed {
- position: fixed;
- }
-
- .absolute {
- position: absolute;
- }
-
- .relative {
- position: relative;
- }
-
- .w100 {
- width: 100%;
- }
-
- .h100 {
- height: 100%;
- }
-
- .card {
- background: #fff;
- border-radius: 15rpx;
- }
-
- .cover-height {
- height: 100%;
- display: flex;
- flex-direction: column;
- box-sizing: border-box;
- }
-
- .row {
- display: flex;
- flex-direction: row;
- }
-
- .column {
- display: flex;
- flex-direction: column;
- }
-
- .justify-start {
- display: flex;
- justify-content: flex-start;
- }
-
- .justify-center {
- display: flex;
- justify-content: center;
- }
-
- .justify-end {
- display: flex;
- justify-content: flex-end;
- }
-
- .justify-around {
- display: flex;
- justify-content: space-around;
- }
- .justify-evenly {
- display: flex;
- justify-content: space-evenly;
- }
-
- .justify-between {
- display: flex;
- justify-content: space-between;
- }
-
- .align-start {
- display: flex;
- align-items: flex-start;
- }
-
- .align-center {
- display: flex;
- align-items: center;
- }
-
- .align-end {
- display: flex;
- align-items: flex-end;
- }
-
- .center {
- display: flex;
- justify-content: center;
- align-items: center;
- }
-
- .centerV {
- display: flex;
- justify-content: center;
- align-items: center;
- flex-direction: column;
- }
-
- .wrap {
- flex-wrap: wrap;
- }
-
- .flex-1 {
- flex: 1;
- }
-
- .ellipsis {
- overflow: hidden;
- text-overflow: ellipsis;
- display: -webkit-box;
- -webkit-box-orient: vertical;
- box-sizing: border-box;
- width: 100%;
- -webkit-line-clamp: 1;
- }
-
- .lines-2 {
- -webkit-line-clamp: 2 !important;
- }
-
- .lines-3 {
- -webkit-line-clamp: 3 !important;
- }
-
- .bold {
- font-weight: bold;
- }
-
- .line-through {
- text-decoration: line-through;
- }
-
- .nowrap {
- white-space: nowrap;
- }
-
- .scrollx {
- overflow-x: scroll;
- }
-
- .scrolly {
- overflow-y: scroll;
- }
-
- .cvauto {
- content-visibility: auto;
- }
- </style>
- <style lang="less">
- /*每个页面公共css */
- @import './assets/iconfont/iconfont.css';
- @import './assets/css/common.less';
- </style>
|