websocket.uvue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <page-head title="websocket通讯示例"></page-head>
  3. <view class="uni-padding-wrap">
  4. <view class="uni-btn-v">
  5. <text class="websocket-msg">{{ showMsg }}</text>
  6. <button class="uni-btn-v" type="primary" @click="connect">
  7. 连接websocket服务
  8. </button>
  9. <button class="uni-btn-v" v-show="connected" type="primary" @click="send">
  10. 发送一条消息
  11. </button>
  12. <button class="uni-btn-v" type="primary" @click="close">
  13. 断开websocket服务
  14. </button>
  15. <text class="websocket-tips">发送消息后会收到一条服务器返回的消息(与发送的消息内容一致)</text>
  16. <text class="websocket-tips">web端和小程序端uni-push功能、app-android端和app-ios端的web-view组件日志回显、app-harmony端的日志回显会占用一个socket链接,此时使用全局的socket api会引发混乱。应使用socketTask操作websocket链接。</text>
  17. <text class="websocket-tips">小程序端日志回显功能也会占用一个socket链接,如果不希望使用此功能可以在HBuilderX控制台关闭日志回显。</text>
  18. <button class="uni-btn-v" type="primary" @click="goSocketTask">
  19. 跳转 socketTask 示例
  20. </button>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. export default {
  26. data() {
  27. return {
  28. connected: false,
  29. connecting: false,
  30. msg: '',
  31. roomId: '',
  32. platform: '',
  33. }
  34. },
  35. computed: {
  36. showMsg() : string {
  37. if (this.connected) {
  38. if (this.msg.length > 0) {
  39. return '收到消息:' + this.msg
  40. } else {
  41. return '等待接收消息'
  42. }
  43. } else {
  44. return '尚未连接'
  45. }
  46. },
  47. },
  48. onLoad() {
  49. this.platform = uni.getDeviceInfo().platform as string
  50. },
  51. onUnload() {
  52. uni.closeSocket({
  53. code: 1000,
  54. reason: 'close reason from client',
  55. success: (res : any) => {
  56. console.log('uni.closeSocket success', res)
  57. },
  58. fail: (err : any) => {
  59. console.log('uni.closeSocket fail', err)
  60. },
  61. } as CloseSocketOptions)
  62. uni.hideLoading()
  63. },
  64. methods: {
  65. connect() {
  66. if (this.connected || this.connecting) {
  67. uni.showModal({
  68. content: '正在连接或者已经连接,请勿重复连接',
  69. showCancel: false,
  70. })
  71. return
  72. }
  73. this.connecting = true
  74. uni.showLoading({
  75. title: '连接中...',
  76. })
  77. uni.connectSocket({
  78. url: 'wss://websocket.dcloud.net.cn',
  79. header: null,
  80. protocols: null,
  81. success: (res : any) => {
  82. // 这里是接口调用成功的回调,不是连接成功的回调,请注意
  83. console.log('uni.connectSocket success', res)
  84. },
  85. fail: (err : any) => {
  86. // 这里是接口调用失败的回调,不是连接失败的回调,请注意
  87. console.log('uni.connectSocket fail', err)
  88. },
  89. })
  90. uni.onSocketOpen((res) => {
  91. this.connecting = false
  92. this.connected = true
  93. uni.hideLoading()
  94. uni.showToast({
  95. icon: 'none',
  96. title: '连接成功',
  97. })
  98. console.log('onOpen', res)
  99. })
  100. uni.onSocketError((err) => {
  101. this.connecting = false
  102. this.connected = false
  103. uni.hideLoading()
  104. uni.showModal({
  105. content: '连接失败,可能是websocket服务不可用,请稍后再试',
  106. showCancel: false,
  107. })
  108. console.log('onError', err)
  109. })
  110. uni.onSocketMessage((res) => {
  111. if(res.data instanceof ArrayBuffer){
  112. var int8 = new Int8Array(res.data)
  113. this.msg = int8.toString()
  114. console.log('onMessage', res)
  115. }else{
  116. this.msg = res.data as string
  117. console.log('onMessage', res)
  118. }
  119. })
  120. uni.onSocketClose((res) => {
  121. this.connected = false
  122. this.msg = ''
  123. console.log('onClose', res)
  124. })
  125. },
  126. send() {
  127. uni.sendSocketMessage({
  128. data:
  129. 'from ' +
  130. this.platform +
  131. ' : ' +
  132. parseInt((Math.random() * 10000).toString()).toString(),
  133. success: (res : any) => {
  134. console.log(res)
  135. },
  136. fail: (err : any) => {
  137. console.log(err)
  138. },
  139. } as SendSocketMessageOptions)
  140. },
  141. close() {
  142. uni.closeSocket({
  143. code: 1000,
  144. reason: 'close reason from client',
  145. success: (res : any) => {
  146. console.log('uni.closeSocket success', res)
  147. },
  148. fail: (err : any) => {
  149. console.log('uni.closeSocket fail', err)
  150. },
  151. } as CloseSocketOptions)
  152. },
  153. goSocketTask() {
  154. uni.navigateTo({
  155. url: '/pages/API/websocket/socketTask',
  156. })
  157. }
  158. },
  159. }
  160. </script>
  161. <style>
  162. .uni-btn-v {
  163. padding: 5px 0;
  164. }
  165. .uni-btn-v {
  166. margin: 10px 0;
  167. }
  168. .websocket-msg {
  169. padding: 40px 0px;
  170. text-align: center;
  171. font-size: 14px;
  172. line-height: 40px;
  173. color: #666666;
  174. }
  175. .websocket-tips {
  176. padding: 10px 0px;
  177. text-align: center;
  178. font-size: 14px;
  179. line-height: 24px;
  180. color: #666666;
  181. }
  182. </style>