App.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <script>
  2. export default {
  3. globalData: {
  4. token: "",
  5. // 文本消息
  6. socketTask: null,
  7. // urlData: "ws://114.115.147.140:12345/api/api_gateway?method=control_center.real_time.im_message",
  8. urlData: "ws://192.168.1.17:12345/api/api_gateway?method=control_center.real_time.im_message",
  9. is_open_socket: false, // 确保websocket是打开状态
  10. list: [],
  11. userId: null, // 用户ID
  12. // 即构
  13. video: null, //房间密码和token
  14. // 测试 - 心跳检测
  15. status: null, // websocket是否关闭
  16. lockReconnect: false, // 避免重复连接
  17. // timeout: 5000, // 多少秒执行监测
  18. timeout: 10000, // 多少秒执行监测
  19. timeoutObj: null, // 检测服务器端是否还活着
  20. reconnectTimeOutObj: null, // 重连之后多久再次重连
  21. // 测试
  22. app: [],
  23. hdskdh: null
  24. },
  25. onLoad() {},
  26. onLaunch: function() {
  27. console.log('启动了')
  28. // App启动
  29. var that = this
  30. uni.getStorage({
  31. key: 'session_key',
  32. success: function(res) {
  33. // console.log(res)
  34. that.globalData.token = res.data
  35. // console.log('登录成功了')
  36. that.msgInit(); // 文本初始化
  37. },
  38. fail: function(err) {
  39. // console.log(err)
  40. // console.log('没有登录成功')
  41. }
  42. })
  43. },
  44. onShow: function() {
  45. // App展示在前台
  46. console.log('+++++++++++++++++++App Show-------------------------------------')
  47. // this.close() // 先关闭
  48. // if (this.globalData.socketTask == null && this.globalData.token !== '') {
  49. // this.msgInit();
  50. // } else if (this.globalData.socketTask !== null) {
  51. // this.close() // 先关闭
  52. // this.msgInit(); // 再进行请求
  53. // }
  54. // console.log(this.globalData.socketTask)
  55. // this.msgInit(); // 再进行请求
  56. },
  57. onHide: function() {
  58. // App不再再展示在前台
  59. },
  60. methods: {
  61. // 实时通信 - 初始化
  62. msgInit() {
  63. var that = this
  64. // 连接webscoket
  65. that.globalData.socketTask = uni.connectSocket({
  66. url: this.globalData.urlData + "&token=" + that.globalData.token,
  67. success(data) {
  68. console.log("websocket连接中");
  69. }
  70. });
  71. // 发送消息 - 获取对话列表
  72. var obj = {
  73. 'action': 'list', // 动作标识,必填
  74. 'recv_user_id': '', // 接收人用户id, 非必填
  75. 'data': {}
  76. }
  77. that.globalData.socketTask.onOpen((res) => {
  78. console.log('websocket开启了')
  79. // 清除重连定时器
  80. // clearInterval(that.globalData.reconnectTimeOutObj)
  81. that.notReconnect()
  82. // 开启检测
  83. that.start()
  84. that.globalData.socketTask.send({
  85. data: JSON.stringify(obj),
  86. async success(res) {
  87. that.globalData.is_open_socket = true
  88. console.log("消息发送成功");
  89. },
  90. });
  91. })
  92. // // 接收服务器返回的消息
  93. // // 注:只有连接正常打开中 ,才能正常收到消息
  94. that.globalData.socketTask.onMessage((res) => {
  95. console.log("收到服务器内容:");
  96. var val = JSON.parse(res.data)
  97. console.log('这是返回的内容---------', val)
  98. if (val.action == 'list') {
  99. var obj1 = {
  100. 'action': 'list', // 动作标识,必填
  101. 'recv_user_id': that.globalData.userId, // 接收人用户id, 非必填
  102. 'data': {
  103. 'msg_status': false, // 消息未读
  104. 'msg_info': '', // 发送消息
  105. }
  106. }
  107. that.globalData.socketTask.send({
  108. data: JSON.stringify(obj1),
  109. async success(res) {
  110. },
  111. });
  112. } else if (val.action == 'none') {
  113. console.log('进入了none')
  114. that.globalData.list = val.data
  115. this.$store.state.list = val.data
  116. } else if (val.action == 'recv_video') {
  117. that.globalData.video = val.data
  118. this.$store.state.video = val.data
  119. }
  120. this.reset();
  121. console.log('消息可正常接收');
  122. });
  123. // 连接断开
  124. // 如果希望websocket连接一直保持,在close或者error上绑定重新连接方法。
  125. this.globalData.socketTask.onClose((res) => {
  126. console.log(res, '连接关闭');
  127. clearInterval(this.globalData.timeoutObj);
  128. that.reconnect();
  129. })
  130. // 连接失败
  131. that.globalData.socketTask.onError((res) => {
  132. console.log(res, '连接错误');
  133. clearInterval(this.globalData.timeoutObj);
  134. that.reconnect();
  135. })
  136. },
  137. // 心跳机制
  138. start() {
  139. var that = this
  140. this.globalData.timeoutObj = setInterval(() => {
  141. //这里发送一个心跳,后端收到后,返回一个心跳消息,
  142. //onmessage拿到返回的心跳就说明连接正常
  143. var obj = {
  144. 'action': 'list', // 动作标识,必填
  145. 'recv_user_id': '', // 接收人用户id, 非必填
  146. 'data': {}
  147. }
  148. that.globalData.socketTask.send({
  149. data: JSON.stringify(obj),
  150. async success(res) {
  151. that.globalData.is_open_socket = true
  152. console.log('消息发送成功1111')
  153. }
  154. });
  155. console.log('进入心跳机制了', this.globalData.timeout)
  156. }, this.globalData.timeout)
  157. },
  158. // 清除心跳机制
  159. reset() {
  160. // 清除定时器重新发送一个心跳信息
  161. // clearTimeout(this.globalData.timeoutObj);
  162. clearInterval(this.globalData.timeoutObj);
  163. this.start();
  164. },
  165. // 重连
  166. reconnect() {
  167. // 防止多个方法调用,多处重连
  168. if (this.lockReconnect) {
  169. return;
  170. };
  171. this.lockReconnect = true;
  172. console.log('准备重连');
  173. //没连接上会一直重连,设置延迟避免请求过多
  174. this.globalData.reconnectTimeOutObj = setInterval(() => {
  175. // 重新连接
  176. this.msgInit()
  177. console.log('正在重新连接')
  178. this.lockReconnect = false;
  179. }, 4000);
  180. },
  181. // 清除重连
  182. notReconnect() {
  183. clearInterval(this.globalData.reconnectTimeOutObj)
  184. },
  185. // 手动关闭
  186. close() {
  187. var that = this
  188. console.log('已经手动关闭了')
  189. that.globalData.socketTask.close()
  190. clearInterval(that.globalData.timeoutObj);
  191. clearInterval(that.globalData.reconnectTimeOutObj);
  192. },
  193. },
  194. }
  195. </script>
  196. <!-- // App.vue文件 中首行的位置引入,注意给style标签加入lang="scss"属性 -->
  197. <style lang="scss">
  198. /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
  199. @import "uview-ui/index.scss";
  200. </style>