App.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <script>
  2. export default {
  3. globalData: {
  4. token: "",
  5. // 文本消息
  6. socketTask: null,
  7. urlData: "wss://www.toreskj.com/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. // 消息发送加载
  24. msgSetTime: null,
  25. },
  26. onLoad() {},
  27. onLaunch: function() {
  28. console.log('启动了')
  29. // App启动
  30. var that = this
  31. // uni.getStorage({
  32. // key: 'session_key',
  33. // success: function(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. var that = this
  46. // App展示在前台
  47. console.log('+++++++++++++++++++App Show-------------------------------------')
  48. // this.msgInit(); // 再进行请求
  49. uni.getStorage({
  50. key: 'session_key',
  51. success: function(res) {
  52. that.globalData.token = res.data
  53. // console.log('登录成功了')
  54. console.log(res)
  55. that.msgInit(); // 文本初始化
  56. },
  57. fail: function(err) {
  58. console.log(err)
  59. console.log('没有登录成功')
  60. }
  61. })
  62. },
  63. onHide: function() {
  64. // App不再再展示在前台
  65. this.close(); // 关闭
  66. },
  67. methods: {
  68. // 实时通信 - 初始化
  69. msgInit() {
  70. console.log('进入了实时通信初始化=--------------------------', this.globalData.token)
  71. var that = this
  72. // 连接webscoket
  73. that.globalData.socketTask = uni.connectSocket({
  74. url: this.globalData.urlData + "&token=" + that.globalData.token,
  75. success(data) {
  76. // console.log("websocket连接中");
  77. }
  78. });
  79. // 发送消息 - 获取对话列表
  80. var obj = {
  81. 'action': 'list', // 动作标识,必填
  82. 'recv_user_id': '', // 接收人用户id, 非必填
  83. 'data': {}
  84. }
  85. that.globalData.socketTask.onOpen((res) => {
  86. console.log('websocket开启了')
  87. // 清除重连定时器
  88. // clearTimeout(that.globalData.reconnectTimeOutObj)
  89. // that.notReconnect()
  90. that.globalData.socketTask.send({
  91. data: JSON.stringify(obj),
  92. async success(res) {
  93. that.globalData.is_open_socket = true
  94. console.log("消息发送成功");
  95. uni.hideLoading(); // 关闭通信异常弹框
  96. },
  97. });
  98. that.start() // 开启心跳机制
  99. })
  100. // // 接收服务器返回的消息
  101. // // 注:只有连接正常打开中 ,才能正常收到消息
  102. that.globalData.socketTask.onMessage((res) => {
  103. // console.log("收到服务器内容:");
  104. var val = JSON.parse(res.data)
  105. console.log('这是返回的内容---------', val)
  106. if (val.action == 'list') {
  107. clearTimeout(that.globalData.msgSetTime); // 消息发送成功,清除发送失败定时器
  108. console.log('这是发送人的ID', that.globalData.userId)
  109. var obj1 = {
  110. 'action': 'list', // 动作标识,必填
  111. 'recv_user_id': that.globalData.userId, // 接收人用户id, 非必填
  112. 'data': {
  113. 'msg_status': false, // 消息未读
  114. 'msg_info': '', // 发送消息
  115. }
  116. }
  117. that.globalData.socketTask.send({
  118. data: JSON.stringify(obj1),
  119. async success(res) {
  120. },
  121. });
  122. that.globalData.userId = null
  123. } else if (val.action == 'none') {
  124. console.log('进入了none', val)
  125. that.globalData.list = val.data
  126. this.$store.state.list = val.data
  127. } else if (val.action == 'recv_video') {
  128. that.globalData.video = val.data
  129. this.$store.state.video = val.data
  130. setTimeout(() => {
  131. console.log('跳转了')
  132. this.$store.getters.watchData;
  133. }, 1500)
  134. } else if (val.action == 'ok') {
  135. this.start(); // 心跳机制
  136. }
  137. // this.reset();
  138. // console.log('消息可正常接收');
  139. });
  140. // 连接断开
  141. // 如果希望websocket连接一直保持,在close或者error上绑定重新连接方法。
  142. this.globalData.socketTask.onClose((res) => {
  143. // console.log(res, '连接关闭');
  144. clearTimeout(this.globalData.timeoutObj);
  145. setTimeout(() => {
  146. that.reconnect();
  147. }, 5000)
  148. })
  149. // 连接失败
  150. that.globalData.socketTask.onError((res) => {
  151. // console.log(res, '连接错误');
  152. uni.showLoading({
  153. title: "通信异常,连接中..."
  154. })
  155. clearTimeout(this.globalData.timeoutObj);
  156. setTimeout(() => {
  157. that.reconnect();
  158. }, 5000)
  159. })
  160. },
  161. // 心跳机制
  162. start() {
  163. var that = this
  164. this.globalData.timeoutObj = setTimeout(() => {
  165. //这里发送一个心跳,后端收到后,返回一个心跳消息,
  166. //onmessage拿到返回的心跳就说明连接正常
  167. var obj = {
  168. 'action': 'keepalive', // 动作标识,必填
  169. 'type': 'App',
  170. 'recv_user_id': '', // 接收人用户id, 非必填
  171. 'data': {
  172. msg_status: false,
  173. msg_info: "",
  174. }
  175. }
  176. that.globalData.socketTask.send({
  177. data: JSON.stringify(obj),
  178. async success(res) {
  179. that.globalData.is_open_socket = true
  180. // console.log('消息发送成功1111')
  181. }
  182. });
  183. console.log('进入心跳机制了', that.globalData.socketTask)
  184. let ws_temp = null
  185. ws_temp = that.globalData.socketTask
  186. that.globalData.socketTask = ws_temp
  187. ws_temp = null
  188. }, this.globalData.timeout)
  189. },
  190. // 清除心跳机制
  191. reset() {
  192. // 清除定时器重新发送一个心跳信息
  193. // clearTimeout(this.globalData.timeoutObj);
  194. clearTimeout(this.globalData.timeoutObj);
  195. this.start();
  196. },
  197. // 重连
  198. reconnect() {
  199. // 防止多个方法调用,多处重连
  200. if (this.lockReconnect) {
  201. return;
  202. };
  203. this.lockReconnect = true;
  204. console.log('准备重连');
  205. //没连接上会一直重连,设置延迟避免请求过多
  206. this.globalData.reconnectTimeOutObj = setTimeout(() => {
  207. // 重新连接
  208. this.msgInit()
  209. console.log('正在重新连接')
  210. this.lockReconnect = false;
  211. }, 4000);
  212. },
  213. // 清除重连
  214. notReconnect() {
  215. clearTimeout(this.globalData.reconnectTimeOutObj)
  216. },
  217. // 手动关闭
  218. close() {
  219. var that = this
  220. // console.log('已经手动关闭了')
  221. that.globalData.socketTask.close()
  222. that.globalData.socketTask = null
  223. clearTimeout(that.globalData.timeoutObj);
  224. clearTimeout(that.globalData.reconnectTimeOutObj);
  225. },
  226. },
  227. }
  228. </script>
  229. <!-- // App.vue文件 中首行的位置引入,注意给style标签加入lang="scss"属性 -->
  230. <style lang="scss">
  231. /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
  232. @import "uview-ui/index.scss";
  233. </style>