Skip to content

Commit 5ce16b0

Browse files
committed
update: 新增websocket示例
1 parent e3c9afd commit 5ce16b0

4 files changed

Lines changed: 367 additions & 1 deletion

File tree

‎pages.json‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,21 @@
653653
"style": {
654654
"navigationBarTitleText": "震动"
655655
}
656-
}
656+
},
657+
// #ifndef MP-ALIPAY
658+
{
659+
"path" : "websocket-socketTask/websocket-socketTask",
660+
"style" : {
661+
"navigationBarTitleText": "websocket-socketTask"
662+
}
663+
},
664+
// #endif
665+
{
666+
"path" : "websocket-global/websocket-global",
667+
"style" : {
668+
"navigationBarTitleText": "websocket-global"
669+
}
670+
}
657671
]
658672
},
659673
{
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
msg: false,
25+
roomId: ''
26+
}
27+
},
28+
computed: {
29+
showMsg() {
30+
if (this.connected) {
31+
if (this.msg) {
32+
return '收到消息:' + this.msg
33+
} else {
34+
return '等待接收消息'
35+
}
36+
} else {
37+
return '尚未连接'
38+
}
39+
}
40+
},
41+
onUnload() {
42+
uni.closeSocket()
43+
},
44+
methods: {
45+
connect() {
46+
if (this.roomId === '') {
47+
uni.showModal({
48+
content: '请输入一个房间号',
49+
showCancel: false
50+
})
51+
return false
52+
}
53+
if (this.connected || this.connecting) {
54+
uni.showModal({
55+
content: '正在连接或者已经连接,请勿重复连接',
56+
showCancel: false
57+
})
58+
return false
59+
}
60+
this.connecting = true
61+
uni.showLoading({
62+
title: '连接中...'
63+
})
64+
uni.connectSocket({
65+
url: 'wss://connect.websocket.in/hello_uni_app?room_id=' + this.roomId,
66+
data() {
67+
return {
68+
msg: 'Hello'
69+
}
70+
},
71+
// #ifdef MP
72+
header: {
73+
'content-type': 'application/json'
74+
},
75+
// #endif
76+
// #ifdef MP-WEIXIN
77+
method: 'GET',
78+
// #endif
79+
success(res) {
80+
// 这里是接口调用成功的回调,不是连接成功的回调,请注意
81+
},
82+
fail(err) {
83+
// 这里是接口调用失败的回调,不是连接失败的回调,请注意
84+
}
85+
})
86+
uni.onSocketOpen((res) => {
87+
this.connecting = false
88+
this.connected = true
89+
uni.hideLoading()
90+
uni.showToast({
91+
icon: 'none',
92+
title: '连接成功'
93+
})
94+
console.log('onOpen', res);
95+
})
96+
uni.onSocketError((err) => {
97+
this.connecting = false
98+
this.connected = false
99+
uni.hideLoading()
100+
uni.showModal({
101+
content: '连接失败,可能是websocket服务不可用,请稍后再试',
102+
showCancel: false
103+
})
104+
console.log('onError', err);
105+
})
106+
uni.onSocketMessage((res) => {
107+
this.msg = res.data
108+
console.log('onMessage', res)
109+
})
110+
uni.onSocketClose((res) => {
111+
this.connected = false
112+
this.startRecive = false
113+
this.msg = false
114+
console.log('onClose', res)
115+
})
116+
},
117+
send() {
118+
uni.sendSocketMessage({
119+
data: 'from ' + platform + ' : ' + parseInt(Math.random() * 10000).toString(),
120+
success(res) {
121+
console.log(res);
122+
},
123+
fail(err) {
124+
console.log(err);
125+
}
126+
})
127+
},
128+
close() {
129+
uni.closeSocket()
130+
}
131+
}
132+
}
133+
</script>
134+
135+
<style>
136+
.uni-padding-wrap {
137+
width: 690rpx;
138+
padding: 0 30rpx;
139+
}
140+
141+
.uni-btn-v {
142+
padding: 10rpx 0;
143+
}
144+
145+
.uni-btn-v button {
146+
margin: 20rpx 0;
147+
}
148+
149+
.websocket-room {
150+
height: 40px;
151+
line-height: 40px;
152+
text-align: center;
153+
border-bottom: solid 1px #DDDDDD;
154+
margin-bottom: 20px;
155+
}
156+
157+
.websocket-msg {
158+
padding: 40px 0px;
159+
text-align: center;
160+
font-size: 14px;
161+
line-height: 40px;
162+
color: #666666;
163+
}
164+
</style>
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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>

‎pages/tabBar/API/API.nvue‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,23 @@
229229
}
230230
]
231231
},
232+
{
233+
id: 'websocket',
234+
name: 'websocket',
235+
open: false,
236+
pages: [
237+
// #ifndef MP-ALIPAY
238+
{
239+
name: 'socketTask',
240+
url: 'websocket-socketTask'
241+
},
242+
// #endif
243+
{
244+
name: '全局websocket',
245+
url: 'websocket-global'
246+
}
247+
]
248+
},
232249
{
233250
id: 'media',
234251
name: '媒体',

0 commit comments

Comments
 (0)