服务发现基础逻辑
This commit is contained in:
+10
-16
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/grandcat/zeroconf"
|
||||
"net"
|
||||
)
|
||||
@@ -12,37 +13,30 @@ type Online struct {
|
||||
AddrIPv6 []net.IP `json:"-"`
|
||||
Port int `json:"port"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"` // 昵称
|
||||
Avatar string `json:"avatar"` // 头像
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
Address string `json:"address"` // 工位
|
||||
Ip string `json:"ip"`
|
||||
}
|
||||
|
||||
func (o *Online) Url(router string) string {
|
||||
return fmt.Sprintf("http://%s:%d%s", o.Ip, o.Port, router)
|
||||
}
|
||||
|
||||
func UserTOnlineList(user *Users) []string {
|
||||
return []string{
|
||||
user.ID,
|
||||
user.Name,
|
||||
user.Avatar,
|
||||
user.Email,
|
||||
user.Phone,
|
||||
user.Address,
|
||||
user.Ip,
|
||||
}
|
||||
}
|
||||
|
||||
func NewOnline(entry *zeroconf.ServiceEntry) (*Online, error) {
|
||||
if len(entry.Text) != 6 {
|
||||
if len(entry.Text) != 2 {
|
||||
return nil, errors.New("invalid online entry")
|
||||
}
|
||||
|
||||
return &Online{
|
||||
AddrIPv4: entry.AddrIPv4,
|
||||
AddrIPv6: entry.AddrIPv6,
|
||||
Port: entry.Port,
|
||||
ID: entry.Text[0],
|
||||
Name: entry.Text[1],
|
||||
Avatar: entry.Text[2],
|
||||
Email: entry.Text[3],
|
||||
Phone: entry.Text[4],
|
||||
Address: entry.Text[5],
|
||||
Ip: entry.Text[1],
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user