Skip to content

Commit 1f06fd4

Browse files
committed
New example - custom_web_server
1 parent a00fe23 commit 1f06fd4

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// To run this script:
2+
// deno run --allow-read --allow-write --allow-run --allow-net --allow-ffi custom_web_server.ts
3+
4+
// To import from local (Debugging and Development)
5+
import { WebUI } from "../../mod.ts";
6+
7+
// To import from online `https://deno.land` (Production)
8+
// import { WebUI } from "https://deno.land/x/[email protected]/mod.ts";
9+
10+
async function allEvents(e: WebUI.Event) {
11+
/*
12+
e.window: WebUI;
13+
e.eventType: WebUI.EventType;
14+
e.element: string;
15+
*/
16+
console.log(`\nallEvents: window = '${e.window}'`);
17+
console.log(`allEvents: eventType = '${e.eventType}'`);
18+
console.log(`allEvents: element = '${e.element}'`);
19+
switch (e.eventType) {
20+
case WebUI.EventType.Disconnected:
21+
// Window disconnection event
22+
console.log(`Window closed.`);
23+
break;
24+
case WebUI.EventType.Connected:
25+
// Window connection event
26+
console.log(`Window connected.`);
27+
break;
28+
case WebUI.EventType.MouseClick:
29+
// Mouse click event
30+
console.log(`Mouse click.`);
31+
break;
32+
case WebUI.EventType.Navigation:
33+
// Window navigation event
34+
const url = e.arg.string(0);
35+
console.log(`Navigation to '${url}'`);
36+
// Because we used `webui_bind(MyWindow, "", events);`
37+
// WebUI will block all `href` link clicks and sent here instead.
38+
// We can then control the behaviour of links as needed.
39+
e.window.navigate(url);
40+
break;
41+
case WebUI.EventType.Callback:
42+
// Function call event
43+
console.log(`Function call.`);
44+
break;
45+
}
46+
}
47+
48+
async function myBackendFunc(e: WebUI.Event) {
49+
const a = e.arg.number(0); // First argument
50+
const b = e.arg.number(1); // Second argument
51+
const c = e.arg.number(2); // Third argument
52+
console.log(`\nFirst argument: ${a}`);
53+
console.log(`Second argument: ${b}`);
54+
console.log(`Third argument: ${c}`);
55+
}
56+
57+
// Create new window
58+
const myWindow = new WebUI();
59+
60+
// Bind All Events
61+
myWindow.bind("", allEvents);
62+
63+
// Bind Backend Function
64+
myWindow.bind("myBackendFunc", myBackendFunc);
65+
66+
// Bind Exit Function
67+
myWindow.bind("exit", () => {
68+
// Close all windows and exit
69+
WebUI.exit();
70+
});
71+
72+
// Set the web-server/WebSocket port that WebUI should
73+
// use. This means `webui.js` will be available at:
74+
// http://localhost:MY_PORT_NUMBER/webui.js
75+
myWindow.setPort(8081);
76+
77+
// Show a new window and point to our custom web server
78+
// Assuming the custom web server is running on port
79+
// 8080...
80+
myWindow.show("http://localhost:8080/");
81+
82+
// Wait until all windows get closed
83+
await WebUI.wait();
84+
85+
console.log("Thank you.");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>WebUI - Custom Web-Server Example (C)</title>
6+
<!-- Connect this window to the WebUI back-end app -->
7+
<script src="http://localhost:8081/webui.js"></script>
8+
</head>
9+
<body>
10+
<h3>Custom Web-Server Example (C)</h3>
11+
<p>
12+
This HTML page is handled by a custom Web-Server other than WebUI.<br />
13+
This window is connected to the back-end because we used: <pre>http://localhost:8081/webui.js</pre>
14+
</p>
15+
<h4><a href="second.html">Simple link example (Local file)</a></h4>
16+
<button onclick="myBackendFunc(123, 456, 789);">Call myBackendFunc()</button>
17+
</body>
18+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>WebUI - Custom Web-Server second page (C)</title>
6+
<!-- Connect this window to the back-end app -->
7+
<script src="http://localhost:8081/webui.js"></script>
8+
</head>
9+
<body>
10+
<h3>This is the second page !</h3>
11+
<h4><a href="index.html">Back</a></h4>
12+
</body>
13+
</html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import http.server
2+
import socketserver
3+
4+
PORT = 8080
5+
6+
Handler = http.server.SimpleHTTPRequestHandler
7+
8+
with socketserver.TCPServer(("", PORT), Handler) as httpd:
9+
print(f"Server started at http://localhost:{PORT}")
10+
httpd.serve_forever()

0 commit comments

Comments
 (0)