- 改动:go.mod module 声明与全部内部导入路径补全为完整域名路径,README 同步 - 原因:裸模块名无法被 go get 解析,发版前必须修正为 GOPRIVATE 可拉取路径
55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
// E5 — mDNS service discovery example.
|
|
// Demonstrates: create MipsService → init → discover _miot-central._tcp.local. → print results.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.zeroonesoft.cn/golib/xiaomihome/miot"
|
|
)
|
|
|
|
func main() {
|
|
// 1. Create MIPS service browser
|
|
svc := miot.NewMipsService()
|
|
|
|
// 2. Initialize mDNS browser
|
|
svc.Init()
|
|
defer svc.Deinit()
|
|
fmt.Println("mDNS 服务浏览器已启动")
|
|
|
|
// 3. Subscribe to service changes
|
|
svc.SubServiceChange("demo", "*", func(groupID string, state miot.MipsServiceState, data map[string]interface{}) {
|
|
fmt.Printf("\n[服务变化] groupID=%s, state=%s\n", groupID, state)
|
|
for k, v := range data {
|
|
fmt.Printf(" %s: %v\n", k, v)
|
|
}
|
|
})
|
|
|
|
// 4. Wait a moment for initial discovery
|
|
fmt.Println("正在搜索 MIoT 中央网关 (5秒)...")
|
|
time.Sleep(5 * time.Second)
|
|
|
|
// 5. Get and print all discovered services
|
|
services := svc.GetServices("")
|
|
fmt.Println("\n=== 已发现的 MIoT 服务 ===")
|
|
if len(services) == 0 {
|
|
fmt.Println(" 未发现任何服务 (可能需要等待更长时间)")
|
|
} else {
|
|
for groupID, s := range services {
|
|
fmt.Printf(" GroupID: %s\n", groupID)
|
|
fmt.Printf(" Name: %s\n", s.Name)
|
|
fmt.Printf(" Addresses: %v\n", s.Addresses)
|
|
fmt.Printf(" Port: %d\n", s.Port)
|
|
fmt.Printf(" DID: %s\n", s.DID)
|
|
fmt.Printf(" Server: %s\n", s.Server)
|
|
fmt.Println()
|
|
}
|
|
}
|
|
|
|
// 6. Keep browser running for more discoveries
|
|
fmt.Println("继续监听服务变化 (30秒)...")
|
|
time.Sleep(30 * time.Second)
|
|
fmt.Println("示例结束")
|
|
}
|