mdns.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // miniprogram/page/API/pages/mdns/mdns.js
  2. let serviceList = []
  3. let resolveFailList = []
  4. Page({
  5. onShareAppMessage() {
  6. return {
  7. title: 'mdns',
  8. path: 'packageAPI/pages/network/mdns/mdns'
  9. }
  10. },
  11. daga: {
  12. serviceList: [],
  13. resolveFailList: [],
  14. },
  15. onShow() {
  16. this.onLocalService()
  17. },
  18. startDiscovery() {
  19. wx.startLocalServiceDiscovery({
  20. serviceType: '_http._tcp.',
  21. success(res) {
  22. console.log(res)
  23. wx.showToast({
  24. title: '开启成功',
  25. icon: 'none',
  26. duration: 2000
  27. })
  28. },
  29. fail: (err) => {
  30. wx.showToast({
  31. title: '开启失败',
  32. icon: 'none',
  33. duration: 2000
  34. })
  35. console.log(err)
  36. },
  37. complete: () => {
  38. console.log('startDiscovery: complete')
  39. }
  40. })
  41. },
  42. stopDiscovery() {
  43. const that = this
  44. wx.stopLocalServiceDiscovery({
  45. success: () => {
  46. wx.showToast({
  47. title: '关闭成功',
  48. icon: 'none',
  49. duration: 2000
  50. })
  51. serviceList = []
  52. resolveFailList = []
  53. that.setData({
  54. serviceList: [],
  55. resolveFailList: []
  56. })
  57. },
  58. fail: () => {
  59. console.log('stopDiscovery: fail')
  60. wx.showToast({
  61. title: '关闭失败',
  62. icon: 'none',
  63. duration: 2000
  64. })
  65. },
  66. complete: () => {
  67. console.log('stopDIscovery: complete')
  68. }
  69. })
  70. },
  71. // 监听列表
  72. onLocalService() {
  73. const that = this
  74. // 监听服务发现事件
  75. wx.onLocalServiceFound(function (obj) {
  76. console.log(obj)
  77. serviceList.push(obj)
  78. that.setData({
  79. serviceList,
  80. })
  81. })
  82. // 监听服务解析失败事件
  83. wx.onLocalServiceResolveFail(function (obj) {
  84. console.log(obj)
  85. resolveFailList.push(obj)
  86. that.setData({
  87. resolveFailList
  88. })
  89. })
  90. // 监听服务离开
  91. wx.onLocalServiceLost(function (obj) {
  92. console.log(obj)
  93. })
  94. // 监听搜索停止
  95. wx.onLocalServiceDiscoveryStop(function () {
  96. console.log('监听到搜索停止事件')
  97. })
  98. },
  99. // 取消监听
  100. offLocalService() {
  101. console.log('是否执行此部分数据')
  102. // 取消监听服务发现事件
  103. wx.offLocalServiceFound(function () {
  104. console.log('取消监听服务发现事件 成功')
  105. })
  106. // 取消监听服务解析失败事件
  107. wx.offLocalServiceResolveFail(function () {
  108. console.log('取消监听 mDNS 服务解析失败的事件 成功')
  109. })
  110. // 取消监听服务离开
  111. wx.offLocalServiceLost(function () {
  112. console.log('取消监听服务离开事件 成功')
  113. })
  114. // 取消监听搜索停止
  115. wx.offLocalServiceDiscoveryStop(function () {
  116. console.log('取消监听 mDNS 服务停止搜索的事件 成功')
  117. })
  118. },
  119. onLoad() {
  120. this.setData({
  121. theme: wx.getSystemInfoSync().theme || 'light'
  122. })
  123. if (wx.onThemeChange) {
  124. wx.onThemeChange(({theme}) => {
  125. this.setData({theme})
  126. })
  127. }
  128. }
  129. })