websocket-global.vue 3.4 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. msg: false,
  23. roomId: ''
  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.closeSocket()
  41. uni.hideLoading()
  42. },
  43. methods: {
  44. connect() {
  45. if (this.connected || this.connecting) {
  46. uni.showModal({
  47. content: '正在连接或者已经连接,请勿重复连接',
  48. showCancel: false
  49. })
  50. return false
  51. }
  52. this.connecting = true
  53. uni.showLoading({
  54. title: '连接中...'
  55. })
  56. uni.connectSocket({
  57. url: 'wss://echo.websocket.org',
  58. data() {
  59. return {
  60. msg: 'Hello'
  61. }
  62. },
  63. // #ifdef MP
  64. header: {
  65. 'content-type': 'application/json'
  66. },
  67. // #endif
  68. // #ifdef MP-WEIXIN
  69. method: 'GET',
  70. // #endif
  71. success(res) {
  72. // 这里是接口调用成功的回调,不是连接成功的回调,请注意
  73. },
  74. fail(err) {
  75. // 这里是接口调用失败的回调,不是连接失败的回调,请注意
  76. }
  77. })
  78. uni.onSocketOpen((res) => {
  79. this.connecting = false
  80. this.connected = true
  81. uni.hideLoading()
  82. uni.showToast({
  83. icon: 'none',
  84. title: '连接成功'
  85. })
  86. console.log('onOpen', res);
  87. })
  88. uni.onSocketError((err) => {
  89. this.connecting = false
  90. this.connected = false
  91. uni.hideLoading()
  92. uni.showModal({
  93. content: '连接失败,可能是websocket服务不可用,请稍后再试',
  94. showCancel: false
  95. })
  96. console.log('onError', err);
  97. })
  98. uni.onSocketMessage((res) => {
  99. this.msg = res.data
  100. console.log('onMessage', res)
  101. })
  102. uni.onSocketClose((res) => {
  103. this.connected = false
  104. this.startRecive = false
  105. this.msg = false
  106. console.log('onClose', res)
  107. })
  108. },
  109. send() {
  110. uni.sendSocketMessage({
  111. data: 'from ' + platform + ' : ' + parseInt(Math.random() * 10000).toString(),
  112. success(res) {
  113. console.log(res);
  114. },
  115. fail(err) {
  116. console.log(err);
  117. }
  118. })
  119. },
  120. close() {
  121. uni.closeSocket()
  122. }
  123. }
  124. }
  125. </script>
  126. <style>
  127. .uni-btn-v {
  128. padding: 10rpx 0;
  129. }
  130. .uni-btn-v button {
  131. margin: 20rpx 0;
  132. }
  133. .websocket-room {
  134. height: 40px;
  135. line-height: 40px;
  136. text-align: center;
  137. border-bottom: solid 1px #DDDDDD;
  138. margin-bottom: 20px;
  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>