2013-04-23 19 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存在分块编码方面的问题。这可能值得检查,如果这是相关的。