socketTask.uvue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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" v-show="connected" type="primary" @click="sendArrayBuffer">
  13. 发送一条ArrayBuffer消息,返回也是ArrayBuffer
  14. </button>
  15. <button class="uni-btn-v" type="primary" @click="close">
  16. 断开websocket服务
  17. </button>
  18. <text class="websocket-tips">发送消息后会收到一条服务器返回的消息(与发送的消息内容一致)</text>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. data() {
  25. return {
  26. connected: false,
  27. connecting: false,
  28. socketTask: null as SocketTask | null,
  29. msg: '',
  30. platform: '',
  31. //自动化测试例专用
  32. jest_result: 0,
  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.hideLoading()
  53. let task = this.socketTask
  54. if (task != null) {
  55. task.close({
  56. code: 1000,
  57. reason: 'close reason from client',
  58. success: (res : any) => {
  59. console.log('uni.closeSocket success', res)
  60. },
  61. fail: (err : any) => {
  62. console.log('uni.closeSocket fail', err)
  63. },
  64. } as CloseSocketOptions)
  65. }
  66. },
  67. methods: {
  68. connect() {
  69. if (this.connected || this.connecting) {
  70. uni.showModal({
  71. content: '正在连接或者已经连接,请勿重复连接',
  72. showCancel: false,
  73. })
  74. return
  75. }
  76. this.connecting = true
  77. uni.showLoading({
  78. title: '连接中...',
  79. })
  80. this.socketTask = uni.connectSocket({
  81. url: 'wss://websocket.dcloud.net.cn',
  82. success: (res : any) => {
  83. // 这里是接口调用成功的回调,不是连接成功的回调,请注意
  84. console.log('uni.connectSocket success', res)
  85. },
  86. fail: (err : any) => {
  87. // 这里是接口调用失败的回调,不是连接失败的回调,请注意
  88. console.log('uni.connectSocket fail', err)
  89. },
  90. })
  91. this.socketTask?.onOpen((res : any) => {
  92. this.connecting = false
  93. this.connected = true
  94. uni.hideLoading()
  95. uni.showToast({
  96. icon: 'none',
  97. title: '连接成功',
  98. })
  99. console.log('onOpen', res)
  100. })
  101. this.socketTask?.onError((err : any) => {
  102. this.connecting = false
  103. this.connected = false
  104. uni.hideLoading()
  105. uni.showModal({
  106. content: '连接失败,可能是websocket服务不可用,请稍后再试',
  107. showCancel: false,
  108. })
  109. console.log('onError', err)
  110. })
  111. this.socketTask?.onMessage((res : OnSocketMessageCallbackResult) => {
  112. if(res.data instanceof ArrayBuffer){
  113. var int8 = new Int8Array(res.data)
  114. this.msg = int8.toString()
  115. console.log('onMessage', res)
  116. }else{
  117. this.msg = res.data as string
  118. console.log('onMessage', res)
  119. }
  120. })
  121. this.socketTask?.onClose((res : any) => {
  122. this.connected = false
  123. this.socketTask = null
  124. this.msg = ''
  125. console.log('onClose', res)
  126. })
  127. },
  128. send() {
  129. const data =
  130. 'from ' +
  131. this.platform +
  132. ' : ' +
  133. parseInt(Math.random() * 10000 + '').toString()
  134. this.socketTask?.send({
  135. data,
  136. success: (res : any) => {
  137. console.log(res)
  138. },
  139. fail: (err : any) => {
  140. console.log(err)
  141. },
  142. } as SendSocketMessageOptions)
  143. },
  144. sendArrayBuffer() {
  145. const data = new ArrayBuffer(2)
  146. let int8= new Int8Array(data)
  147. int8[0]=1
  148. int8[1]=2
  149. this.socketTask?.send({
  150. data,
  151. success: (res : any) => {
  152. console.log(res)
  153. },
  154. fail: (err : any) => {
  155. console.log(err)
  156. },
  157. } as SendSocketMessageOptions)
  158. },
  159. close() {
  160. this.socketTask?.close({
  161. code: 1000,
  162. reason: 'close reason from client',
  163. success: (res : any) => {
  164. console.log('uni.closeSocket success', res)
  165. },
  166. fail: (err : any) => {
  167. console.log('uni.closeSocket fail', err)
  168. },
  169. } as CloseSocketOptions)
  170. },
  171. //自动化测试例专用
  172. jest_connectSocket() {
  173. this.socketTask = uni.connectSocket({
  174. url: 'wss://websocket.dcloud.net.cn',
  175. success: (_) => {
  176. this.jest_result++
  177. },
  178. fail: (_) => {
  179. this.jest_result = 0
  180. },
  181. })
  182. this.socketTask?.onOpen((_) => {
  183. const data =
  184. 'from ' +
  185. this.platform +
  186. ' : ' +
  187. parseInt(Math.random() * 10000 + '').toString()
  188. this.socketTask?.send({
  189. data,
  190. success: (_) => {
  191. this.jest_result++
  192. },
  193. fail: (_) => {
  194. this.jest_result = 0
  195. },
  196. } as SendSocketMessageOptions)
  197. })
  198. this.socketTask?.onError((_) => {
  199. this.jest_result = 0;
  200. })
  201. }
  202. },
  203. }
  204. </script>
  205. <style>
  206. .uni-btn-v {
  207. padding: 5px 0;
  208. }
  209. .uni-btn-v {
  210. margin: 10px 0;
  211. }
  212. .websocket-msg {
  213. padding: 40px 0px;
  214. text-align: center;
  215. font-size: 14px;
  216. line-height: 40px;
  217. color: #666666;
  218. }
  219. .websocket-tips {
  220. padding: 40px 0px;
  221. text-align: center;
  222. font-size: 14px;
  223. line-height: 24px;
  224. color: #666666;
  225. }
  226. </style>