Go 为什么通过http.ServeContent阅读我的视频的客户端会一直关闭连接?

Go 为什么通过http.ServeContent阅读我的视频的客户端会一直关闭连接?,go,video-streaming,Go,Video Streaming,我目前正在做一个小项目,通过http.ServeContent为浏览器或其他媒体客户端提供视频服务。我已经实现了自己的ReadSeek,如下所示: //the seek is not fully working yet but works fine for the initial two calls that is being called internally from http to decide the file size. func (c *Client) Seek(offset in

我目前正在做一个小项目,通过http.ServeContent为浏览器或其他媒体客户端提供视频服务。我已经实现了自己的ReadSeek,如下所示:

//the seek is not fully working yet but works fine for the initial two calls that is being called internally from http to decide the file size.

func (c *Client) Seek(offset int64, whence int) (t int64, e error) {
    switch whence {
    case 0:
        t = offset
    case 1:
        t = c.seek + offset
    case 2:
        t = c.SelectedFile().Length + offset
    }

    if c.seek != t {
        //TODO
        c.seek = t
    }

    return
}

func (c *Client) Read(p []byte) (n int, err error) {
    if c.done {
        return 0, io.EOF
    }

    for {
        result := <-c.Results

        if result.piece == nil {
            return 0, io.EOF
        }

        for i, b := range result.bytes {
            index := int64(i)
            if index < 0 || index > c.SelectedFile().Length {
                continue
            }

            n++
            p[index] = b
        }

        return
    }
}
我已经试着调试了好几天,但似乎无法找出原因。我已经将我提供的字节与实际文件进行了比较,数据似乎没有任何问题


你知道哪里不对吗?

没什么不对的。在服务器发送所有数据之前,没有任何东西要求客户端不断开连接。客户端执行标准http请求,然后在收到头后取消,这是非常常见的,尤其是对于mp4文件


但是我怀疑视频没有按照你期望的方式播放?如果是这种情况,请确保服务器实现http范围请求

哦,视频根本没有在任何浏览器上播放,很抱歉没有提到这一点。播放机加载,但在播放任何内容之前连接已断开。此外,断开连接发生在几秒钟之后,我以前从未在流式传输文件时经历过类似的行为,因此我非常确定我的实现或提供数据的方式有问题。根据文档,在使用http.ServeContent和ReadSeek时,应该正确处理范围请求。
此外,断开连接发生在几秒钟后,我以前从未在流式传输文件时遇到过类似的行为。
这很奇怪,因为它非常常见,尤其是在苹果客户机上。唯一的选择是你正在以某种方式破坏数据。你说它很常见是什么意思?所以,通常任何通过http观看视频的尝试都会失败?可以选择添加视频流的VLC肯定不会像我的浏览器那样运行吗?在大约相同数量的块之后,我得到了完全相同的错误。也许我应该尝试切换到另一个操作系统,看看问题是否仍然存在,但我确实认为其他操作系统有问题。不,在接收到最后一个字节之前,客户端取消HTTP请求(通过关闭TCP会话)是很常见的。如果使用HTTP连接,则必须以base 7格式发送数据。然后客户端必须将其转换回base 8。
...
func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) {
    ...
    for {
        nr, er := src.Read(buf)
        if nr > 0 {
            nw, ew := dst.Write(buf[0:nr])
            if nw > 0 {
                written += int64(nw)
            }
            if ew != nil {
                err = ew
                break
            }
            if nr != nw {
                err = ErrShortWrite
                break
            }
        }
        if er != nil {
            if er != EOF {
                err = er
            }
            break
        }
    }
    return written, err
}