websocket.uvue 5.0 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1
<template>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
  <page-head title="websocket通讯示例"></page-head>
  <view class="uni-padding-wrap">
    <view class="uni-btn-v">
      <text class="websocket-msg">{{ showMsg }}</text>
      <button class="uni-btn-v" type="primary" @click="connect">
        连接websocket服务
      </button>
      <button class="uni-btn-v" v-show="connected" type="primary" @click="send">
        发送一条消息
      </button>
      <button class="uni-btn-v" type="primary" @click="close">
        断开websocket服务
      </button>
      <text class="websocket-tips">发送消息后会收到一条服务器返回的消息(与发送的消息内容一致)</text>
16 17 18
      <button class="uni-btn-v" type="primary" @click="goSocketTask">
        跳转 socketTask 示例
      </button>
DCloud-WZF's avatar
DCloud-WZF 已提交
19 20
    </view>
  </view>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
21 22
</template>

H
hdx 已提交
23
<script>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
24 25 26 27 28 29 30 31
  export default {
    data() {
      return {
        connected: false,
        connecting: false,
        msg: '',
        roomId: '',
        platform: '',
DCloud-WZF's avatar
DCloud-WZF 已提交
32 33
      }
    },
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
34 35 36 37 38 39 40 41 42 43 44
    computed: {
      showMsg() : string {
        if (this.connected) {
          if (this.msg.length > 0) {
            return '收到消息:' + this.msg
          } else {
            return '等待接收消息'
          }
        } else {
          return '尚未连接'
        }
DCloud-WZF's avatar
DCloud-WZF 已提交
45
      },
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
46 47
    },
    onLoad() {
48 49 50 51 52 53
      // #ifdef MP-WEIXIN
      this.platform = uni.getDeviceInfo().platform
      // #endif
      // #ifndef MP-WEIXIN
      this.platform = uni.getSystemInfoSync().platform
      // #endif
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
54 55 56 57 58 59 60
    },
    onUnload() {
      uni.closeSocket({
        code: 1000,
        reason: 'close reason from client',
        success: (res : any) => {
          console.log('uni.closeSocket success', res)
DCloud-WZF's avatar
DCloud-WZF 已提交
61
        },
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
62 63
        fail: (err : any) => {
          console.log('uni.closeSocket fail', err)
DCloud-WZF's avatar
DCloud-WZF 已提交
64
        },
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
      } as CloseSocketOptions)
      uni.hideLoading()
    },
    methods: {
      connect() {
        if (this.connected || this.connecting) {
          uni.showModal({
            content: '正在连接或者已经连接,请勿重复连接',
            showCancel: false,
          })
          return
        }
        this.connecting = true
        uni.showLoading({
          title: '连接中...',
        })
H
hdx 已提交
81 82 83 84 85 86 87 88 89 90 91 92
        uni.connectSocket({
          url: 'ws://websocket.dcloud.net.cn',
          header: null,
          protocols: null,
          success: (res : any) => {
            // 这里是接口调用成功的回调,不是连接成功的回调,请注意
            console.log('uni.connectSocket success', res)
          },
          fail: (err : any) => {
            // 这里是接口调用失败的回调,不是连接失败的回调,请注意
            console.log('uni.connectSocket fail', err)
          },
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
93 94
        })
        uni.onSocketOpen((res) => {
DCloud-WZF's avatar
DCloud-WZF 已提交
95 96 97
          this.connecting = false
          this.connected = true
          uni.hideLoading()
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
98

DCloud-WZF's avatar
DCloud-WZF 已提交
99 100 101 102 103
          uni.showToast({
            icon: 'none',
            title: '连接成功',
          })
          console.log('onOpen', res)
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
104 105
        })
        uni.onSocketError((err) => {
DCloud-WZF's avatar
DCloud-WZF 已提交
106 107 108
          this.connecting = false
          this.connected = false
          uni.hideLoading()
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
109

DCloud-WZF's avatar
DCloud-WZF 已提交
110 111 112 113 114
          uni.showModal({
            content: '连接失败,可能是websocket服务不可用,请稍后再试',
            showCancel: false,
          })
          console.log('onError', err)
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
115 116
        })
        uni.onSocketMessage((res) => {
DCloud-WZF's avatar
DCloud-WZF 已提交
117 118
          this.msg = res.data as string
          console.log('onMessage', res)
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
119 120
        })
        uni.onSocketClose((res) => {
DCloud-WZF's avatar
DCloud-WZF 已提交
121 122 123
          this.connected = false
          this.msg = ''
          console.log('onClose', res)
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        })
      },
      send() {
        uni.sendSocketMessage({
          data:
            'from ' +
            this.platform +
            ' : ' +
            parseInt((Math.random() * 10000).toString()).toString(),
          success: (res : any) => {
            console.log(res)
          },
          fail: (err : any) => {
            console.log(err)
          },
        } as SendSocketMessageOptions)
      },
      close() {
        uni.closeSocket({
          code: 1000,
          reason: 'close reason from client',
          success: (res : any) => {
            console.log('uni.closeSocket success', res)
          },
          fail: (err : any) => {
            console.log('uni.closeSocket fail', err)
          },
        } as CloseSocketOptions)
      },
153 154 155 156 157
      goSocketTask() {
        uni.navigateTo({
          url: '/pages/API/websocket/socketTask',
        })
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
158
    },
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
159
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
160 161 162
</script>

<style>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
163
  .uni-btn-v {
H
hdx 已提交
164
    padding: 5px 0;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
165
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
166

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
167
  .uni-btn-v {
H
hdx 已提交
168
    margin: 10px 0;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
169
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
170

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
171 172 173 174 175 176 177
  .websocket-msg {
    padding: 40px 0px;
    text-align: center;
    font-size: 14px;
    line-height: 40px;
    color: #666666;
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
178

taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
179 180 181 182 183 184 185
  .websocket-tips {
    padding: 40px 0px;
    text-align: center;
    font-size: 14px;
    line-height: 24px;
    color: #666666;
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
186
</style>