简体   繁体   English

如何通过Yaws流式传输内容?

[英]How do I stream content with Yaws?

I read about Yaws: streaming data to the client . 我读到有关Yaws的内容:将数据流传输到客户端 I created a simple example shown below, but it doesn't work. 我创建了一个如下所示的简单示例,但是它不起作用。 I get an error and the process is dying. 我收到一个错误,过程即将结束。

Here is my yaws-file: 这是我的偏航文件:

<erl>
out(A) ->
    Self = self(),  
    spawn(fun() ->
        yaws_api:stream_chunk_deliver_blocking(Self, "Hello"),
        yaws_api:stream_chunk_end(Self, "Thanks"),
        exit(normal)
    end),
    {streamcontent, "text/html; charset=utf-8", "First\r\n"}.
</erl>

I also tried with yaws_api:stream_chunk_deliver/2 but I got the same error. 我也尝试了yaws_api:stream_chunk_deliver/2但是遇到了同样的错误。 Here is the error message I get in the command prompt: 这是我在命令提示符下收到的错误消息:

=ERROR REPORT==== 13-Feb-2012::09:23:30 ===
Error in process <0.117.0> with exit value: {undef,[{yaws_api,stream_chunk_end,[
\n"],[]}]}"Thanks

1>
=ERROR REPORT==== 13-Feb-2012::09:24:00 ===
Yaws process died: {stream_timeout,
                       [{yaws_server,stream_loop_send,5,
                            [{file,"yaws_server.erl"},{line,2821}]},
                        {yaws_server,aloop,3,
                            [{file,"yaws_server.erl"},{line,1167}]},
                        {yaws_server,acceptor0,2,
                            [{file,"yaws_server.erl"},{line,1025}]},
                        {proc_lib,init_p_do_apply,3,
                            [{file,"proc_lib.erl"},{line,227}]}]}
1>

The last chunk doesn't seem to be sent to the client: 最后一块似乎没有发送给客户端:

HTTP/1.1 200 OK
Server: Yaws 1.92
Date: Mon, 13 Feb 2012 07:31:18 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked

7
First

7
Hello

Here is the JavaScript client code I use (only working with IE8 and IE9), using XDomainRequest : 这是我使用的JavaScript客户端代码(仅适用于IE8和IE9)以及XDomainRequest

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script>
window.onload = function() {
    var el = document.getElementById("requestbutton");
    if(el.attachEvent) {
        el.attachEvent('onclick', doXDR);
    }
}

function writeStatus(message) {
    var html = document.createElement("div");
    html.setAttribute("class", "message");
    html.innerHTML = message;
    document.getElementById("status").appendChild(html);
}

function doXDR() {
    if(!window.XDomainRequest) {
        writeStatus("No XDR support in your browser");
        return;
    }

    writeStatus("Beginning XDR");
    try {
        var xdr = new XDomainRequest();
        xdr.open("GET", "http://localhost:8090/stream.yaws");
        xdr.send();
        xdr.onload = function() {
            writeStatus("XDR response: " + xdr.responseText);
        };
        xdr.onerror = function() {
            writeStatus("XDR error");
        };
        xdr.onprogress = function() {
            writeStatus("XDR intermediate: " + xdr.responseText);
        };
    } catch (e) {
        writeStatus("XDR Exception: " + e);
    }
}
</script>
</head>
<body>
<h1>Test page</h1>
<button id="requestbutton">Send request</button><br>
<div id="status"></div>
</body>
</html>

On the JavaScript client, the xdr.onerror = function() method is called. 在JavaScript客户端上,将xdr.onerror = function()方法。 The client should not show any data in this example since it need a 2k prelude , but it should be sent, as how I understand it. 在此示例中,客户端不应显示任何数据,因为它需要2k的前奏 ,但应按照我的理解发送。


Update 更新资料

After fixing the Erlang issues pointed out by Steve Vinoski and removing \\r\\n on my data, the Yaws server send the correct data. 解决了Steve Vinoski指出的Erlang问题并删除了我的数据\\r\\n ,Yaws服务器发送了正确的数据。 But I still get xdr.onerror = function() error on the JavaScript client. 但是我在JavaScript客户端上仍然出现xdr.onerror = function()错误。 And it seems that I need to add another header to the response Access-Control-Allow-Origin: * as documented in XDomainRequest Object : 似乎我需要向响应Access-Control-Allow-Origin: *添加另一个标头Access-Control-Allow-Origin: *XDomainRequest对象中所述

The document will request data from the domain's server by sending an Origin header with the value of the origin. 该文档将通过发送带有标头值的标头来向域的服务器请求数据。 It will only complete the connection if the server responds with an Access-Control-Allow-Origin header of either * or the exact URL of the requesting document . 仅当服务器使用*或请求文档的确切URL的Access-Control-Allow-Origin标头响应时,它才会完成连接。 This behavior is part of the World Wide Web Consortium (W3C)'s Web Application Working Group's draft framework on client-side cross-domain communication that the XDomainRequest object integrates with. 此行为是XDomainRequest对象与之集成的万维网联盟(W3C)的Web应用程序工作组的客户端跨域通信框架的一部分。

How do I add this header to the HTTP response? 如何将此标头添加到HTTP响应中? It looks like I only can set the MIME type in the return value: {streamcontent, MimeType, FirstChunk} ? 看来我只能在返回值中设置MIME类型: {streamcontent, MimeType, FirstChunk}

Anytime you see an undef error, like this one you showed above: 每当您看到undef错误时,就像您在上面显示的那样:

Error in process <0.117.0> with exit value: {undef,[{yaws_api,stream_chunk_end,

it means you're either calling a function in a module that isn't on your load path, or you're calling a function that doesn't exist. 这意味着您正在调用不在加载路径上的模块中的函数,或者正在调用不存在的函数。 In your code, you're calling yaws_api:stream_chunK_end/2 , which doesn't exist. 在您的代码中,您正在调用yaws_api:stream_chunK_end/2 ,该名称不存在。 What you want instead is yaws_api:stream_chunk_end/1 . 您想要的是yaws_api:stream_chunk_end/1 Change your code to the following: 将您的代码更改为以下内容:

<erl>
  out(A) ->
    Self = self(),  
      spawn(fun() ->
        yaws_api:stream_chunk_deliver_blocking(Self, "Hello\r\n"),
        yaws_api:stream_chunk_deliver_blocking(Self, "Thanks\r\n"),
        yaws_api:stream_chunk_end(Self),
        exit(normal)
      end),
    {streamcontent, "text/html; charset=utf-8", "First\r\n"}.
</erl>

and then I think your example will work fine. 然后我认为您的示例会很好。

Update: 更新:

To answer the new part of your question, you use a list return value rather than just a single tuple, making sure the streamcontent tuple is last in the list: 为了回答问题的新部分,您可以使用列表返回值,而不仅仅是一个元组,请确保streamcontent元组在列表中位于最后:

[{header, {"Access-Control-Allow-Origin", "*"}},
 {streamcontent, "text/html; charset=utf-8", "First"}].

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示IE7的特定内容? - How do I display IE7 specific content? 如何在IE和Firefox中使内容CSS属性语法有效? - How do I make the content CSS property syntax work in IE & Firefox? 使用webkit-image-set时,如何为非WebKit浏览器提供备用内容? - How do I present alternate content for non-WebKit browsers when using webkit-image-set? 流式传输ASF文件需要哪些HTTP头? - What HTTP headers do I need to stream an ASF file? 如何阻止Internet Explorer的专有渐变过滤器切断应该溢出的内容? - How do I stop internet explorer's propriety gradient filter from cutting off content that should overflow? 如何在不通过IE中的电子邮件中推送其他内容的情况下获取div? - How do I get a div to overflow without pushing other content down in an email in IE? 您是否只想查看安全交付的网页内容? 安全警告,即如何通过编程来解决 - Do you want to view only the webpage content that was delivered securely? secury waring in ie, how can i fix though programing 如何在IE中用元素自己的内容替换元素? - How do you replace element with its own content in IE? 如何清除gwt + IE中的fileupload小部件的内容? - how can i clear content of fileupload widget in gwt + IE? 如何在Chrome,Firefox和IE中显示MJPEG,H264 Live Stream和播放视频? - How can I show MJPEG, H264 Live Stream, and playback video in Chrome, Firefox and IE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM