Skip to content

Commit

Permalink
update: 新增websocket示例
Browse files Browse the repository at this point in the history
  • Loading branch information
Wangyaqi committed Oct 23, 2019
1 parent e3c9afd commit 5ce16b0
Show file tree
Hide file tree
Showing 4 changed files with 367 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pages.json
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,21 @@
"style": {
"navigationBarTitleText": "震动"
}
}
},
// #ifndef MP-ALIPAY
{
"path" : "websocket-socketTask/websocket-socketTask",
"style" : {
"navigationBarTitleText": "websocket-socketTask"
}
},
// #endif
{
"path" : "websocket-global/websocket-global",
"style" : {
"navigationBarTitleText": "websocket-global"
}
}
]
},
{
Expand Down
164 changes: 164 additions & 0 deletions pages/API/websocket-global/websocket-global.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<template>
<view>
<page-head title="websocket通讯示例"></page-head>
<view class="uni-padding-wrap">
<view class="uni-btn-v">
<view class="websocket-msg">{{showMsg}}</view>
<input class="websocket-room" type="text" value="" v-model.trim="roomId" placeholder="请输入一个房间号" />
<button type="primary" @click="connect">连接websocket服务</button>
<button v-show="connected" type="primary" @click="send">发送一条消息</button>
<button type="primary" @click="close">断开websocket服务</button>
<view class="websocket-msg">同时运行两个hello uniapp填入相同的房间号进行测试</view>
</view>
</view>
</view>
</template>

<script>
let platform = uni.getSystemInfoSync().platform
export default {
data() {
return {
connected: false,
connecting: false,
msg: false,
roomId: ''
}
},
computed: {
showMsg() {
if (this.connected) {
if (this.msg) {
return '收到消息:' + this.msg
} else {
return '等待接收消息'
}
} else {
return '尚未连接'
}
}
},
onUnload() {
uni.closeSocket()
},
methods: {
connect() {
if (this.roomId === '') {
uni.showModal({
content: '请输入一个房间号',
showCancel: false
})
return false
}
if (this.connected || this.connecting) {
uni.showModal({
content: '正在连接或者已经连接,请勿重复连接',
showCancel: false
})
return false
}
this.connecting = true
uni.showLoading({
title: '连接中...'
})
uni.connectSocket({
url: 'wss://connect.websocket.in/hello_uni_app?room_id=' + this.roomId,
data() {
return {
msg: 'Hello'
}
},
// #ifdef MP
header: {
'content-type': 'application/json'
},
// #endif
// #ifdef MP-WEIXIN
method: 'GET',
// #endif
success(res) {
// 这里是接口调用成功的回调,不是连接成功的回调,请注意
},
fail(err) {
// 这里是接口调用失败的回调,不是连接失败的回调,请注意
}
})
uni.onSocketOpen((res) => {
this.connecting = false
this.connected = true
uni.hideLoading()
uni.showToast({
icon: 'none',
title: '连接成功'
})
console.log('onOpen', res);
})
uni.onSocketError((err) => {
this.connecting = false
this.connected = false
uni.hideLoading()
uni.showModal({
content: '连接失败,可能是websocket服务不可用,请稍后再试',
showCancel: false
})
console.log('onError', err);
})
uni.onSocketMessage((res) => {
this.msg = res.data
console.log('onMessage', res)
})
uni.onSocketClose((res) => {
this.connected = false
this.startRecive = false
this.msg = false
console.log('onClose', res)
})
},
send() {
uni.sendSocketMessage({
data: 'from ' + platform + ' : ' + parseInt(Math.random() * 10000).toString(),
success(res) {
console.log(res);
},
fail(err) {
console.log(err);
}
})
},
close() {
uni.closeSocket()
}
}
}
</script>

<style>
.uni-padding-wrap {
width: 690rpx;
padding: 0 30rpx;
}
.uni-btn-v {
padding: 10rpx 0;
}
.uni-btn-v button {
margin: 20rpx 0;
}
.websocket-room {
height: 40px;
line-height: 40px;
text-align: center;
border-bottom: solid 1px #DDDDDD;
margin-bottom: 20px;
}
.websocket-msg {
padding: 40px 0px;
text-align: center;
font-size: 14px;
line-height: 40px;
color: #666666;
}
</style>
171 changes: 171 additions & 0 deletions pages/API/websocket-socketTask/websocket-socketTask.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<template>
<view>
<page-head title="websocket通讯示例"></page-head>
<view class="uni-padding-wrap">
<view class="uni-btn-v">
<view class="websocket-msg">{{showMsg}}</view>
<input class="websocket-room" type="text" value="" v-model.trim="roomId" placeholder="请输入一个房间号" />
<button type="primary" @click="connect">连接websocket服务</button>
<button v-show="connected" type="primary" @click="send">发送一条消息</button>
<button type="primary" @click="close">断开websocket服务</button>
<view class="websocket-msg">同时运行两个hello uniapp填入相同的房间号进行测试</view>
</view>
</view>
</view>
</template>

<script>
let platform = uni.getSystemInfoSync().platform
export default {
data() {
return {
connected: false,
connecting: false,
socketTask: false,
msg: false,
roomId: ''
}
},
computed: {
showMsg() {
if (this.connected) {
if (this.msg) {
return '收到消息:' + this.msg
} else {
return '等待接收消息'
}
} else {
return '尚未连接'
}
}
},
onUnload() {
if (this.socketTask && this.socketTask.close) {
this.socketTask.close()
}
},
methods: {
connect() {
if (this.roomId === '') {
uni.showModal({
content: '请输入一个房间号',
showCancel: false
})
return false
}
if (this.connected || this.connecting) {
uni.showModal({
content: '正在连接或者已经连接,请勿重复连接',
showCancel: false
})
return false
}
this.connecting = true
uni.showLoading({
title: '连接中...'
})
this.socketTask = uni.connectSocket({
url: 'wss://connect.websocket.in/hello_uni_app?room_id=' + this.roomId,
data() {
return {
msg: 'Hello'
}
},
// #ifdef MP
header: {
'content-type': 'application/json'
},
// #endif
// #ifdef MP-WEIXIN
method: 'GET',
// #endif
success(res) {
// 这里是接口调用成功的回调,不是连接成功的回调,请注意
},
fail(err) {
// 这里是接口调用失败的回调,不是连接失败的回调,请注意
}
})
this.socketTask.onOpen((res) => {
this.connecting = false
this.connected = true
uni.hideLoading()
uni.showToast({
icon: 'none',
title: '连接成功'
})
console.log('onOpen', res);
})
this.socketTask.onError((err) => {
this.connecting = false
this.connected = false
uni.hideLoading()
uni.showModal({
content: '连接失败,可能是websocket服务不可用,请稍后再试',
showCancel: false
})
console.log('onError', err);
})
this.socketTask.onMessage((res) => {
this.msg = res.data
console.log('onMessage', res)
})
this.socketTask.onClose((res) => {
this.connected = false
this.startRecive = false
this.socketTask = false
this.msg = false
console.log('onClose', res)
})
console.log('task', this.socketTask)
},
send() {
this.socketTask.send({
data: 'from ' + platform + ' : ' + parseInt(Math.random() * 10000).toString(),
success(res) {
console.log(res);
},
fail(err) {
console.log(err);
}
})
},
close() {
if (this.socketTask && this.socketTask.close) {
this.socketTask.close()
}
}
}
}
</script>

<style>
.uni-padding-wrap {
width: 690rpx;
padding: 0 30rpx;
}
.uni-btn-v {
padding: 10rpx 0;
}
.uni-btn-v button {
margin: 20rpx 0;
}
.websocket-room {
height: 40px;
line-height: 40px;
text-align: center;
border-bottom: solid 1px #DDDDDD;
margin-bottom: 20px;
}
.websocket-msg {
padding: 40px 0px;
text-align: center;
font-size: 14px;
line-height: 40px;
color: #666666;
}
</style>
17 changes: 17 additions & 0 deletions pages/tabBar/API/API.nvue
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,23 @@
}
]
},
{
id: 'websocket',
name: 'websocket',
open: false,
pages: [
// #ifndef MP-ALIPAY
{
name: 'socketTask',
url: 'websocket-socketTask'
},
// #endif
{
name: '全局websocket',
url: 'websocket-global'
}
]
},
{
id: 'media',
name: '媒体',
Expand Down

0 comments on commit 5ce16b0

Please sign in to comment.