<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Main</title>
<style> body { margin: 0; }</style>
</head>
<body>
<div id="elm"></div>
<script>{{elm-main}}</script>
<script>
function getTextWidth (t, f) {
var blah = getTextMetrics(t, f);
return blah.width;
};
function getTextMetrics (text, font) {
var canvas = getTextMetrics.canvas || (getTextMetrics.canvas = document.createElement("canvas"));
var context = canvas.getContext("2d");
context.font = font;
var metrics = context.measureText(text);
return metrics;
};
function requestTextSize (rts) {
metrics = getTextMetrics(rts.string, rts.font);
var reply = { width : metrics.width, controlId : rts.controlId };
app.ports.receiveTextMetrics.send(reply);
}
</script>
<script>
var mySockets = {};
function sendSocketCommand(wat) {
if (wat.cmd == "connect")
{
socket = new WebSocket(wat.address, wat.protocol);
socket.onmessage = function (event) {
app.ports.receiveSocketMsg.send({ name : wat.name
, msg : "data"
, data : event.data} );
}
mySockets[wat.name] = socket;
}
else if (wat.cmd == "send")
{
mySockets[wat.name].send(wat.content);
}
else if (wat.cmd == "close")
{
mySockets[wat.name].close();
delete mySockets[wat.name];
}
}
</script>
<script>
var app = Elm.Main.init(
{ flags: { location : document.location.origin || "",
wsport : {{websockets-port}},
width : window.innerWidth,
height : window.innerHeight
},
node: document.getElementById("elm")
});
if (document.getElementById("elm"))
{
document.getElementById("elm").innerText = 'This is a headless program, meaning there is nothing to show here.\n\nI started the program anyway though, and you can access it as `app` in the developer console.';
}
app.ports.sendSocketCommand.subscribe(sendSocketCommand);
app.ports.requestTextSize.subscribe(requestTextSize);
</script>
</body>
</html>