暂时能用了

This commit is contained in:
giaogiao 2023-09-05 17:53:38 +08:00
parent 39071c1550
commit 1292175126
7 changed files with 36 additions and 23 deletions

View File

@ -11,7 +11,7 @@ import (
"golang.org/x/net/websocket"
)
func Send_start(mac string, ws **websocket.Conn) {
func Send_start(mac string, ws **websocket.Conn, uuid string) {
allNetworkInterfaces, err := net.Interfaces()
if err != nil {
println(err)
@ -29,30 +29,23 @@ func Send_start(mac string, ws **websocket.Conn) {
}
}
err = websocket.Message.Send(*ws, res.Get_res_string(200, "已经向地址发送 mac: "+mac, "", ""))
err = websocket.Message.Send(*ws, res.Get_res_string(200, "已经向地址发送 mac: "+mac, uuid, ""))
if err != nil {
// c.Logger().Error(err)
return
}
}
func Getall(ws **websocket.Conn) {
func Getall(ws **websocket.Conn, uuid string) {
wolarray := make([]database.Wol, 0)
// wol_item := new(wol)
// wol_item.ID = 1
// wol_item.Name = "电脑"
// wol_item.Address = "10:7B:44:80:F4:6A"
// wol_item2 := new(wol)
// wol_item2.ID = 2
// wol_item2.Name = "aa"
// wol_item2.Address = "10:7B:44:80:F4:6A"
// wolarray = append(wolarray, *wol_item, *wol_item2)
database.Getallwol(&wolarray)
v, err := json.Marshal(wolarray)
println(v)
if err != nil {
fmt.Println("marshal failed!", err)
return
}
err = websocket.Message.Send(*ws, res.Get_res_string(200, "", "", string(v)))
err = websocket.Message.Send(*ws, res.Get_res_string(200, "", uuid, wolarray))
if err != nil {
// c.Logger().Error(err)
return

View File

@ -18,7 +18,7 @@ func init() {
file := "./wol.db"
engine, err = xorm.NewEngine("sqlite3", file)
if err != nil {
panic(err)
println(err)
}
@ -26,14 +26,31 @@ func init() {
func CreateTable() int {
err := engine.Sync2(new(Wol))
if err != nil {
panic(err)
println(err)
}
return 0
}
func Getallwol(everyone *[]Wol) {
// everyone := make([]wol, 0)
err := engine.Find(&(*everyone))
if err != nil {
println("Getallwol err", err)
}
}
func Getallwoltext() {
everyone := make([]Wol, 0)
err := engine.Find(&everyone)
if err != nil {
panic(err)
println("Getallwoltext", err)
}
println(len(everyone))
}
func Insert(wol *Wol) {
affected, err := engine.Insert(&wol)
if err != nil {
println(err)
}
println(affected)
}

1
go.mod
View File

@ -12,6 +12,7 @@ require (
github.com/cli/safeexec v1.0.1 // indirect
github.com/cosmtrek/air v1.45.0 // indirect
github.com/creack/pty v1.1.18 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect

1
go.sum
View File

@ -104,6 +104,7 @@ github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=

View File

@ -69,6 +69,7 @@ func hello(c echo.Context) error {
type TooLTT struct {
Method string `json:"method"`
Data Data `json:"data"`
Uuid string `json:"uuid"`
}
websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
@ -93,9 +94,9 @@ func hello(c echo.Context) error {
json.Unmarshal([]byte(msg), &wsValue)
switch {
case wsValue.Method == "start":
controller.Send_start(wsValue.Data.Mac, &ws)
controller.Send_start(wsValue.Data.Mac, &ws, wsValue.Uuid)
case wsValue.Method == "getall":
controller.Getall(&ws)
controller.Getall(&ws, wsValue.Uuid)
default:
fmt.Printf("error")
}

View File

@ -5,15 +5,15 @@ import "encoding/json"
type response struct {
Code int
Message string
Error string
Data string
Uuid string
Data interface{} `json:"data,omitempty"`
}
func Get_res_string(Code int, Message string, Error string, Data string) string {
func Get_res_string(Code int, Message string, Uuid string, Data ...interface{}) string {
var res = new(response)
res.Code = Code
res.Message = Message
res.Error = Error
res.Uuid = Uuid
res.Data = Data
res_str, err := json.Marshal(res)
if err != nil {

BIN
wol.db

Binary file not shown.