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