websocket-socketTask.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <view>
  3. <page-head title="websocket通讯示例"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view class="uni-btn-v">
  6. <view class="websocket-msg">{{showMsg}}</view>
  7. <button type="primary" @click="connect">连接websocket服务</button>
  8. <button v-show="connected" type="primary" @click="send">发送一条消息</button>
  9. <button type="primary" @click="close">断开websocket服务</button>
  10. <view class="websocket-tips">发送消息后会收到一条服务器返回的消息(与发送的消息内容一致)</view>
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. let platform = uni.getSystemInfoSync().platform
  17. export default {
  18. data() {
  19. return {
  20. connected: false,
  21. connecting: false,
  22. socketTask: false,
  23. msg: false,
  24. }
  25. },
  26. computed: {
  27. showMsg() {
  28. if (this.connected) {
  29. if (this.msg) {
  30. return '收到消息:' + this.msg
  31. } else {
  32. return '等待接收消息'
  33. }
  34. } else {
  35. return '尚未连接'
  36. }
  37. }
  38. },
  39. onUnload() {
  40. uni.hideLoading()
  41. if (this.socketTask && this.socketTask.close) {
  42. this.socketTask.close()
  43. }
  44. },
  45. methods: {
  46. connect() {
  47. if (this.connected || this.connecting) {
  48. uni.showModal({
  49. content: '正在连接或者已经连接,请勿重复连接',
  50. showCancel: false
  51. })
  52. return false
  53. }
  54. this.connecting = true
  55. uni.showLoading({
  56. title: '连接中...'
  57. })
  58. this.socketTask = uni.connectSocket({
  59. url: 'wss://echo.websocket.org',
  60. data() {
  61. return {
  62. msg: 'Hello'
  63. }
  64. },
  65. // #ifdef MP
  66. header: {
  67. 'content-type': 'application/json'
  68. },
  69. // #endif
  70. // #ifdef MP-WEIXIN
  71. method: 'GET',
  72. // #endif
  73. success(res) {
  74. // 这里是接口调用成功的回调,不是连接成功的回调,请注意
  75. },
  76. fail(err) {
  77. // 这里是接口调用失败的回调,不是连接失败的回调,请注意
  78. }
  79. })
  80. console.log(this.socketTask);
  81. this.socketTask.onOpen((res) => {
  82. this.connecting = false
  83. this.connected = true
  84. uni.hideLoading()
  85. uni.showToast({
  86. icon: 'none',
  87. title: '连接成功'
  88. })
  89. console.log('onOpen', res);
  90. })
  91. this.socketTask.onError((err) => {
  92. this.connecting = false
  93. this.connected = false
  94. uni.hideLoading()
  95. uni.showModal({
  96. content: '连接失败,可能是websocket服务不可用,请稍后再试',
  97. showCancel: false
  98. })
  99. console.log('onError', err);
  100. })
  101. this.socketTask.onMessage((res) => {
  102. this.msg = res.data
  103. console.log('onMessage', res)
  104. })
  105. this.socketTask.onClose((res) => {
  106. this.connected = false
  107. this.startRecive = false
  108. this.socketTask = false
  109. this.msg = false
  110. console.log('onClose', res)
  111. })
  112. console.log('task', this.socketTask)
  113. },
  114. send() {
  115. this.socketTask.send({
  116. data: 'from ' + platform + ' : ' + parseInt(Math.random() * 10000).toString(),
  117. success(res) {
  118. console.log(res);
  119. },
  120. fail(err) {
  121. console.log(err);
  122. }
  123. })
  124. },
  125. close() {
  126. if (this.socketTask && this.socketTask.close) {
  127. this.socketTask.close()
  128. }
  129. }
  130. }
  131. }
  132. </script>
  133. <style>
  134. .uni-btn-v {
  135. padding: 10rpx 0;
  136. }
  137. .uni-btn-v button {
  138. margin: 20rpx 0;
  139. }
  140. .websocket-msg {
  141. padding: 40px 0px;
  142. text-align: center;
  143. font-size: 14px;
  144. line-height: 40px;
  145. color: #666666;
  146. }
  147. .websocket-tips{
  148. padding: 40px 0px;
  149. text-align: center;
  150. font-size: 14px;
  151. line-height: 24px;
  152. color: #666666;
  153. }
  154. </style>