完成了分享下载全部流程
This commit is contained in:
+16
-15
@@ -4,13 +4,14 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/grandcat/zeroconf"
|
||||
"time"
|
||||
"slices"
|
||||
"work_cation/cfg"
|
||||
"work_cation/models"
|
||||
)
|
||||
|
||||
type zeroconfService struct {
|
||||
server *zeroconf.Server
|
||||
server *zeroconf.Server
|
||||
onlines []models.Online
|
||||
}
|
||||
|
||||
var Zeroconf = &zeroconfService{}
|
||||
@@ -37,31 +38,31 @@ func (s *zeroconfService) Close() {
|
||||
}
|
||||
|
||||
// FindService 查找被发现服务
|
||||
func (s *zeroconfService) FindService() (chan *models.Online, error) {
|
||||
func (s *zeroconfService) FindService() ([]models.Online, error) {
|
||||
return s.onlines, nil
|
||||
}
|
||||
|
||||
func (s *zeroconfService) StartFindService() error {
|
||||
resolver, err := zeroconf.NewResolver(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
allEntries := make(chan *zeroconf.ServiceEntry)
|
||||
useEntries := make(chan *models.Online)
|
||||
|
||||
go func(results <-chan *zeroconf.ServiceEntry) {
|
||||
for entry := range results {
|
||||
fmt.Println(entry.AddrIPv4, entry.ServiceInstanceName())
|
||||
if entry.ServiceInstanceName() == fmt.Sprintf("%s._http._tcp.local.", cfg.T.ZeroconfKey) {
|
||||
online, err := models.NewOnline(entry)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
useEntries <- online
|
||||
delOnlines := slices.DeleteFunc(s.onlines, func(item models.Online) bool {
|
||||
return item.ID == online.ID
|
||||
})
|
||||
s.onlines = append(delOnlines, *online)
|
||||
}
|
||||
}
|
||||
}(allEntries)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
err = resolver.Browse(ctx, "_http._tcp", "local.", allEntries)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return nil, err
|
||||
}
|
||||
time.AfterFunc(10*time.Second, cancel)
|
||||
return useEntries, nil
|
||||
err = resolver.Browse(context.Background(), "_http._tcp", "local.", allEntries)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user