websocket-socketTask.uvue 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
<template>
	<view>
		<page-head title="websocket通讯示例"></page-head>
		<view class="uni-padding-wrap">
			<view class="uni-btn-v">
				<view class="websocket-msg">{{showMsg}}</view>
				<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>
				<view class="websocket-tips">发送消息后会收到一条服务器返回的消息(与发送的消息内容一致)</view>
			</view>
		</view>
	</view>
</template>

<script>
	import SocketTask from 'uts.sdk.modules.DCloudUniWebsocket.SocketTask';
	import CloseSocketOptions from 'uts.sdk.modules.DCloudUniWebsocket.CloseSocketOptions';
	import SendSocketMessageOptions from 'uts.sdk.modules.DCloudUniWebsocket.SendSocketMessageOptions';

	export default {
		data() {
			return {
				connected: false,
				connecting: false,
				socketTask: null as SocketTask | null,
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
27 28 29
				msg: "",
				platform: "",
				pageVisible: false
30 31 32 33 34 35 36 37 38 39 40 41 42 43
			}
		},
		computed: {
			showMsg(): string {
				if (this.connected) {
					if (this.msg.length > 0) {
						return '收到消息:' + this.msg
					} else {
						return '等待接收消息'
					}
				} else {
					return '尚未连接'
				}
			}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
44 45 46 47
		},
		onLoad() {
			this.platform = uni.getSystemInfoSync().platform;
			this.pageVisible = true;
48 49
		},
		onUnload() {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
50
			this.pageVisible = false;
51
			uni.hideLoading()
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
52 53
			let task = this.socketTask;
			if (task != null) {
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
				const closeOptions = new CloseSocketOptions();
				task.close(closeOptions)
			}
		},
		methods: {
			connect() {
				if (this.connected || this.connecting) {
					uni.showModal({
						content: '正在连接或者已经连接,请勿重复连接',
						showCancel: false
					})
					return
				}
				this.connecting = true
				uni.showLoading({
					title: '连接中...'
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
70
				})
71
				this.socketTask = uni.connectSocket({
72
					url: 'ws://websocket.dcloud.net.cn',
73 74 75 76 77 78
					success(res) {
						// 这里是接口调用成功的回调,不是连接成功的回调,请注意
					},
					fail(err) {
						// 这里是接口调用失败的回调,不是连接失败的回调,请注意
					}
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
79
				})
80
				this.socketTask?.onOpen((res) => {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
81 82 83 84 85 86 87 88 89 90
					if (this.pageVisible) {
						this.connecting = false
						this.connected = true
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: '连接成功'
						})
						console.log('onOpen', res);
					}
91 92
				})
				this.socketTask?.onError((err) => {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
93 94 95 96 97 98 99 100 101 102
					if (this.pageVisible) {
						this.connecting = false
						this.connected = false
						uni.hideLoading()
						uni.showModal({
							content: '连接失败,可能是websocket服务不可用,请稍后再试',
							showCancel: false
						})
						console.log('onError', err);
					}
103 104
				})
				this.socketTask?.onMessage((res) => {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
105 106 107 108
					if (this.pageVisible) {
						this.msg = res.data as string
						console.log('onMessage', res)
					}
109 110
				})
				this.socketTask?.onClose((res) => {
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
111 112 113 114 115 116
					if (this.pageVisible) {
						this.connected = false
						this.socketTask = null
						this.msg = ""
						console.log('onClose', res)
					}
117 118 119 120 121 122 123 124 125 126 127 128 129
				})
			},
			send() {
				const data = 'from ' + platform + ' : ' + parseInt((Math.random() * 10000) + "").toString()
				let content = new SendSocketMessageOptions(data, function(res) {
					console.log(res);
				}, function(err) {
					console.log(err);
				}, null);
				this.socketTask?.send(content)
			},
			close() {
				let task = this.socketTask;
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
130
				if (task != null) {
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
					let closeOptions = new CloseSocketOptions();
					task.close(closeOptions)
				}
			}
		}
	}
</script>

<style>
	.uni-btn-v {
		padding: 10rpx 0;
	}

	.uni-btn-v {
		margin: 20rpx 0;
	}

	.websocket-msg {
		padding: 40px 0px;
		text-align: center;
		font-size: 14px;
		line-height: 40px;
		color: #666666;
	}

	.websocket-tips {
		padding: 40px 0px;
		text-align: center;
		font-size: 14px;
		line-height: 24px;
		color: #666666;
	}
</style>