diff --git a/golang_learn/data_func/go_to_listen/demo01/main.go b/golang_learn/data_func/go_to_listen/demo01/main.go new file mode 100644 index 0000000..e101fbf --- /dev/null +++ b/golang_learn/data_func/go_to_listen/demo01/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "log" + "net" +) + +func handleConnection(conn net.Conn) { + br := bufio.NewReader(conn) + for { + data, err := br.ReadString('\n') + if err == io.EOF { + break + } + fmt.Printf("%s", data) + fmt.Fprintf(conn, "OK\n") + } + conn.Close() +} +func main() { + ln, err := net.Listen("tcp", ":54022") + if err != nil { + panic(err) + } + for { + conn, err := ln.Accept() + if err != nil { + log.Fatal("get client connection error: ", err) + } + go handleConnection(conn) + } +} diff --git a/golang_learn/data_func/go_to_listen/demo02/main.go b/golang_learn/data_func/go_to_listen/demo02/main.go new file mode 100644 index 0000000..d8357dc --- /dev/null +++ b/golang_learn/data_func/go_to_listen/demo02/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "fmt" + "net" + "os" + "time" +) + +// 获取IP和端口 +func getIpPort() []string { + // 根据接收参数个数,定义动态数组, + ipPorts := make([]string, len(os.Args)-1) + i := 0 + for index, value := range os.Args { + //排除脚本名称 + if index == 0 { + continue + } + //写入数组 + ipPorts[i] = value + i++ + } + return ipPorts +} + +// 检测端口 +func checkPorts(ipPorts []string) { + now := time.Now().Format("2006-01-02 15:04:05") + for _, ipPort := range ipPorts { + // 检测端口 + conn, err := net.DialTimeout("tcp", ipPort, 3*time.Second) + if err != nil { + fmt.Println("["+now+"]", ipPort, "端口未开启(fail)!") + } else { + if conn != nil { + fmt.Println("["+now+"]", ipPort, "端口已开启(success)!") + conn.Close() + } else { + fmt.Println("["+now+"]", ipPort, "端口未开启(fail)!") + } + } + } +} + +func main() { + + ret := getIpPort() + if len(ret) == 0 { + ret = []string{":35017", ":54022"} + } + checkPorts(ret) +} diff --git a/golang_learn/data_func/go_to_tray/demo03/build.bat b/golang_learn/data_func/go_to_tray/demo03/build.bat new file mode 100644 index 0000000..3a9eed4 --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo03/build.bat @@ -0,0 +1,5 @@ +set GO111MODULE=on +set GOARCH=386 +rsrc -manifest main.manifest -ico main.ico -o main.syso +go generate +go build -ldflags="-s -w -H=windowsgui" -o ˵-м.exe diff --git a/golang_learn/data_func/go_to_tray/demo03/main.go b/golang_learn/data_func/go_to_tray/demo03/main.go new file mode 100644 index 0000000..a7c7561 --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo03/main.go @@ -0,0 +1,225 @@ +package main + +import ( + "log" + "net" + "os/exec" + "runtime" + "syscall" + "time" + + "github.com/braintree/manners" + "github.com/lxn/walk" +) + +type MyWindow struct { + *walk.MainWindow + ni *walk.NotifyIcon +} + +func NewMyWindow() *MyWindow { + mw := new(MyWindow) + var err error + mw.MainWindow, err = walk.NewMainWindow() + checkError(err) + return mw +} + +func (mw *MyWindow) AddNotifyIcon() { + var err error + mw.ni, err = walk.NewNotifyIcon(mw) + checkError(err) + _ = mw.ni.SetVisible(true) + + icon, err := walk.NewIconFromResourceId(3) + checkError(err) + _ = mw.SetIcon(icon) + _ = mw.ni.SetIcon(icon) + + // 数据库 服务 + mw.addDbMenu() + + // 应用程序 服务 + mw.addCxMenu() + + // 其他快捷键 + mw.addOther() + +} + +func init() { + +} +func main() { + mw := NewMyWindow() + + mw.AddNotifyIcon() + mw.Run() +} + +func checkError(err error) { + if err != nil { + log.Fatal(err) + } +} + +// open opens the specified URL in the default browser of the user. +func OpenUrl(url string) error { + var cmd string + var args []string + + switch runtime.GOOS { + case "windows": + cmd = "cmd" + args = []string{"/c", "start"} + case "darwin": + cmd = "open" + default: // "linux", "freebsd", "openbsd", "netbsd" + cmd = "xdg-open" + } + args = append(args, url) + c := exec.Command(cmd, args...) + c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + return c.Start() +} + +// addOther 增加其他按钮操作 +func (mw *MyWindow) addOther() { + mw.addAction(nil, "打开应用").Triggered().Attach(func() { + _ = OpenUrl("http://localhost:54022/qggwy") + }) + + helpMenu := mw.addMenu("帮助") + mw.addAction(helpMenu, "升级").Triggered().Attach(func() { + walk.MsgBox(mw, "即将打开网页", "点击《确认》进行下载安装组工软件!", walk.MsgBoxIconInformation) + _ = OpenUrl("https://www.12371.cn/zgrjxz/gwywx/") + }) + + mw.addAction(helpMenu, "关于").Triggered().Attach(func() { + walk.MsgBox(mw, "关于", "辅助使用公务员系统服务", walk.MsgBoxIconInformation) + }) + + mw.addAction(nil, "退出").Triggered().Attach(func() { + mw.ni.Dispose() + mw.Dispose() + walk.App().Exit(0) + }) +} + +// addDbMenu 增加数据库服务按钮操作 +func (mw *MyWindow) addDbMenu() { + dbMenu := mw.addMenu("数据服务") + startDbAction := mw.addAction(dbMenu, "启动") + stopDbAction := mw.addAction(dbMenu, "停止") + + _ = stopDbAction.SetEnabled(false) + startDbAction.Triggered().Attach(func() { + ok := manners.Close() + if !ok { + setEnableAndCheck(startDbAction, stopDbAction, true, false) + mw.msgbox("启动", "启动失败!", walk.MsgBoxIconError) + } else { + setEnableAndCheck(startDbAction, stopDbAction, false, true) + mw.msgbox("启动", "启动成功.", walk.MsgBoxIconInformation) + } + }) + + stopDbAction.Triggered().Attach(func() { + ok := manners.Close() + if !ok { + setEnableAndCheck(startDbAction, stopDbAction, false, true) + mw.msgbox("停止", "停止失败!", walk.MsgBoxIconError) + } else { + setEnableAndCheck(startDbAction, stopDbAction, true, false) + mw.msgbox("停止", "停止成功.", walk.MsgBoxIconInformation) + } + }) + + go AddListen(startDbAction, stopDbAction, ":35017", 3*time.Second) +} + +// addCxMenu 增加程序服务按钮操作 +func (mw *MyWindow) addCxMenu() { + cxMenu := mw.addMenu("应用服务") + startCxAction := mw.addAction(cxMenu, "启动") + stopCxAction := mw.addAction(cxMenu, "停止") + _ = stopCxAction.SetEnabled(false) + startCxAction.Triggered().Attach(func() { + ok := manners.Close() + if !ok { + setEnableAndCheck(startCxAction, stopCxAction, true, false) + mw.msgbox("启动", "启动失败!", walk.MsgBoxIconError) + } else { + setEnableAndCheck(startCxAction, stopCxAction, false, true) + mw.msgbox("启动", "启动成功.", walk.MsgBoxIconInformation) + } + }) + + stopCxAction.Triggered().Attach(func() { + ok := manners.Close() + if ok { + setEnableAndCheck(startCxAction, stopCxAction, false, true) + mw.msgbox("停止", "停止失败!", walk.MsgBoxIconError) + } else { + setEnableAndCheck(startCxAction, stopCxAction, true, false) + mw.msgbox("停止", "停止成功.", walk.MsgBoxIconInformation) + } + }) + + go AddListen(startCxAction, stopCxAction, ":54022", 3*time.Second) +} + +// AddListen 增加服务监听,控制按钮状态 +func AddListen(startAction *walk.Action, stopAction *walk.Action, address string, timeout time.Duration) { + conn, err := net.DialTimeout("tcp", address, timeout) + if err != nil { + // fmt.Println("["+now+"]", ipPort, "端口未开启(fail)!") + setEnableAndCheck(startAction, stopAction, false, false) + } else { + if conn != nil { + // fmt.Println("["+now+"]", ipPort, "端口已开启(success)!") + setEnableAndCheck(startAction, stopAction, false, true) + _ = conn.Close() + } else { + // fmt.Println("["+now+"]", ipPort, "端口未开启(fail)!") + setEnableAndCheck(startAction, stopAction, true, false) + } + } +} + +// setEnableAndCheck 设置按钮状态 +func setEnableAndCheck(startAction, stopAction *walk.Action, startBool, stopBool bool) { + _ = startAction.SetEnabled(startBool) + _ = stopAction.SetEnabled(stopBool) + _ = startAction.SetChecked(!startBool) + _ = stopAction.SetChecked(!stopBool) +} + +// addMenu 增加分类按钮操作 +func (mw *MyWindow) addMenu(name string) *walk.Menu { + helpMenu, err := walk.NewMenu() + checkError(err) + help, err := mw.ni.ContextMenu().Actions().AddMenu(helpMenu) + checkError(err) + _ = help.SetText(name) + + return helpMenu +} + +// addAction 增加分类子按钮操作 +func (mw *MyWindow) addAction(menu *walk.Menu, name string) *walk.Action { + action := walk.NewAction() + _ = action.SetText(name) + if menu != nil { + _ = menu.Actions().Add(action) + } else { + _ = mw.ni.ContextMenu().Actions().Add(action) + } + return action +} + +// msgbox 提示框 +func (mw *MyWindow) msgbox(title, message string, style walk.MsgBoxStyle) { + _ = mw.ni.ShowInfo(title, message) + walk.MsgBox(mw, title, message, style) +} diff --git a/golang_learn/data_func/go_to_tray/demo03/main.ico b/golang_learn/data_func/go_to_tray/demo03/main.ico new file mode 100644 index 0000000..758a8d4 Binary files /dev/null and b/golang_learn/data_func/go_to_tray/demo03/main.ico differ diff --git a/golang_learn/data_func/go_to_tray/demo03/main.manifest b/golang_learn/data_func/go_to_tray/demo03/main.manifest new file mode 100644 index 0000000..4a042b0 --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo03/main.manifest @@ -0,0 +1,22 @@ + + + + + + + + + + + PerMonitorV2, PerMonitor + True + + + + + + + + + + \ No newline at end of file diff --git a/golang_learn/data_func/go_to_tray/demo03/main.syso b/golang_learn/data_func/go_to_tray/demo03/main.syso new file mode 100644 index 0000000..a47c9ab Binary files /dev/null and b/golang_learn/data_func/go_to_tray/demo03/main.syso differ diff --git a/golang_learn/data_func/go_to_tray/demo04/main.go b/golang_learn/data_func/go_to_tray/demo04/main.go new file mode 100644 index 0000000..4099b9c --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo04/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" + "github.com/braintree/manners" + "net/http" +) + +func main() { + err := manners.ListenAndServe(":54022", http.DefaultServeMux) + if err == nil { + fmt.Println("哼") + } + fmt.Println("哈哈") +}