Skip to content

Commit 7b51c44

Browse files
committedApr 23, 2020
add samples: mdns
1 parent 8275e35 commit 7b51c44

File tree

6 files changed

+158
-1
lines changed

6 files changed

+158
-1
lines changed
 

‎miniprogram/app.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@
147147
"page/weui/example/wxml-to-canvas/wxml-to-canvas",
148148
"page/weui/example/toptips/toptips",
149149
"page/component/pages/aria-component/aria-component",
150-
"page/API/pages/audio/audio"
150+
"page/API/pages/audio/audio",
151+
"page/API/pages/mdns/mdns"
151152
],
152153
"window": {
153154
"navigationBarTextStyle": "black",

‎miniprogram/page/API/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ Page({
163163
}, {
164164
zh: '下载文件',
165165
url: 'download-file/download-file'
166+
}, {
167+
zh: 'mDNS',
168+
url: 'mdns/mdns'
166169
}
167170
]
168171
}, {
+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// miniprogram/page/API/pages/mdns/mdns.js
2+
let serviceList = [];
3+
let resolveFailList = [];
4+
Page({
5+
daga: {
6+
serviceList: [],
7+
resolveFailList: [],
8+
},
9+
onShow() {
10+
this.onLocalService();
11+
},
12+
13+
startDiscovery() {
14+
wx.startLocalServiceDiscovery({
15+
serviceType: '_http._tcp.',
16+
success: function(res) {
17+
console.log(res);
18+
wx.showToast({
19+
title: '开启成功',
20+
icon: 'none',
21+
duration: 2000
22+
})
23+
},
24+
fail: (err) => {
25+
wx.showToast({
26+
title: '开启失败',
27+
icon: 'none',
28+
duration: 2000
29+
})
30+
console.log(err)
31+
},
32+
complete: () => {
33+
console.log('startDiscovery: complete')
34+
}
35+
})
36+
},
37+
38+
stopDiscovery() {
39+
const that = this;
40+
wx.stopLocalServiceDiscovery({
41+
success: (res) => {
42+
wx.showToast({
43+
title: '关闭成功',
44+
icon: 'none',
45+
duration: 2000
46+
})
47+
serviceList = [];
48+
resolveFailList = [];
49+
that.setData({
50+
serviceList: [],
51+
resolveFailList: []
52+
})
53+
},
54+
fail: () => {
55+
console.log('stopDiscovery: fail')
56+
wx.showToast({
57+
title: '关闭失败',
58+
icon: 'none',
59+
duration: 2000
60+
})
61+
},
62+
complete: () => {
63+
console.log('stopDIscovery: complete')
64+
}
65+
})
66+
},
67+
68+
69+
// 监听列表
70+
onLocalService() {
71+
let that = this
72+
73+
// 监听服务发现事件
74+
wx.onLocalServiceFound(function(obj) {
75+
console.log(obj)
76+
serviceList.push(obj);
77+
78+
that.setData({
79+
serviceList: serviceList,
80+
})
81+
})
82+
83+
// 监听服务解析失败事件
84+
wx.onLocalServiceResolveFail(function(obj) {
85+
console.log(obj)
86+
resolveFailList.push(obj)
87+
that.setData({
88+
resolveFailList: resolveFailList
89+
})
90+
})
91+
92+
// 监听服务离开
93+
wx.onLocalServiceLost(function(obj) {
94+
console.log(obj)
95+
})
96+
97+
// 监听搜索停止
98+
wx.onLocalServiceDiscoveryStop(function(obj) {
99+
console.log('监听到搜索停止事件')
100+
})
101+
},
102+
// 取消监听
103+
offLocalService() {
104+
105+
console.log('是否执行此部分数据')
106+
// 取消监听服务发现事件
107+
wx.offLocalServiceFound(function () {
108+
console.log('取消监听服务发现事件 成功')
109+
})
110+
111+
// 取消监听服务解析失败事件
112+
wx.offLocalServiceResolveFail(function () {
113+
console.log('取消监听 mDNS 服务解析失败的事件 成功')
114+
})
115+
116+
// 取消监听服务离开
117+
wx.offLocalServiceLost(function () {
118+
console.log('取消监听服务离开事件 成功')
119+
})
120+
121+
// 取消监听搜索停止
122+
wx.offLocalServiceDiscoveryStop(function () {
123+
console.log('取消监听 mDNS 服务停止搜索的事件 成功')
124+
})
125+
},
126+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"usingComponents": {}
3+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<import src="../../../common/head.wxml" />
2+
<import src="../../../common/foot.wxml" />
3+
4+
<view class="container">
5+
<template is="head" data="{{title: 'mDNS'}}"/>
6+
<view class="page-body">
7+
<view class="page-section">
8+
<button type="primary" bind:tap="startDiscovery">
9+
开始搜索 mDNS
10+
</button>
11+
<button type="primary" bind:tap="stopDiscovery">
12+
停止搜索 mDNS
13+
</button>
14+
<view>
15+
<view wx:for="{{serviceList}}" wx:key="{{index}}" class='row'>
16+
<text>设备服务名:{{item.serviceName}}</text>
17+
<text>ip地址:{{item.ip}}:{{item.port}}</text>
18+
<text>当前服务类型:{{item.serviceType}}</text>
19+
</view>
20+
</view>
21+
</view>
22+
</view>
23+
</view>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* miniprogram/page/API/pages/mdns/mdns.wxss */

0 commit comments

Comments
 (0)