Go:如何在服务器开始监听后启动浏览器?

Ste*_*ger 10 http go server

在Go中,如何在服务器开始监听后启动浏览器?
最好是最简单的方法.

到目前为止,我的代码超级愚蠢到了这一点:

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!\n")
    })

    //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")
}
Run Code Online (Sandbox Code Playgroud)

Cer*_*món 20

打开监听器,启动浏览器,然后进入服务器循环:

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)) 
Run Code Online (Sandbox Code Playgroud)

没有必要进行轮询,如另一个答案中所示.如果在启动浏览器之前打开侦听套接字,浏览器将连接.

ListenAndServe是一个便利功能,可以打开一个套接字并调用Serve.此答案中的代码拆分了这些步骤,因此浏览器可以在收听开始后但在阻止调用Serve之前打开.

  • 这真的是正确答案。侦听套接字有一个内部固定大小的积压队列,即使在给定时间没有“接受”主动等待,它也可以接受连接。这就是它起作用的原因。 (2认同)
  • [`server.ListenAndServe`](https://golang.org/src/net/http/server.go#L2576)似乎也使用了“ tcpKeepAliveListener”。似乎将套接字的TCP keepalive设置为3分钟。您可能也应该这样做。 (2认同)

icz*_*cza 13

如果没有错误,http.ListenAndServe()将永远不会返回.因此,除了处理失败的代码之外,您不应该在此之后添加代码.

你必须开始一个新的goroutine,所以ListenAndServe()在一个goroutine中调用,并且代码检查是否它应该运行在另一个goroutine上.

您可以通过对其进行简单的HTTP GET调用来检查您的服务器是否已启动,例如使用http.Get().

以下示例故意延迟启动7秒.新的goroutine启动一个无限for循环,检查服务器是否启动,在尝试之间休息1秒.

例:

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))
Run Code Online (Sandbox Code Playgroud)

示例输出:

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!
Run Code Online (Sandbox Code Playgroud)