go_echo_wol/controller/wol_item.go

61 lines
1.4 KiB
Go
Raw Normal View History

2023-09-02 16:05:31 +08:00
package controller
import (
"encoding/json"
"fmt"
2023-09-04 18:00:09 +08:00
"goweb/database"
2023-09-02 16:05:31 +08:00
"goweb/res"
"goweb/wol"
"net"
"golang.org/x/net/websocket"
)
func Send_start(mac string, ws **websocket.Conn) {
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, "", ""))
if err != nil {
// c.Logger().Error(err)
return
}
}
func Getall(ws **websocket.Conn) {
2023-09-04 18:00:09 +08:00
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)
2023-09-02 16:05:31 +08:00
v, err := json.Marshal(wolarray)
if err != nil {
fmt.Println("marshal failed!", err)
return
}
err = websocket.Message.Send(*ws, res.Get_res_string(200, "", "", string(v)))
if err != nil {
// c.Logger().Error(err)
return
}
}