实时视频或音频流如何工作?

How live video or audio streams work?

我对生成 html 页面的服务器进行编码,以便用户可以在浏览器中查看这些页面。

它有 onGetRequest 事件,这是它的处理程序:

            var req = e.Request;
            var res = e.Response;

            var path = req.RawUrl.Replace("%20", " ");
            if (path == "/")
                path += "index.html";

            if (path.Contains("/../"))
            {
                res.StatusCode = (int)HttpStatusCode.Forbidden;
                return;
            }

            var content = this.ServerToRun.GetFile(path); //getting file to read
            if (content == null)
            {
                res.StatusCode = (int)HttpStatusCode.NotFound;
                return;
            }

            string extension = path.Substring(path.LastIndexOf('.'));

            string auto_mime = PageControls.MimeTypeDeterminer.GetMimeTypeFor(extension);

            if (string.IsNullOrEmpty(auto_mime))
            {
                if (extension.Length > 1)
                    res.ContentType = "application/" + extension.Substring(1);
                else
                    res.ContentType = "application/unknown";
            }
            else
                res.ContentType = auto_mime;

            if (path.EndsWith(".html") || path.EndsWith(".htm"))
                res.ContentEncoding = Encoding.UTF8;

            res.WriteContent(content); //sending content to client

我不明白支持直播需要做什么。 例如,我可以从麦克风录制音频,所以文件会每秒增加它的大小。

我可以在 html 代码中做到这一点:

<audio>
    <source src = "live.wav" type = "audio/wav" />
</audio>

服务器将接收此文件的查询,读取它直到最后并将其发送给客户端,但在此之后 live.wav 将获得服务器不再发送给客户端的更多声音块。

所以,我被困住了,直播是如何运作的,我需要做什么?

我已经为每个客户端打开了 WebSocket,所以我可以调用一些脚本。

您应该使用 Transfer-Encoding: Chunked HTTP header。这 header 允许您在不需要指定 Content-Length 的情况下以块的形式发送数据,因此在服务器指示最后一个块已发送之前,客户端不会关闭套接字。参见 https://en.wikipedia.org/wiki/Chunked_transfer_encoding