go_echo_wol/controller/ws.go

63 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"
)
2023-09-05 17:53:38 +08:00
func Send_start(mac string, ws **websocket.Conn, uuid string) {
2023-09-02 16:05:31 +08:00
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)
}
}
2023-09-05 17:53:38 +08:00
err = websocket.Message.Send(*ws, res.Get_res_string(200, "已经向地址发送 mac: "+mac, uuid, ""))
2023-09-02 16:05:31 +08:00
if err != nil {
// c.Logger().Error(err)
return
}
}
2023-09-05 17:53:38 +08:00
func Getall(ws **websocket.Conn, uuid string) {
2023-09-04 18:00:09 +08:00
wolarray := make([]database.Wol, 0)
2023-09-05 17:53:38 +08:00
database.Getallwol(&wolarray)
2023-09-02 16:05:31 +08:00
v, err := json.Marshal(wolarray)
2023-09-05 17:53:38 +08:00
println(v)
2023-09-02 16:05:31 +08:00
if err != nil {
fmt.Println("marshal failed!", err)
return
}
2023-09-05 17:53:38 +08:00
err = websocket.Message.Send(*ws, res.Get_res_string(200, "", uuid, wolarray))
2023-09-02 16:05:31 +08:00
if err != nil {
// c.Logger().Error(err)
return
}
}
2023-09-08 10:34:03 +08:00
func Del(ws **websocket.Conn, uuid string, id int) {
2023-09-08 11:21:13 +08:00
database.Del(id)
2023-09-08 10:34:03 +08:00
err := websocket.Message.Send(*ws, res.Get_res_string(200, "", uuid, id))
if err != nil {
// c.Logger().Error(err)
return
}
}