前往:服务器开始监听后,如何启动浏览器?
In Go, how can I start the browser AFTER the server started listening ?
Preferably the simplest way possible.
My code so far, super dumbed down to the point:
package main
import (
// Standard library packages
"fmt"
"net/http"
"github.com/skratchdot/open-golang/open"
// Third party packages
"github.com/julienschmidt/httprouter"
)
// go get github.com/toqueteos/webbrowser
func main() {
// Instantiate a new router
r := httprouter.New()
// Add a handler on /test
r.GET("/test", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// Simply write some test data for now
fmt.Fprint(w, "Welcome!
")
})
//open.Run("https://google.com/")
// open.Start("https://google.com")
// http://127.0.0.1:3000/test
// Fire up the server
http.ListenAndServe("localhost:3000", r)
fmt.Println("ListenAndServe is blocking")
open.RunWith("http://localhost:3000/test", "firefox")
fmt.Println("Done")
}
在Go中,如何在服务器开始侦听后启动浏览器?
最好是最简单的方法 。 p>
到目前为止,我的代码超级笨拙,直到: p>
package main
import(
//标准 库软件包
“ fmt”
“ net / http”
“ github.com/skratchdot/open-golang/open"
//第三方软件包
” github.com/julienschmidt/httprouter"
)
//获取github.com/toqueteos/webbrowser
func main(){
//实例化一个新路由器
r:= httprouter.New()
//添加一个 / test
r.GET(“ / test”,func(w http.ResponseWriter,r * http.Request,_ httprouter.Params)上的处理程序{
//现在只写一些测试数据
fmt.Fprint (w,“ Welcome!
”)
})
//open.Run("https://google.com/")
// open.Start(“ https:// google.com“)
// http://127.0.0.1:3000/test
//启动服务器
http.ListenAndServe(” localhost:3000“,r)
fmt.Printl n(“ ListenAndServe正在阻止”)
open.RunWith(“ http:// localhost:3000 / test”,“ firefox”)
fmt.Println(“ Done”)
}
code>
div>
Open the listener, start the browser and then enter the server loop:
l, err := net.Listen("tcp", "localhost:3000")
if err != nil {
log.Fatal(err)
}
// The browser can connect now because the listening socket is open.
err := open.Start("http://localhost:3000/test")
if err != nil {
log.Println(err)
}
// Start the blocking server loop.
log.Fatal(http.Serve(l, r))
There's no need to poll as shown in another answer. The browser will connect if the listening socket is open before the browser is started.
ListenAndServe is a convenience function that opens a socket and calls Serve. The code in this answer splits out these steps so the browser can be opened after listening starts but before the blocking call to Serve.
If there is no error, http.ListenAndServe()
will never return. So you shouldn't add code after that except code that handles failure.
You have to start a new goroutine, so ListenAndServe()
is called in one goroutine, and code checking if it is up should run on the other goroutine.
And you can check if your server is up by making a simple HTTP GET
call to it, for example using http.Get()
.
The following example delays startup for 7 seconds on purpose. The new goroutine starts an endless for
loop that checks if server is up, sleeping 1 second between attempts.
Example:
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hi!"))
})
go func() {
for {
time.Sleep(time.Second)
log.Println("Checking if started...")
resp, err := http.Get("http://localhost:8081")
if err != nil {
log.Println("Failed:", err)
continue
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Println("Not OK:", resp.StatusCode)
continue
}
// Reached this point: server is up and running!
break
}
log.Println("SERVER UP AND RUNNING!")
}()
log.Println("Starting server...")
time.Sleep(time.Second * 7)
log.Fatal(http.ListenAndServe(":8081", nil))
Example output:
2015/09/23 13:53:03 Starting server...
2015/09/23 13:53:04 Checking if started...
2015/09/23 13:53:06 Failed: Get http://localhost:8081: dial tcp [::1]:8081: connectex: No connection could be made because the target machine actively refused it.
2015/09/23 13:53:07 Checking if started...
2015/09/23 13:53:09 Failed: Get http://localhost:8081: dial tcp [::1]:8081: connectex: No connection could be made because the target machine actively refused it.
2015/09/23 13:53:10 Checking if started...
2015/09/23 13:53:10 SERVER UP AND RUNNING!