<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## HTTP2 HTTP/2 (原本的名字是 HTTP/2.0) 是萬維網使用的 HTTP 網絡協議的第二個主要版本。HTTP/2 提供了更快的速度和更好的用戶體驗。 ### 特性 - 使用二進制格式傳輸數據,而不是文本。使得在解析和優化擴展上更為方便。 - 多路復用,所有的請求都是通過一個 TCP 連接并發完成。 - 對消息頭采用 HPACK 進行壓縮傳輸,能夠節省消息頭占用的網絡的流量。 - Server Push:服務端能夠更快的把資源推送給客戶端。 ## 怎樣運行 HTTP2 和 HTTPS 服務? ### 生成一個自簽名的 X.509 TLS 證書(HTTP/2 需要 TLS 才能運行) ```sh go run $GOROOT/src/crypto/tls/generate_cert.go --host localhost ``` 上面的命令會生一個`cert.pem` 和`key.pem` 文件。 > 這里只是展示使用,所以我們用了自簽名的證書,正式環境建議去 > [CA](https://zh.wikipedia.org/wiki/%E6%95%B0%E5%AD%97%E8%AF%81%E4%B9%A6%E8%AE%A4%E8%AF%81%E6%9C%BA%E6%9E%84)申請證書。 ### 配置服務器引擎 `engine.Config` `server.go` ```go package main import ( "fmt" "net/http" "time" "github.com/labstack/echo" ) func request(c echo.Context) error { req := c.Request() format := "<pre><strong>Request Information</strong>\n\n<code>Protocol: %s\nHost: %s\nRemote Address: %s\nMethod: %s\nPath: %s\n</code></pre>" return c.HTML(http.StatusOK, fmt.Sprintf(format, req.Proto, req.Host, req.RemoteAddr, req.Method, req.URL.Path)) } func stream(c echo.Context) error { res := c.Response() gone := res.CloseNotify() res.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8) res.WriteHeader(http.StatusOK) ticker := time.NewTicker(1 * time.Second) defer ticker.Stop() fmt.Fprint(res, "<pre><strong>Clock Stream</strong>\n\n<code>") for { fmt.Fprintf(res, "%v\n", time.Now()) res.Flush() select { case <-ticker.C: case <-gone: break } } } func main() { e := echo.New() e.GET("/request", request) e.GET("/stream", stream) e.Logger.Fatal(e.StartTLS(":1323", "cert.pem", "key.pem")) } ``` ### 最后 - https://localhost:1323/request (顯示 HTTP 請求信息) - https://localhost:1323/stream (實時展示當前時間)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看