提交 5a3a7803 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

fix(socket): type error

上级 129bee85
...@@ -3,27 +3,39 @@ ...@@ -3,27 +3,39 @@
<page-head title="websocket通讯示例"></page-head> <page-head title="websocket通讯示例"></page-head>
<view class="uni-padding-wrap"> <view class="uni-padding-wrap">
<view class="uni-btn-v"> <view class="uni-btn-v">
<view class="websocket-msg">{{showMsg}}</view> <view class="websocket-msg">{{ showMsg }}</view>
<button class="uni-btn-v" type="primary" @click="connect">连接websocket服务</button> <button class="uni-btn-v" type="primary" @click="connect">
<button class="uni-btn-v" v-show="connected" type="primary" @click="send">发送一条消息</button> 连接websocket服务
<button class="uni-btn-v" type="primary" @click="close">断开websocket服务</button> </button>
<view class="websocket-tips">发送消息后会收到一条服务器返回的消息(与发送的消息内容一致)</view> <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> </view>
</view> </view>
</template> </template>
<script> <script lang="ts">
import CloseSocketOptions from 'uts.sdk.modules.DCloudUniWebsocket.CloseSocketOptions'; export default {
export default {
data() { data() {
return { return {
connected: false, connected: false,
connecting: false, connecting: false,
msg: "", msg: '',
roomId: '', roomId: '',
platform: "", platform: '',
pageVisible: false pageVisible: false,
} }
}, },
computed: { computed: {
...@@ -37,16 +49,24 @@ ...@@ -37,16 +49,24 @@
} else { } else {
return '尚未连接' return '尚未连接'
} }
} },
}, },
onLoad() { onLoad() {
this.platform = uni.getSystemInfoSync().platform; this.platform = uni.getSystemInfoSync().platform
this.pageVisible = true; this.pageVisible = true
}, },
onUnload() { onUnload() {
this.pageVisible = false; this.pageVisible = false
const closeOptions = new CloseSocketOptions(); uni.closeSocket({
uni.closeSocket(closeOptions); 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)
uni.hideLoading() uni.hideLoading()
}, },
methods: { methods: {
...@@ -54,22 +74,24 @@ ...@@ -54,22 +74,24 @@
if (this.connected || this.connecting) { if (this.connected || this.connecting) {
uni.showModal({ uni.showModal({
content: '正在连接或者已经连接,请勿重复连接', content: '正在连接或者已经连接,请勿重复连接',
showCancel: false showCancel: false,
}) })
return return
} }
this.connecting = true this.connecting = true
uni.showLoading({ uni.showLoading({
title: '连接中...' title: '连接中...',
}) })
uni.connectSocket({ uni.connectSocket({
url: 'ws://websocket.dcloud.net.cn', url: 'ws://websocket.dcloud.net.cn',
success(res) { success(res: any) {
// 这里是接口调用成功的回调,不是连接成功的回调,请注意 // 这里是接口调用成功的回调,不是连接成功的回调,请注意
console.log('uni.connectSocket success', res)
}, },
fail(err) { fail(err: any) {
// 这里是接口调用失败的回调,不是连接失败的回调,请注意 // 这里是接口调用失败的回调,不是连接失败的回调,请注意
} console.log('uni.connectSocket fail', err)
},
}) })
uni.onSocketOpen((res) => { uni.onSocketOpen((res) => {
if (this.pageVisible) { if (this.pageVisible) {
...@@ -79,9 +101,9 @@ ...@@ -79,9 +101,9 @@
uni.showToast({ uni.showToast({
icon: 'none', icon: 'none',
title: '连接成功' title: '连接成功',
}) })
console.log('onOpen', res); console.log('onOpen', res)
} }
}) })
uni.onSocketError((err) => { uni.onSocketError((err) => {
...@@ -92,9 +114,9 @@ ...@@ -92,9 +114,9 @@
uni.showModal({ uni.showModal({
content: '连接失败,可能是websocket服务不可用,请稍后再试', content: '连接失败,可能是websocket服务不可用,请稍后再试',
showCancel: false showCancel: false,
}) })
console.log('onError', err); console.log('onError', err)
} }
}) })
uni.onSocketMessage((res) => { uni.onSocketMessage((res) => {
...@@ -106,60 +128,72 @@ ...@@ -106,60 +128,72 @@
uni.onSocketClose((res) => { uni.onSocketClose((res) => {
if (this.pageVisible) { if (this.pageVisible) {
this.connected = false this.connected = false
this.msg = "" this.msg = ''
console.log('onClose', res) console.log('onClose', res)
} }
}) })
}, },
send() { send() {
uni.sendSocketMessage({ uni.sendSocketMessage({
data: 'from ' + platform + ' : ' + parseInt((Math.random() * 10000).toString()).toString(), data:
success(res) { 'from ' +
console.log(res); platform +
' : ' +
parseInt((Math.random() * 10000).toString()).toString(),
success(res: any) {
console.log(res)
}, },
fail(err) { fail(err: any) {
console.log(err); console.log(err)
} },
}) } as SendSocketMessageOptions)
}, },
close() { close() {
const closeSocketOptions = new CloseSocketOptions(); uni.closeSocket({
uni.closeSocket(closeSocketOptions) 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)
},
},
}
</script> </script>
<style> <style>
.uni-btn-v { .uni-btn-v {
padding: 10rpx 0; padding: 10rpx 0;
} }
.uni-btn-v { .uni-btn-v {
margin: 20rpx 0; margin: 20rpx 0;
} }
.websocket-room { .websocket-room {
height: 40px; height: 40px;
line-height: 40px; line-height: 40px;
text-align: center; text-align: center;
border-bottom: solid 1px #DDDDDD; border-bottom: solid 1px #dddddd;
margin-bottom: 20px; margin-bottom: 20px;
} }
.websocket-msg { .websocket-msg {
padding: 40px 0px; padding: 40px 0px;
text-align: center; text-align: center;
font-size: 14px; font-size: 14px;
line-height: 40px; line-height: 40px;
color: #666666; color: #666666;
} }
.websocket-tips { .websocket-tips {
padding: 40px 0px; padding: 40px 0px;
text-align: center; text-align: center;
font-size: 14px; font-size: 14px;
line-height: 24px; line-height: 24px;
color: #666666; color: #666666;
} }
</style> </style>
...@@ -3,30 +3,39 @@ ...@@ -3,30 +3,39 @@
<page-head title="websocket通讯示例"></page-head> <page-head title="websocket通讯示例"></page-head>
<view class="uni-padding-wrap"> <view class="uni-padding-wrap">
<view class="uni-btn-v"> <view class="uni-btn-v">
<view class="websocket-msg">{{showMsg}}</view> <view class="websocket-msg">{{ showMsg }}</view>
<button class="uni-btn-v" type="primary" @click="connect">连接websocket服务</button> <button class="uni-btn-v" type="primary" @click="connect">
<button class="uni-btn-v" v-show="connected" type="primary" @click="send">发送一条消息</button> 连接websocket服务
<button class="uni-btn-v" type="primary" @click="close">断开websocket服务</button> </button>
<view class="websocket-tips">发送消息后会收到一条服务器返回的消息(与发送的消息内容一致)</view> <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> </view>
</view> </view>
</template> </template>
<script> <script lang="ts">
import SocketTask from 'uts.sdk.modules.DCloudUniWebsocket.SocketTask'; export default {
import CloseSocketOptions from 'uts.sdk.modules.DCloudUniWebsocket.CloseSocketOptions';
import SendSocketMessageOptions from 'uts.sdk.modules.DCloudUniWebsocket.SendSocketMessageOptions';
export default {
data() { data() {
return { return {
connected: false, connected: false,
connecting: false, connecting: false,
socketTask: null as SocketTask | null, socketTask: null as SocketTask | null,
msg: "", msg: '',
platform: "", platform: '',
pageVisible: false pageVisible: false,
} }
}, },
computed: { computed: {
...@@ -40,19 +49,27 @@ ...@@ -40,19 +49,27 @@
} else { } else {
return '尚未连接' return '尚未连接'
} }
} },
}, },
onLoad() { onLoad() {
this.platform = uni.getSystemInfoSync().platform; this.platform = uni.getSystemInfoSync().platform
this.pageVisible = true; this.pageVisible = true
}, },
onUnload() { onUnload() {
this.pageVisible = false; this.pageVisible = false
uni.hideLoading() uni.hideLoading()
let task = this.socketTask; let task = this.socketTask
if (task != null) { if (task != null) {
const closeOptions = new CloseSocketOptions(); task.close({
task.close(closeOptions) 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)
} }
}, },
methods: { methods: {
...@@ -60,104 +77,118 @@ ...@@ -60,104 +77,118 @@
if (this.connected || this.connecting) { if (this.connected || this.connecting) {
uni.showModal({ uni.showModal({
content: '正在连接或者已经连接,请勿重复连接', content: '正在连接或者已经连接,请勿重复连接',
showCancel: false showCancel: false,
}) })
return return
} }
this.connecting = true this.connecting = true
uni.showLoading({ uni.showLoading({
title: '连接中...' title: '连接中...',
}) })
this.socketTask = uni.connectSocket({ this.socketTask = uni.connectSocket({
url: 'ws://websocket.dcloud.net.cn', url: 'ws://websocket.dcloud.net.cn',
success(res) { success(res: any) {
// 这里是接口调用成功的回调,不是连接成功的回调,请注意 // 这里是接口调用成功的回调,不是连接成功的回调,请注意
console.log('uni.connectSocket success', res)
}, },
fail(err) { fail(err: any) {
// 这里是接口调用失败的回调,不是连接失败的回调,请注意 // 这里是接口调用失败的回调,不是连接失败的回调,请注意
} console.log('uni.connectSocket fail', err)
},
}) })
this.socketTask?.onOpen((res) => { this.socketTask?.onOpen((res: any) => {
if (this.pageVisible) { if (this.pageVisible) {
this.connecting = false this.connecting = false
this.connected = true this.connected = true
uni.hideLoading() uni.hideLoading()
uni.showToast({ uni.showToast({
icon: 'none', icon: 'none',
title: '连接成功' title: '连接成功',
}) })
console.log('onOpen', res); console.log('onOpen', res)
} }
}) })
this.socketTask?.onError((err) => { this.socketTask?.onError((err: any) => {
if (this.pageVisible) { if (this.pageVisible) {
this.connecting = false this.connecting = false
this.connected = false this.connected = false
uni.hideLoading() uni.hideLoading()
uni.showModal({ uni.showModal({
content: '连接失败,可能是websocket服务不可用,请稍后再试', content: '连接失败,可能是websocket服务不可用,请稍后再试',
showCancel: false showCancel: false,
}) })
console.log('onError', err); console.log('onError', err)
} }
}) })
this.socketTask?.onMessage((res) => { this.socketTask?.onMessage((res: OnSocketMessageCallbackResult) => {
if (this.pageVisible) { if (this.pageVisible) {
this.msg = res.data as string this.msg = res.data as string
console.log('onMessage', res) console.log('onMessage', res)
} }
}) })
this.socketTask?.onClose((res) => { this.socketTask?.onClose((res: any) => {
if (this.pageVisible) { if (this.pageVisible) {
this.connected = false this.connected = false
this.socketTask = null this.socketTask = null
this.msg = "" this.msg = ''
console.log('onClose', res) console.log('onClose', res)
} }
}) })
}, },
send() { send() {
const data = 'from ' + platform + ' : ' + parseInt((Math.random() * 10000) + "").toString() const data =
let content = new SendSocketMessageOptions(data, function(res) { 'from ' +
console.log(res); platform +
}, function(err) { ' : ' +
console.log(err); parseInt(Math.random() * 10000 + '').toString()
}, null); this.socketTask?.send({
this.socketTask?.send(content) data,
success(res: any) {
console.log(res)
},
fail(err: any) {
console.log(err)
},
} as SendSocketMessageOptions)
}, },
close() { close() {
let task = this.socketTask; this.socketTask?.close({
if (task != null) { code: 1000,
let closeOptions = new CloseSocketOptions(); reason: 'close reason from client',
task.close(closeOptions) success(res: any) {
} console.log('uni.closeSocket success', res)
} },
} fail(err: any) {
} console.log('uni.closeSocket fail', err)
},
} as CloseSocketOptions)
},
},
}
</script> </script>
<style> <style>
.uni-btn-v { .uni-btn-v {
padding: 10rpx 0; padding: 10rpx 0;
} }
.uni-btn-v { .uni-btn-v {
margin: 20rpx 0; margin: 20rpx 0;
} }
.websocket-msg { .websocket-msg {
padding: 40px 0px; padding: 40px 0px;
text-align: center; text-align: center;
font-size: 14px; font-size: 14px;
line-height: 40px; line-height: 40px;
color: #666666; color: #666666;
} }
.websocket-tips { .websocket-tips {
padding: 40px 0px; padding: 40px 0px;
text-align: center; text-align: center;
font-size: 14px; font-size: 14px;
line-height: 24px; line-height: 24px;
color: #666666; color: #666666;
} }
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册