Browse Source

实现基本的菜单栏工具设置

master
VIVIMAN 3 years ago
parent
commit
93dbce72c9
  1. 39
      golang_learn/data_func/go_to_socket/demo01/main.go
  2. 27
      golang_learn/data_func/go_to_socket/demo02/main.go
  3. 32
      golang_learn/data_func/go_to_socket/demo03/main.go
  4. 31
      golang_learn/data_func/go_to_socket/demo04/main.go
  5. 58
      golang_learn/data_func/go_to_socket/demo10/main.go
  6. 32
      golang_learn/data_func/go_to_socket/demo11/main.go
  7. 2
      golang_learn/data_func/go_to_tray/demo01/main.go
  8. 1
      golang_learn/data_func/go_to_tray/demo03/main.go
  9. 4
      golang_learn/data_func/go_to_tray/demo04/build.sh
  10. 135
      golang_learn/data_func/go_to_tray/demo04/main.go

39
golang_learn/data_func/go_to_socket/demo01/main.go

@ -0,0 +1,39 @@
package main
import (
"fmt"
"log"
"net"
"time"
)
func echo(conn *net.TCPConn) {
tick := time.Tick(5 * time.Second) // 五秒的心跳间隔
for now := range tick {
n, err := conn.Write([]byte(now.String()))
if err != nil {
log.Println(err)
conn.Close()
return
}
fmt.Printf("send %d bytes to %s\n", n, conn.RemoteAddr())
}
}
func main() {
address := net.TCPAddr{
IP: net.ParseIP("127.0.0.1"), // 把字符串IP地址转换为net.IP类型
Port: 8000,
}
listener, err := net.ListenTCP("tcp4", &address) // 创建TCP4服务器端监听器
if err != nil {
log.Fatal(err) // Println + os.Exit(1)
}
for {
conn, err := listener.AcceptTCP()
if err != nil {
log.Fatal(err) // 错误直接退出
}
fmt.Println("remote address:", conn.RemoteAddr())
go echo(conn)
}
}

27
golang_learn/data_func/go_to_socket/demo02/main.go

@ -0,0 +1,27 @@
package main
import (
"log"
"net"
"os"
)
func main() {
if len(os.Args) != 2 {
log.Fatalf("Usage: %s host:port", os.Args[0])
}
service := os.Args[1]
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
if err != nil {
log.Fatal(err)
}
conn, err := net.DialTCP("tcp4", nil, tcpAddr)
if err != nil {
log.Fatal(err)
}
n, err := conn.Write([]byte("HEAD / HTTP/1.1\r\n\r\n"))
if err != nil {
log.Fatal(err)
}
log.Fatal(n)
}

32
golang_learn/data_func/go_to_socket/demo03/main.go

@ -0,0 +1,32 @@
package main
import (
"fmt"
"net"
"os"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s host:port", os.Args[0])
os.Exit(1)
}
service := os.Args[1]
udpAddr, err := net.ResolveUDPAddr("udp4", service)
checkError(err)
conn, err := net.DialUDP("udp", nil, udpAddr)
checkError(err)
_, err = conn.Write([]byte("anything"))
checkError(err)
var buf [512]byte
n, err := conn.Read(buf[0:])
checkError(err)
fmt.Println(string(buf[0:n]))
os.Exit(0)
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error %s", err.Error())
os.Exit(1)
}
}

31
golang_learn/data_func/go_to_socket/demo04/main.go

@ -0,0 +1,31 @@
package main
import (
"fmt"
"net"
"os"
"time"
)
func main() {
service := ":8000"
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
daytime := time.Now().String()
conn.Write([]byte(daytime)) // don't care about return value
conn.Close() // we're finished with this client
}
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}

58
golang_learn/data_func/go_to_socket/demo10/main.go

@ -0,0 +1,58 @@
package main
import (
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
)
func main() {
service := ":1200"
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go handleClient(conn)
}
}
func handleClient(conn net.Conn) {
conn.SetReadDeadline(time.Now().Add(2 * time.Minute)) // set 2 minutes timeout
request := make([]byte, 128) // set maxium request length to 128B to prevent flood attack
defer conn.Close() // close connection before exit
for {
read_len, err := conn.Read(request)
if err != nil {
fmt.Println(err)
break
}
if read_len == 0 {
break // connection already closed by client
} else if strings.TrimSpace(string(request[:read_len])) == "timestamp" {
daytime := strconv.FormatInt(time.Now().Unix(), 10)
conn.Write([]byte(daytime))
} else {
daytime := time.Now().String()
conn.Write([]byte(daytime))
}
request = make([]byte, 128) // clear last read content
}
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}

32
golang_learn/data_func/go_to_socket/demo11/main.go

@ -0,0 +1,32 @@
package main
import (
"fmt"
"io/ioutil"
"net"
"os"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s host:port ", os.Args[0])
os.Exit(1)
}
service := os.Args[1]
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
checkError(err)
conn, err := net.DialTCP("tcp", nil, tcpAddr)
checkError(err)
_, err = conn.Write([]byte("HEAD / HTTP/1.0\r\n\r\n"))
checkError(err)
result, err := ioutil.ReadAll(conn)
checkError(err)
fmt.Println(string(result))
os.Exit(0)
}
func checkError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}

2
golang_learn/data_func/go_to_tray/demo01/main.go

@ -20,7 +20,7 @@ func main() {
func onReady() {
timezone = "默认文字"
systray.SetIcon(getIcon("/Users/zhangxianwei/GolandProjects/golangs_learn/golang_learn/data_func/go_to_tray/demo01/icon.ico"))
_ = systray.SetIcon(getIcon("/Users/zhangxianwei/GolandProjects/golangs_learn/golang_learn/data_func/go_to_tray/demo01/icon.ico"))
submenu := systray.AddSubMenu("子菜单")
_ = submenu.AddSubMenuItem("开始", "", 0)

1
golang_learn/data_func/go_to_tray/demo03/main.go

@ -4,6 +4,7 @@
package main
import (
"github.com/lxn/walk"
"log"
"net"
"os/exec"

4
golang_learn/data_func/go_to_tray/demo04/build.sh

@ -1,5 +1,7 @@
SET CGO_ENABLED=0
set GOARCH=amd
# shellcheck disable=SC2121
set GOARCH=amd64
# shellcheck disable=SC2121
set GOOS=linux
rsrc -manifest main.manifest -ico main.ico -o main.syso
go generate

135
golang_learn/data_func/go_to_tray/demo04/main.go

@ -1,30 +1,36 @@
package main
import (
"fmt"
"github.com/riftbit/go-systray"
"io/ioutil"
)
var (
timezone string
"log"
"os/exec"
"runtime"
"syscall"
)
func main() {
systray.Run(onReady, onExit)
_ = systray.Run(onReady, onExit)
}
func onReady() {
timezone = "公务员"
_ = systray.SetIcon(getIcon("/Users/zhangxianwei/GolandProjects/golangs_learn/golang_learn/data_func/go_to_tray/demo01/icon.ico"))
timezone := "公务员"
_ = systray.SetTitle(timezone)
_ = systray.SetTooltip(timezone)
_ = systray.SetIcon(getIcon("D:\\ViviCode\\golangs_learn\\golang_learn\\data_func\\go_to_tray\\demo04\\main.ico"))
submenu := systray.AddSubMenu("子菜单")
_ = submenu.AddSubMenuItem("开始", "", 0)
_ = submenu.AddSubMenuItem("结束", "", 0)
dbMenu := systray.AddSubMenu("数据服务")
startDbMenu := dbMenu.AddSubMenuItem("启动", "启动 - 数据库服务", 0)
stopDbMenu := dbMenu.AddSubMenuItem("停止", "停止 - 数据库服务", 0)
cxMenu := systray.AddSubMenu("应用服务")
startCxMenu := cxMenu.AddSubMenuItem("启动", "启动 - 应用服务", 0)
stopCxMenu := cxMenu.AddSubMenuItem("停止", "停止 - 应用服务", 0)
localTime := systray.AddMenuItem("炎龙", "炎龙", 0)
openMenu := systray.AddMenuItem("打开应用", "打开应用", 0)
fmt.Printf("%#v", localTime)
submenu := systray.AddSubMenu("帮助")
upCxMenu := submenu.AddSubMenuItem("升级", "进行下载安装组工软件", 0)
infoMenu := submenu.AddSubMenuItem("关于", "辅助使用公务员系统服务", 0)
systray.AddSeparator()
mQuit := systray.AddMenuItem("退出", "退出", 0)
@ -32,10 +38,29 @@ func onReady() {
go func() {
for {
select {
case <-localTime.OnClickCh():
timezone = "炎龙"
case <-startDbMenu.OnClickCh():
_ = OpenServe(true)
return
case <-stopDbMenu.OnClickCh():
_ = CloseServe(true)
return
case <-startCxMenu.OnClickCh():
_ = OpenServe(false)
return
case <-stopCxMenu.OnClickCh():
_ = CloseServe(false)
return
case <-openMenu.OnClickCh():
_ = OpenUrl("http://localhost:54022/qggwy")
return
case <-upCxMenu.OnClickCh():
_ = OpenUrl("https://www.12371.cn/zgrjxz/gwywx/")
return
case <-infoMenu.OnClickCh():
_ = OpenUrl("https://www.12371.cn/zgrjxz/gwywx/")
return
case <-mQuit.OnClickCh():
systray.Quit()
_ = systray.Quit()
return
}
}
@ -49,7 +74,83 @@ func onExit() {
func getIcon(s string) []byte {
b, err := ioutil.ReadFile(s)
if err != nil {
fmt.Print(err)
log.Println(err)
}
return b
}
// OpenUrl 按照地址打开浏览器,进行请求。
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()
}
// OpenServe 打开服务
func OpenServe(isDb bool) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
if isDb {
cmd = "cmd"
args = []string{"/c", "./shell/startDB.bat"}
} else {
cmd = "/bin/sh"
args = []string{"-c", "./shell/startDB.sh"}
}
default: // "linux", "freebsd", "openbsd", "netbsd", "mac"
if isDb {
cmd = "cmd"
args = []string{"/c", "./shell/startCX.bat"}
} else {
cmd = "/bin/sh"
args = []string{"-c", "./shell/startCX.sh"}
}
}
c := exec.Command(cmd, args...)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
return c.Start()
}
// CloseServe 停止服务
func CloseServe(isDb bool) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
if isDb {
cmd = "cmd"
args = []string{"/c", "./shell/stopDB.bat"}
} else {
cmd = "/bin/sh"
args = []string{"-c", "./shell/stopDB.sh"}
}
default: // "linux", "freebsd", "openbsd", "netbsd", "mac"
if isDb {
cmd = "cmd"
args = []string{"/c", "./shell/stopCX.bat"}
} else {
cmd = "/bin/sh"
args = []string{"-c", "./shell/stopCX.sh"}
}
}
c := exec.Command(cmd, args...)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
return c.Start()
}

Loading…
Cancel
Save