aardio 文档
aardio 范例: event-stream
//event-stream
import win.ui;
/*DSG{{*/
var winform = win.form(text="web.view - event-stream";right=753;bottom=434)
winform.add()
/*}}*/
import web.view;
var wb = web.view(winform);
//启动多线程 HTTP 服务器
import wsock.tcp.simpleHttpServer;
wb.eventSourceUrl = wsock.tcp.simpleHttpServer.startUrl(
function(response,request,session){
response.headers["Access-Control-Allow-Origin"] = "*"
while (true) {
response.eventStream(
event = "ping";
data = { time = time() };
)
sleep(1000);
}
}
)
//HTML 支持 aardio 模板语法(可嵌入 aardio 代码,owner 参数为 wb 对象)
wb.html = /**
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
html,body{ height:100%; margin:50; }
</style>
<script src="https://lib.baomitu.com/dayjs/1.11.10/dayjs.min.js"></script>
<script>
const evtSource = new EventSource("<?=owner.eventSourceUrl?>");
evtSource.onerror = function(err) {
console.error("EventSource failed:", err);
};
evtSource.addEventListener("ping", function(event) {
const data = JSON.parse(event.data);
document.body.insertAdjacentHTML("beforeEnd","<div>" + "ping at " + dayjs(data.time).format("YYYY-MM-DD HH:mm:ss") + "</div>");
});
evtSource.onmessage = (event) => {};
</script>
</head>
<body></body>
</html>
**/
winform.show();
win.loopMessage();
Markdown 格式