问题
I am trying to build an application which would act as a streaming proxy server with caching features. The thing is, I want to do it manually without using NewSingleHostReverseProxy
. Manually means performing these steps:
- Perform single
GET
request to the server - Read
resp.Body
to buffer and write to connected client(s)
And the issue is that VLC doesn't play anything. If I access stream directly - VLC plays it without problems, but if I do it via GO - VLC (as well as Kodi) just keeps buffering and never starts playing.
Things I've tried, but did not work:
io.Copy(...)
bufio
reader/scanner and writing to the connected client. Flushing also did not help.
This is what curl -v <stream_url>
says (accessing streaming url directly):
...
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 Ok
< Server: Myserver
< Cache-Control: no-cache
< Pragma: no-cache
< Content-Type: application/octet-stream
< Access-Control-Allow-Origin: *
< Connection: close
<
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.
* Failed writing body (0 != 2896)
* Closing connection 0
This is what curl -v <my_app_url>
says:
...
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Access-Control-Allow-Origin: *
< Cache-Control: no-cache
< Content-Type: application/octet-stream
< Pragma: no-cache
< Server: Myserver
< Date: Sun, 29 Dec 2019 09:13:44 GMT
< Transfer-Encoding: chunked
<
Warning: Binary output can mess up your terminal. Use "--output -" to tell
Warning: curl to output it to your terminal anyway, or consider "--output
Warning: <FILE>" to save to a file.
* Failed writing body (0 != 14480)
* Failed reading the chunked-encoded stream
* Closing connection 0
Seems the issue is here, but how do I solve it?
Good:
* Failed writing body (0 != 2896)
* Closing connection 0
Bad:
* Failed writing body (0 != 14480)
* Failed reading the chunked-encoded stream
* Closing connection 0
Also adding a source code for example:
func main() {
http.HandleFunc("/stream", func(w http.ResponseWriter, r *http.Request) {
resp, err := http.Get("http://example.com/stream/videostream")
if err != nil {
panic(err)
}
defer resp.Body.Close()
reader := bufio.NewReader(resp.Body)
buf := make([]byte, 1024)
for {
k, _ := reader.Read(buf)
if k == 0 {
break
}
w.Write(buf)
}
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
来源:https://stackoverflow.com/questions/59518175/how-do-i-manually-proxy-application-octet-stream-live-video-in-go