aardio 文档

aardio 范例:event-stream

//event-stream
import win.ui;
/*DSG{{*/
var winform = win.form(text="web.rest - event-stream";right=753;bottom=434)
winform.add(
edit={cls="edit";left=20;top=12;right=734;bottom=404;edge=1;multiline=1;z=1}
)
/*}}*/

import wsock.tcp.simpleHttpServer;  
var url = wsock.tcp.simpleHttpServer.startUrl(function(response,request,session){

    while (true) {   
        response.eventStream(
            event = "ping";
            data = { time = time() };
        ) 

        sleep(1000);
    } 

    /*
    //推送 ndjson 流
    response.contentType = "application/x-ndjson";
    while (true) {    
        response.write( { time = time() },'\n' )
        sleep(1000);
    } 
    */
})

thread.invoke( 
    function(url,winform){ 
        import web.rest.jsonLiteClient;
        var http = web.rest.jsonLiteClient();

        var eventSource = http.api(url) 

        //参数 @2 或 参数@3 指定接收数据回调函数则自动支持 SSE,兼容 ndjson 流。
        eventSource.get( , function(message){   
            //注意这里的 message 已经由 JSON 解析为单个对象
            winform.edit.print("HTTP 服务端推送了事件",type(message))
            winform.edit.print(message);
        } ) 

    },url,winform
)

winform.show();
win.loopMessage();

Markdown 格式