-- https://cesbo.com/
local server_addr = "127.0.0.1"
local server_port = 8000
local index_html =
[[<!doctype html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<style>
body {
font-family: monospace;
}
hr {
background-color: #0074c8;
height: 1px;
border: 0px;
}
#log {
border: 1px solid #0074c8;
font-family: monospace;
}
.buttons>input {
padding-right: 10px;
}
</style>
</head>
<body>
<h1>Astra WebSocket test</h1>
<hr>
<div class="buttons">
<input type="button" value="Get Data" id="get_data" />
</div>
<textarea id="log" rows="24" cols="80"></textarea>
</body>
<script>
$log = $('#log');
$log.append("Astra WebSocket test is started...\n");
ws = new WebSocket("ws://" + location.host + "/api");
ws.onopen = function(evt) { $log.append("Connection established\n"); };
ws.onmessage = function(evt) { $log.append("Received:\n" + evt.data + "\n"); };
ws.onclose = function(evt) { $log.append("Connection closed\n"); };
ws.onerror = function(evt) {
$log.append("Error. See console for more information\n")
console.log("WebSocket Error: ", evt);
};
$('#get_data').click(function(obj) { ws.send("GET DATA"); });
</script>
</html>
]]
function http_callback_root(server, client, request)
if not request then return nil end
server:send(client, {
code = 200,
headers =
{
"Content-Type: text/html",
"Connection: close",
},
content = index_html,
})
end
function http_callback_api(server, client, request)
if request == "GET DATA" then
local f = io.open("COPYING")
local data = f:read("*all")
f:close()
data = data:gsub("&", "&")
data = data:gsub("<", "<")
data = data:gsub(">", ">")
server:send(client, data)
end
end
http_server({
addr = server_addr,
port = server_port,
name = "WebSocket test",
route = {
{ "/", http_callback_root },
{ "/api", http_websocket({ callback = http_callback_api }) },
}
})
Comments