Repository navigation
Expand file tree
/
Copy pathmain.d
More file actions
67 lines (50 loc) · 1.93 KB
/
Copy pathmain.d
File metadata and controls
67 lines (50 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// D-WebUI - Public Network Access Example
import std.format : format;
import webui;
// Embed both UIs into the binary at compile time.
enum privateHtml = import("private.html");
enum publicHtml = import("public.html");
Window privateWindow;
Window publicWindow;
void main()
{
privateWindow = newWindow();
publicWindow = newWindow();
// Wait forever, never time out
setTimeout(0);
// -- Public window -------------------------------------------------------
// Make the URL reachable from public networks
publicWindow.setPublic(true);
// An empty element name binds every event of the window
publicWindow.bind("", &publicWindowEvents);
// Use a fixed port so the public URL is predictable
publicWindow.setPort(9090);
// Start the server without opening a browser window
publicWindow.showBrowser(publicHtml, Browser.noBrowser);
// -- Private window ------------------------------------------------------
privateWindow.bind("", &privateWindowEvents);
privateWindow.bind("Exit", () { exit(); });
privateWindow.show(privateHtml);
// Wait until all windows get closed
wait();
// Free all memory resources (optional)
clean();
}
void publicWindowEvents(Event* e)
{
if (e.eventType == EventType.connected)
privateWindow.run(`document.getElementById("Logs").value += "New connection.\n";`);
else if (e.eventType == EventType.disconnected)
privateWindow.run(`document.getElementById("Logs").value += "Disconnected.\n";`);
}
void privateWindowEvents(Event* e)
{
if (e.eventType != EventType.connected)
return;
// Show the public window address in the private window
privateWindow.run(format(
"document.getElementById('urlSpan1').innerHTML = 'http://localhost:%d';",
publicWindow.getPort()));
privateWindow.run(format(
"document.getElementById('urlSpan2').innerHTML = '%s';", publicWindow.getUrl()));
}