miketheprogrammer / go-thrust

Cross Platform UI Kit powered by Blink/V8/Chromium Content Lib
MIT License
445 stars 34 forks source link

Why do I need the webview in the basic_webserver_app.go example? #72

Open jheuel opened 8 years ago

jheuel commented 8 years ago

I was wondering why my net/http server does not terminate when I close the go-thrust window while using an index page like <html><body>hi</body></html> instead of the html given in basic_webserver_app.go.

I would think that the web server should terminate without the webview part. Can you help me understand why go-thrust is working that way or is this behaviour not intended?

This is the basic_webserver_app.go example without the javascript block which does not terminate on my machine:

package main

import (
    "fmt"
    "net/http"

    "github.com/miketheprogrammer/go-thrust/thrust"
    "github.com/miketheprogrammer/go-thrust/tutorials/provisioner"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, htmlIndex)
}

func main() {
    http.HandleFunc("/", handler)
    thrust.InitLogger()
    // Set any Custom Provisioners before Start
    thrust.SetProvisioner(tutorial.NewTutorialProvisioner())
    // thrust.Start() must always come before any bindings are created.
    thrust.Start()

    mysession := thrust.NewSession(false, false, "cache")

    thrustWindow := thrust.NewWindow(thrust.WindowOptions{
        RootUrl: "http://localhost:8080/",
        Session: mysession,
    })
    thrustWindow.Show()
    thrustWindow.Maximize()
    thrustWindow.Focus()

    // See, we dont use thrust.LockThread() because we now have something holding the process open
    http.ListenAndServe(":8080", nil)
}

var htmlIndex string = `
<html>
  <body>
    <h2> Welcome to Go-Thrust <h3>
    <img height="50px" width="120px" src="http://i.imgur.com/DwFKI0J.png"/>
  </body>
</html>`

Tested on:

4.4.0-22-generic #40-Ubuntu SMP Thu May 12 22:03:46 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
go version go1.6.1 linux/amd64
miketheprogrammer commented 8 years ago

If i understand correctly, you want the webserver to close, when the user closes a browser window. The correct way to handle this would be to listen to a window event

// In https://github.com/miketheprogrammer/go-thrust/blob/master/tutorials/basic_window_events/basic_window_events.go

    /*
        Here we use a CommandResponse callback just because we can, it provides us more data
        than the EventResult callback
    */
    onclose, err := thrust.NewEventHandler("closed", func(cr commands.EventResult) {
        fmt.Println("Close Event Occured")
    })
    fmt.Println(onclose)
    if err != nil {
        fmt.Println(err)
        connection.CleanExit()
    }

In the above event you could do something like server.close or server.kill or whatever the API is for that stop server action.

miketheprogrammer commented 8 years ago

However I should mention, depending on how you architected your app, you could have more than one window. Meaning you would have to keep track of all the windows and their events.

on evt: 'window.close' if (len(windows) === 1) { windows.remove(current) webserver.close } else { window.remove(current) }

jheuel commented 8 years ago

I was just wondering why the program terminated in one case but not in the other case where solely the html was different.

Thanks for your input about window events I will look into that. Really like the work you have done. :-)