go_echo_wol/controller/ws.go

86 lines
2.0 KiB
Go

package controller
import (
"encoding/json"
"fmt"
"goweb/database"
"goweb/res"
"goweb/wol"
"net"
"golang.org/x/net/websocket"
)
func Send_start(mac string, ws **websocket.Conn, uuid string) {
allNetworkInterfaces, err := net.Interfaces()
if err != nil {
println(err)
err = websocket.Message.Send(*ws, res.Get_res_string(200, "", "获取所有网络接口失败", ""))
if err != nil {
// c.Logger().Error(err)
return
}
return
}
for i := range allNetworkInterfaces {
if int(allNetworkInterfaces[i].Flags) > 50 {
fmt.Printf("正在往 %s 接口发送\n", allNetworkInterfaces[i].Name)
wol.WakespecifyInterfac(mac, "255.255.255.255", "9", allNetworkInterfaces[i].Name)
}
}
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, uuid string) {
wolarray := make([]database.Wol, 0)
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, "", uuid, wolarray))
if err != nil {
// c.Logger().Error(err)
return
}
}
func Del(ws **websocket.Conn, uuid string, id int64) {
database.Del(id)
err := websocket.Message.Send(*ws, res.Get_res_string(200, "", uuid, id))
if err != nil {
// c.Logger().Error(err)
return
}
}
func Add(ws **websocket.Conn, uuid string, name string, mac string) {
wol := new(database.Wol)
wol.Mac = mac
wol.Name = name
database.Insert(wol)
err := websocket.Message.Send(*ws, res.Get_res_string(200, "", uuid, ""))
if err != nil {
// c.Logger().Error(err)
return
}
}
func Edi(ws **websocket.Conn, uuid string, id int64, name string, mac string) {
wol := new(database.Wol)
wol.Mac = mac
wol.Name = name
database.Edi(wol, id)
err := websocket.Message.Send(*ws, res.Get_res_string(200, "", uuid, ""))
if err != nil {
// c.Logger().Error(err)
return
}
}