ReadSocket.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <view style="display: none;">阅读socket正在运行...</view>
  3. </template>
  4. <script>
  5. import {
  6. info
  7. } from '@/api/user'
  8. export default {
  9. name: 'ReadSocket',
  10. props: {
  11. value: String,
  12. courseId: String
  13. },
  14. data() {
  15. return {
  16. websocket: null,
  17. stompClient: null,
  18. timer: null,
  19. fileId: '',
  20. userId: '',
  21. msgShow: false
  22. }
  23. },
  24. watch: {
  25. value: {
  26. handler() {
  27. this.fileId = this.value
  28. }
  29. }
  30. },
  31. created() {
  32. this.fileId = this.value
  33. },
  34. mounted() {
  35. let token = uni.getStorageSync('token');
  36. info(token).then(res => {
  37. console.log(res)
  38. this.userId = res.id
  39. this.connection()
  40. })
  41. },
  42. destroyed() {
  43. // 关闭连接
  44. uni.closeSocket()
  45. // 清理定时器
  46. if(this.timer!=null){
  47. clearInterval(this.timer)
  48. }
  49. },
  50. methods: {
  51. beat() {
  52. // 清理定时器
  53. if (this.timer!=null) {
  54. clearInterval(this.timer)
  55. }
  56. let that = this
  57. // 10秒联系一次
  58. this.timer = setInterval(() => {
  59. // 发送消息
  60. that.sendPing()
  61. }, 10000)
  62. // 维持心跳
  63. this.sendPing();
  64. },
  65. sendPing() {
  66. // 发送消息,断线重连
  67. uni.sendSocketMessage({
  68. data: 'ping',
  69. success: res => {
  70. return;
  71. },
  72. fail: err => {
  73. // 未连接打开websocket连接
  74. this.connection();
  75. }
  76. });
  77. },
  78. // 建立连接
  79. connection() {
  80. // socket服务器
  81. let host = this.$urls.socket
  82. // 如果为空,则表示使用自己的
  83. if (host == null || host === '') {
  84. const https = 'https:' == document.location.protocol ? true : false;
  85. host = https ? 'wss://'+window.location.host : 'ws://' + window.location.host
  86. }
  87. let that = this
  88. const socketUrl = `${host}/api/socket/read/${this.userId}/${this.courseId}/${this.fileId}`
  89. console.log('开始连接:', socketUrl);
  90. uni.connectSocket({
  91. url: socketUrl
  92. });
  93. uni.onSocketError(function(res) {
  94. console.log('WebSocket连接打开失败,请检查!');
  95. });
  96. // 连接打开开始计时
  97. uni.onSocketOpen(function(res) {
  98. console.log('WebSocket连接已打开!');
  99. // 开始发送数据
  100. that.beat()
  101. });
  102. // 关闭连接
  103. uni.onSocketClose(function(res) {
  104. console.log('WebSocket 已关闭!');
  105. });
  106. // 收到消息
  107. uni.onSocketMessage(function(res) {
  108. console.log('msg', res.data)
  109. const data = JSON.parse(res.data)
  110. // 意外终止消息
  111. if (data.stopped) {
  112. uni.showToast({
  113. icon: 'none',
  114. title: data.stopMsg,
  115. duration: 3500
  116. })
  117. that.msgShow = true
  118. setTimeout(() => {
  119. // 切到课程页面回去
  120. uni.switchTab({
  121. url: '/pages/course/index'
  122. })
  123. }, 3500)
  124. return
  125. }
  126. if (data.all && !that.msgShow) {
  127. uni.showToast({
  128. title: '恭喜,您已经学完全部课程啦!',
  129. icon: 'none',
  130. duration: 2000
  131. });
  132. that.msgShow = true
  133. return
  134. }
  135. if (data.current && !that.msgShow) {
  136. uni.showToast({
  137. title: '恭喜,您已学完本课件!',
  138. icon: 'none',
  139. duration: 2000
  140. });
  141. that.msgShow = true
  142. return
  143. }
  144. })
  145. }
  146. }
  147. }
  148. </script>