完善个人数据维护, 初步建立局域网链接逻辑
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
"work_cation/cfg"
|
||||
"work_cation/global"
|
||||
"work_cation/repo"
|
||||
)
|
||||
|
||||
type serverService struct {
|
||||
Status string
|
||||
server *http.Server
|
||||
}
|
||||
|
||||
const (
|
||||
StatusOnline = "online"
|
||||
StatusOffline = "offline"
|
||||
)
|
||||
|
||||
var Server = &serverService{Status: StatusOffline}
|
||||
|
||||
func (s *serverService) Online() error {
|
||||
if s.Status == StatusOnline {
|
||||
return nil
|
||||
}
|
||||
// 启动服务发现
|
||||
user := repo.User.GetUserInfo(global.DB)
|
||||
if err := Zeroconf.Register(user); err != nil {
|
||||
return err
|
||||
}
|
||||
// 启动服务
|
||||
if err := s.StartListenServer(); err != nil {
|
||||
return err
|
||||
}
|
||||
// 修改状态
|
||||
s.Status = StatusOnline
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *serverService) StatusOffline() error {
|
||||
if err := s.server.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
// 关闭服务发现
|
||||
Zeroconf.Close()
|
||||
// 修改状态
|
||||
s.Status = StatusOffline
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *serverService) StartListenServer() error {
|
||||
router := gin.New()
|
||||
router.GET("/", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"message": "hello"})
|
||||
})
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: cfg.T.ServerAddr,
|
||||
Handler: router,
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Fatalf("listen: %s\n", err)
|
||||
}
|
||||
}()
|
||||
s.server = srv
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/grandcat/zeroconf"
|
||||
"time"
|
||||
"work_cation/cfg"
|
||||
"work_cation/models"
|
||||
)
|
||||
|
||||
type zeroconfService struct {
|
||||
server *zeroconf.Server
|
||||
}
|
||||
|
||||
var Zeroconf = &zeroconfService{}
|
||||
|
||||
// Register 注册被发现服务
|
||||
func (s *zeroconfService) Register(user *models.Users) error {
|
||||
if s.server != nil {
|
||||
return nil // already registered
|
||||
}
|
||||
server, err := zeroconf.Register(cfg.T.ZeroconfKey, "_http._tcp",
|
||||
"local.", cfg.T.ZeroconfPort, models.UserTOnlineList(user), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.server = server
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *zeroconfService) Close() {
|
||||
if s.server != nil {
|
||||
s.server.Shutdown()
|
||||
s.server = nil
|
||||
}
|
||||
}
|
||||
|
||||
// FindService 查找被发现服务
|
||||
func (s *zeroconfService) FindService() (chan *models.Online, error) {
|
||||
resolver, err := zeroconf.NewResolver(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
allEntries := make(chan *zeroconf.ServiceEntry)
|
||||
useEntries := make(chan *models.Online)
|
||||
|
||||
go func(results <-chan *zeroconf.ServiceEntry) {
|
||||
for entry := range results {
|
||||
if entry.ServiceInstanceName() == fmt.Sprintf("%s._http._tcp.local.", cfg.T.ZeroconfKey) {
|
||||
//fmt.Printf("发现服务: %s \nIP:%s:%d \nInfo: %v\n", entry.ServiceInstanceName(), entry.AddrIPv4, entry.Port, entry.Text)
|
||||
online, err := models.NewOnline(entry)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
useEntries <- 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
|
||||
}
|
||||
Reference in New Issue
Block a user