App.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <script>
  2. var wsUrl="ws://127.0.0.1:1234";
  3. var pingpangTimes=null;
  4. var initTimes=null;
  5. var isSocketOpen=false;
  6. var socket=null;
  7. export default {
  8. data() {
  9. return {
  10. };
  11. },
  12. onLaunch: function() {
  13. this.initSocket()
  14. console.log('App Launch')
  15. var that=this;
  16. uni.$on('initSocket', () => {
  17. that.initSocket()
  18. })
  19. uni.$on('sendMsg', (item) => {
  20. that.sendMsg(item)
  21. })
  22. uni.$on('closeWebSocket', () => {
  23. that.closeWebSocket()
  24. })
  25. this.getEWechatSdk();
  26. // var initTimes=setInterval(function(){
  27. // if(!isSocketOpen){
  28. // try{
  29. // uni.closeSocket();
  30. // }
  31. // catch(err){
  32. // }
  33. // clearInterval(pingpangTimes);
  34. // that.initSocket()
  35. // }
  36. // },5000);
  37. },
  38. onShow: function() {
  39. console.log('App Show')
  40. },
  41. onHide: function() {
  42. console.log('App Hide')
  43. },
  44. onUnload() {
  45. console.log(pingpangTimes)
  46. clearInterval(pingpangTimes)
  47. // clearInterval(initTimes)
  48. },
  49. methods:{
  50. getEWechatSdk() {
  51. let eWechatSdk = ''
  52. if (/(Android)/i.test(navigator.userAgent)) {
  53. eWechatSdk = 'jWeixin'
  54. } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  55. eWechatSdk = 'wx'
  56. } else {
  57. eWechatSdk = 'jWeixin'
  58. }
  59. uni.setStorageSync("wxSdk",eWechatSdk)
  60. },
  61. closeWebSocket(){
  62. if(socket!=null){
  63. uni.closeSocket();
  64. }
  65. clearInterval(pingpangTimes)
  66. },
  67. reConnect(){
  68. var that=this;
  69. try{
  70. uni.closeSocket();
  71. }
  72. catch(e){
  73. }
  74. setTimeout(function(){
  75. that.initSocket();
  76. },10000);
  77. },
  78. initSocket(){
  79. var that=this;
  80. console.log("initSocket")
  81. //创建一个socket连接
  82. socket=uni.connectSocket({
  83. url:wsUrl,
  84. multiple:true,
  85. success: res=>{
  86. //先确保清除了之前的心跳定时器
  87. clearInterval(pingpangTimes)
  88. uni.onSocketMessage((res)=>{
  89. const redata = JSON.parse(res.data);
  90. console.log(redata);
  91. if(redata.cmd=='deleteId'){
  92. uni.$emit('deleteId');
  93. }
  94. else if(redata.cmd=='init'){
  95. uni.$emit('init',redata.data);
  96. }
  97. else if(redata.cmd=='reload'){
  98. uni.$emit('reload');
  99. }
  100. else if(redata.cmd=='sendStatus'){
  101. uni.$emit('sendStatus',redata.data);
  102. }
  103. })
  104. pingpangTimes=setInterval(()=>{
  105. var data={cmd:"heartbeat"};
  106. console.log(data)
  107. uni.sendSocketMessage({
  108. data:JSON.stringify(data),
  109. success:()=>{
  110. console.log('WebSocket发送心条数据!');
  111. },
  112. fail:(res)=>{
  113. console.log(data);
  114. isSocketOpen=false
  115. }
  116. });
  117. },2000)
  118. },
  119. error: res=>{
  120. uni.$emit('websocket',0);
  121. console.log(res)
  122. },
  123. })
  124. //监听socket打开
  125. uni.onSocketOpen(()=>{
  126. isSocketOpen=true
  127. console.log('WebSocket连接已打开!!');
  128. // uni.$emit('websocket',1);
  129. uni.showToast({
  130. title: "插件已打开",
  131. icon: 'none',
  132. });
  133. })
  134. //监听socket关闭
  135. uni.onSocketClose(()=>{
  136. isSocketOpen=false
  137. clearInterval(pingpangTimes)
  138. console.log('WebSocket连接已关闭!');
  139. // uni.$emit('websocket',0);
  140. uni.showToast({
  141. title: "插件离线",
  142. icon: 'none',
  143. });
  144. //重连
  145. that.reConnect()
  146. })
  147. //监听socket错误
  148. uni.onSocketError(()=>{
  149. isSocketOpen=false
  150. clearInterval(pingpangTimes)
  151. console.log('WebSocket连接打开失败');
  152. // uni.$emit('websocket',0);
  153. uni.showToast({
  154. title: "插件离线",
  155. icon: 'none',
  156. });
  157. that.reConnect()
  158. })
  159. },
  160. sendMsg(data){
  161. if(isSocketOpen){
  162. uni.sendSocketMessage({
  163. data: JSON.stringify(data),
  164. success:()=>{
  165. console.log("发送成功")
  166. },
  167. fail:()=>{
  168. console.log("发送失败")
  169. }
  170. });
  171. }
  172. },
  173. }
  174. }
  175. </script>
  176. <style lang="scss">
  177. /*每个页面公共css */
  178. @import "@/uni_modules/uview-ui/index.scss";
  179. view{
  180. box-sizing: border-box;
  181. }
  182. .ellipsis{
  183. overflow: hidden;
  184. text-overflow: ellipsis;
  185. white-space: nowrap;
  186. }
  187. .ellipsis2{
  188. overflow:hidden;
  189. text-overflow:ellipsis;
  190. display:-webkit-box;
  191. -webkit-box-orient:vertical;
  192. -webkit-line-clamp:2;
  193. }
  194. .no-data-box{
  195. height:100%;
  196. width: 100%;
  197. display: flex;
  198. justify-content: center;
  199. align-items: center;
  200. flex-direction: column;
  201. image{
  202. width: 264upx;
  203. height: 212upx;
  204. }
  205. .empty-title{
  206. margin-top: 20rpx;
  207. font-size: 28rpx;
  208. color: gray;
  209. }
  210. }
  211. </style>
  212. <style lang="less">
  213. </style>