Created
April 25, 2019 13:47
-
-
Save nicopace/149d64ff8dbf9441b6eb4e06bdc46f0d to your computer and use it in GitHub Desktop.
Server-sent events for openwrt on pure lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<!-- this one goes in /www/ubuslisten.html --> | |
<meta charset="UTF-8"> | |
<body></body> | |
<script> | |
var source = new EventSource("/cgi-bin/ubuslisten.lua"); | |
source.addEventListener('message', function(e) { | |
console.log(e.data); | |
}, false); | |
</script> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/lua | |
-- this one goes in /www/cgi-bin/ubuslisten.lua | |
io.stdout:write("Content-Type: text/event-stream\n\n") | |
io.stdout:flush() | |
ubuslisten = io.popen("ubus listen") | |
for line in ubuslisten:lines() do | |
io.stdout:flush() | |
io.stdout:write("data: ") | |
io.stdout:write(line) | |
io.stdout:write("\n\n") | |
io.stdout:flush() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment