2013-04-23 66 views
2

我正在測試反向代理。主要用於播放來自其他後端服務器的底層nginx和流式視頻。視頻流中的導航,golang中的反向代理

問題是在瀏覽視頻時。例如,通過代理玩vlc時 - 視頻正常啓動,但在嘗試導航時停止。但是,如果我直接從nginx播放這個視頻 - 它工作正常。

我預計導航播放器會創建與Range: N-標題的新連接,但只有在再次啓動視頻時纔會有新的連接。

問:

如何播放導航,播放視頻流時?它發送給服務器的請求是什麼? 也許我錯過了連接處理的東西?

這對於測試非常基本的版本,它從本地nginx的流視頻,(本地視頻網址 - http://localhost/31285611):

package main 

import (
    "net/http" 
) 

func main() { 
    (&proxy{}).start() 
} 

type proxy struct { 
    // ... 
} 

func (p *proxy) start() { 
    http.HandleFunc("/play", p.connection) 
    http.ListenAndServe("localhost:8040", nil) 
} 

func (p *proxy) connection(w http.ResponseWriter, r *http.Request) { 
    disconnect := make(chan bool, 1) 
    go p.send(w, r, disconnect) 

    // ... 

    <-disconnect 
} 


func (p *proxy) send(rv http.ResponseWriter, rvq *http.Request, disconnect chan bool) { 

    rq, _ := http.NewRequest("GET", "http://localhost/31285611", rvq.Body) 
    rq.Header = rvq.Header 

    rs, _ := http.DefaultClient.Do(rq) 
    for k, v := range rs.Header { 
     rv.Header().Set(k, v[0]) 
    } 
    rv.WriteHeader(http.StatusOK) 

    buf := make([]byte, 1024) 

    // for testing sending only first part. 
    for i := 0; i < 100000; i++ { 
     n, e := rs.Body.Read(buf[0:]) 
     if n == 0 || e != nil { 
      break 
     } 
     rv.Write(buf[0:]) 
    } 

    disconnect <- true 

} 

更新(頭轉儲):

第一個球員的連接:從nginx的,

map[User-Agent:[VLC/2.0.0 LibVLC/2.0.0] Range:[bytes=0-] Connection:[close] Icy-Metadata:[1]] 

響應創建中去連接時:

map[Server:[nginx/1.3.4] Date:[Tue, 23 Apr 2013 13:29:00 GMT] Content-Type:[application/octet-stream] Content-Length:[8147855699] Last-Modified:[Tue, 21 Aug 2012 20:47:20 GMT] Etag:["5033f3d8-1e5a66953"] Content-Range:[bytes 0-8147855698/8147855699]] 
+0

爲什麼不轉儲標題並觀看播放器在做什麼? – Philipp 2013-04-23 14:58:33

+0

我做了,只是從代碼中移除了和平(現在會發布標題轉儲),正如我寫的 - 在播放器中導航時沒有新的連接 – user1579228 2013-04-23 15:31:26

回答

0

我知道這是不是真的回答你的問題(我沒有足夠的積分,評論還,所以對不起提供這個作爲一個答案!),但你嘗試過使用Go的內置http.ReverseProxy(http://golang.org/pkg/net/http/httputil/#ReverseProxy )?

似乎是一個不錯的,簡單的在這裏例如https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/1ufEw_IEVM4我已經非常略低於修改:

package main 

import "http" 
import "log" 

func main(){ 

     proxy := http.NewSingleHostReverseProxy(&http.URL{Scheme:"http",Host:"http://localhost/31285611",Path:"/"}) 


     err := http.ListenAndServe(":8040", proxy) 
     if err != nil { 
       log.Fatal("ListenAndServe: ", err.String()) 
     } 

} 

看看是否能完成這項工作。

此外,在之前關聯的Google網上論壇討論中,提到了NginX存在分塊編碼方面的問題。這可能值得檢查,如果這是相關的。