Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
5a3a7803
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
5995
Star
90
Fork
162
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
18
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
18
Issue
18
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
5a3a7803
编写于
7月 08, 2023
作者:
DCloud-WZF
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(socket): type error
上级
129bee85
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
372 addition
and
307 deletion
+372
-307
pages/API/websocket-global/websocket-global.uvue
pages/API/websocket-global/websocket-global.uvue
+187
-153
pages/API/websocket-socketTask/websocket-socketTask.uvue
pages/API/websocket-socketTask/websocket-socketTask.uvue
+185
-154
未找到文件。
pages/API/websocket-global/websocket-global.uvue
浏览文件 @
5a3a7803
<template>
<template>
<view>
<view>
<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
</view>
class="uni-btn-v"
</view>
v-show="connected"
</view>
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>
</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: {
showMsg(): string {
showMsg(): string {
if (this.connected) {
if (this.connected) {
if (this.msg.length > 0) {
if (this.msg.length > 0) {
return '收到消息:' + this.msg
return '收到消息:' + this.msg
} else {
} else {
return '等待接收消息'
return '等待接收消息'
}
}
} 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.closeSocket({
const closeOptions = new CloseSocketOptions();
code: 1000,
uni.closeSocket(closeOptions);
reason: 'close reason from client',
uni.hideLoading()
success(res: any) {
},
console.log('uni.closeSocket success', res)
methods: {
},
connect() {
fail(err: any) {
if (this.connected || this.connecting) {
console.log('uni.closeSocket fail', err)
uni.showModal({
},
content: '正在连接或者已经连接,请勿重复连接',
} as CloseSocketOptions)
showCancel: false
uni.hideLoading()
})
},
return
methods: {
}
connect() {
this.connecting = true
if (this.connected || this.connecting) {
uni.showLoading({
uni.showModal({
title: '连接中...'
content: '正在连接或者已经连接,请勿重复连接',
})
showCancel: false,
uni.connectSocket({
})
url: 'ws://websocket.dcloud.net.cn',
return
success(res) {
}
// 这里是接口调用成功的回调,不是连接成功的回调,请注意
this.connecting = true
},
uni.showLoading({
fail(err) {
title: '连接中...',
// 这里是接口调用失败的回调,不是连接失败的回调,请注意
})
}
uni.connectSocket({
})
url: 'ws://websocket.dcloud.net.cn',
uni.onSocketOpen((res) => {
success(res: any) {
if (this.pageVisible) {
// 这里是接口调用成功的回调,不是连接成功的回调,请注意
this.connecting = false
console.log('uni.connectSocket success', res)
this.connected = true
},
uni.hideLoading()
fail(err: any) {
// 这里是接口调用失败的回调,不是连接失败的回调,请注意
console.log('uni.connectSocket fail', err)
},
})
uni.onSocketOpen((res) => {
if (this.pageVisible) {
this.connecting = false
this.connected = true
uni.hideLoading()
uni.showToast({
uni.showToast({
icon: 'none',
icon: 'none',
title: '连接成功'
title: '连接成功',
})
})
console.log('onOpen', res);
console.log('onOpen', res)
}
}
})
})
uni.onSocketError((err) => {
uni.onSocketError((err) => {
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)
}
}
})
})
uni.onSocketMessage((res) => {
uni.onSocketMessage((res) => {
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)
}
}
})
})
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 +
},
' : ' +
fail(err) {
parseInt((Math.random() * 10000).toString()).toString(),
console.log(err);
success(res: any) {
}
console.log(res)
})
},
},
fail(err: any) {
close() {
console.log(err)
const closeSocketOptions = new CloseSocketOptions();
},
uni.closeSocket(closeSocketOptions)
} 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)
},
},
}
</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>
\ No newline at end of file
pages/API/websocket-socketTask/websocket-socketTask.uvue
浏览文件 @
5a3a7803
<template>
<template>
<view>
<view>
<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
</view>
class="uni-btn-v"
</view>
v-show="connected"
</view>
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>
</template>
<script>
<script lang="ts">
import SocketTask from 'uts.sdk.modules.DCloudUniWebsocket.SocketTask';
export default {
import CloseSocketOptions from 'uts.sdk.modules.DCloudUniWebsocket.CloseSocketOptions';
data() {
import SendSocketMessageOptions from 'uts.sdk.modules.DCloudUniWebsocket.SendSocketMessageOptions';
return {
connected: false,
export default {
connecting: false,
data() {
socketTask: null as SocketTask | null,
return {
msg: '',
connected: false,
platform: '',
connecting: false,
pageVisible: false,
socketTask: null as SocketTask | null,
}
msg: "",
},
platform: "",
computed: {
pageVisible: false
showMsg(): string {
}
if (this.connected) {
},
if (this.msg.length > 0) {
computed: {
return '收到消息:' + this.msg
showMsg(): string {
} else {
if (this.connected) {
return '等待接收消息'
if (this.msg.length > 0) {
}
return '收到消息:' + this.msg
} else {
} else {
return '尚未连接'
return '等待接收消息'
}
}
},
} else {
},
return '尚未连接'
onLoad() {
}
this.platform = uni.getSystemInfoSync().platform
}
this.pageVisible = true
},
},
onLoad() {
onUnload() {
this.platform = uni.getSystemInfoSync().platform;
this.pageVisible = false
this.pageVisible = true;
uni.hideLoading()
},
let task = this.socketTask
onUnload() {
if (task != null) {
this.pageVisible = false;
task.close({
uni.hideLoading()
code: 1000,
let task = this.socketTask;
reason: 'close reason from client',
if (task != null) {
success(res: any) {
const closeOptions = new CloseSocketOptions();
console.log('uni.closeSocket success', res)
task.close(closeOptions)
},
}
fail(err: any) {
},
console.log('uni.closeSocket fail', err)
methods: {
},
connect() {
} as CloseSocketOptions)
if (this.connected || this.connecting) {
}
uni.showModal({
},
content: '正在连接或者已经连接,请勿重复连接',
methods: {
showCancel: false
connect() {
})
if (this.connected || this.connecting) {
return
uni.showModal({
}
content: '正在连接或者已经连接,请勿重复连接',
this.connecting = true
showCancel: false,
uni.showLoading({
})
title: '连接中...'
return
})
}
this.socketTask = uni.connectSocket({
this.connecting = true
url: 'ws://websocket.dcloud.net.cn',
uni.showLoading({
success(res) {
title: '连接中...',
// 这里是接口调用成功的回调,不是连接成功的回调,请注意
})
},
this.socketTask = uni.connectSocket({
fail(err) {
url: 'ws://websocket.dcloud.net.cn',
// 这里是接口调用失败的回调,不是连接失败的回调,请注意
success(res: any) {
}
// 这里是接口调用成功的回调,不是连接成功的回调,请注意
})
console.log('uni.connectSocket success', res)
this.socketTask?.onOpen((res) => {
},
if (this.pageVisible) {
fail(err: any) {
this.connecting = false
// 这里是接口调用失败的回调,不是连接失败的回调,请注意
this.connected = true
console.log('uni.connectSocket fail', err)
uni.hideLoading()
},
uni.showToast({
})
icon: 'none',
this.socketTask?.onOpen((res: any) => {
title: '连接成功'
if (this.pageVisible) {
})
this.connecting = false
console.log('onOpen', res);
this.connected = true
}
uni.hideLoading()
})
uni.showToast({
this.socketTask?.onError((err) => {
icon: 'none',
if (this.pageVisible) {
title: '连接成功',
this.connecting = false
})
this.connected = false
console.log('onOpen', res)
uni.hideLoading()
}
uni.showModal({
})
content: '连接失败,可能是websocket服务不可用,请稍后再试',
this.socketTask?.onError((err: any) => {
showCancel: false
if (this.pageVisible) {
})
this.connecting = false
console.log('onError', err);
this.connected = false
}
uni.hideLoading()
})
uni.showModal({
this.socketTask?.onMessage((res) => {
content: '连接失败,可能是websocket服务不可用,请稍后再试',
if (this.pageVisible) {
showCancel: false,
this.msg = res.data as string
})
console.log('onMessage', res)
console.log('onError', err)
}
}
})
})
this.socketTask?.onClose((res) => {
this.socketTask?.onMessage((res: OnSocketMessageCallbackResult) => {
if (this.pageVisible) {
if (this.pageVisible) {
this.connected = false
this.msg = res.data as string
this.socketTask = null
console.log('onMessage', res)
this.msg = ""
}
console.log('onClose', res)
})
}
this.socketTask?.onClose((res: any) => {
})
if (this.pageVisible) {
},
this.connected = false
send() {
this.socketTask = null
const data = 'from ' + platform + ' : ' + parseInt((Math.random() * 10000) + "").toString()
this.msg = ''
let content = new SendSocketMessageOptions(data, function(res) {
console.log('onClose', res)
console.log(res);
}
}, function(err) {
})
console.log(err);
},
}, null);
send() {
this.socketTask?.send(content)
const data =
},
'from ' +
close() {
platform +
let task = this.socketTask;
' : ' +
if (task != null) {
parseInt(Math.random() * 10000 + '').toString()
let closeOptions = new CloseSocketOptions();
this.socketTask?.send({
task.close(closeOptions)
data,
}
success(res: any) {
}
console.log(res)
}
},
}
fail(err: any) {
console.log(err)
},
} as SendSocketMessageOptions)
},
close() {
this.socketTask?.close({
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-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>
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录