example 'clog-demo': use websocket server on android, remove old hack

This commit is contained in:
pls.153 2025-04-19 07:58:53 +02:00
parent 24f0d4a17d
commit eda0672f1e
6 changed files with 50 additions and 89 deletions

View file

@ -1,4 +1,4 @@
var _ws=null; var ws=null;
var adr; var adrc; var adr; var adrc;
var clog={}; var clog={};
var pingerid; var pingerid;
@ -6,7 +6,6 @@ var retryid;
var s = document.location.search; var s = document.location.search;
var tokens; var tokens;
var r = /[?&]?([^=]+)=([^&]*)/g; var r = /[?&]?([^=]+)=([^&]*)/g;
var ios = false;
clog['body']=document.body; clog['body']=document.body;
clog['head']=document.head; clog['head']=document.head;
@ -20,31 +19,18 @@ if (typeof clog_debug == 'undefined') {
clog_debug = false; clog_debug = false;
} }
const ws = { function Pingws() {
// replace 'ws.send()' if (ws.readyState == 1) {
send: function(message) { ws.send ('0');
if (ios) {
_ws.send(message);
} else {
// hack, see QML 'onTitleChanged()'
document.title = message;
document.title = "-"; // non empty
}
}
};
function Ping_ws() {
if (_ws.readyState == 1) {
_ws.send ('0');
} }
} }
function Shutdown_ws(event) { function Shutdownws(event) {
if (_ws != null) { if (ws != null) {
_ws.onerror = null; ws.onerror = null;
_ws.onclose = null; ws.onclose = null;
_ws.close (); ws.close ();
_ws = null; ws = null;
} }
clearInterval (pingerid); clearInterval (pingerid);
if (clog['html_on_close'] != '') { if (clog['html_on_close'] != '') {
@ -52,8 +38,8 @@ function Shutdown_ws(event) {
} }
} }
function Setup_ws() { function Setupws() {
_ws.onmessage = function (event) { ws.onmessage = function (event) {
try { try {
if (clog_debug == true) { if (clog_debug == true) {
console.log ('eval data = ' + event.data); console.log ('eval data = ' + event.data);
@ -67,35 +53,35 @@ function Setup_ws() {
var rc = function (event) { var rc = function (event) {
console.log (event); console.log (event);
clearInterval (retryid); clearInterval (retryid);
_ws = null; ws = null;
_ws = new WebSocket (adr + '?r=' + clog['connection_id']); ws = new WebSocket (adr + '?r=' + clog['connection_id']);
_ws.onopen = function (event) { ws.onopen = function (event) {
console.log ('reconnect successful'); console.log ('reconnect successful');
Setup_ws(); Setupws();
} }
_ws.onclose = function (event) { ws.onclose = function (event) {
console.log ('reconnect failure'); console.log ('reconnect failure');
console.log (Date.now()); console.log (Date.now());
retryid = setInterval(function () {rc("Failed reconnect - trying again")}, 500); retryid = setInterval(function () {rc("Failed reconnect - trying again")}, 500);
} }
} }
_ws.onerror = function (event) { ws.onerror = function (event) {
console.log ('onerror: reconnect'); console.log ('onerror: reconnect');
rc("onerror - trying reconnect") rc("onerror - trying reconnect")
} }
_ws.onclose = function (event) { ws.onclose = function (event) {
if (event.code && event.code === 1000) { if (event.code && event.code === 1000) {
console.log("WebSocket connection got normal close from server. Don't reconnect."); console.log("WebSocket connection got normal close from server. Don't reconnect.");
Shutdown_ws(event); Shutdownws(event);
} else { } else {
rc("onclose - trying reconnnect"); rc("onclose - trying reconnnect");
} }
} }
} }
function Open_ws() { function Openws() {
/* /*
if (location.protocol == 'https:') { if (location.protocol == 'https:') {
adr = 'wss://' + location.hostname; adr = 'wss://' + location.hostname;
@ -107,7 +93,6 @@ function Open_ws() {
adr = adr + '/clog'; adr = adr + '/clog';
*/ */
if (ios) {
adr = 'ws://127.0.0.1:8080'; adr = 'ws://127.0.0.1:8080';
if (clog['connection_id']) { if (clog['connection_id']) {
@ -116,24 +101,23 @@ function Open_ws() {
try { try {
console.log ('connecting to ' + adrc); console.log ('connecting to ' + adrc);
_ws = new WebSocket (adrc); ws = new WebSocket (adrc);
} catch (e) { } catch (e) {
console.log ('trying again, connecting to ' + adrc); console.log ('trying again, connecting to ' + adrc);
_ws = new WebSocket (adrc); ws = new WebSocket (adrc);
} }
if (_ws != null) { if (ws != null) {
_ws.onopen = function (event) { ws.onopen = function (event) {
console.log ('connection successful'); console.log ('connection successful');
Setup_ws(); Setupws();
} }
pingerid = setInterval (function () {Ping_ws ();}, 10000); pingerid = setInterval (function () {Pingws ();}, 10000);
} else { } else {
document.writeln ('If you are seeing this your browser or your connection to the internet is blocking websockets.'); document.writeln ('If you are seeing this your browser or your connection to the internet is blocking websockets.');
} }
}
} }
$( document ).ready(function() { $( document ).ready(function() {
if (_ws == null) { Open_ws(); } if (ws == null) { Openws(); }
}); });

View file

@ -3,8 +3,7 @@
(in-package :clog) (in-package :clog)
(setf clog-connection::*send-to-webview* (setf clog-connection::*send-to-webview*
#+ios (lambda (js) (qml:qjs |send| ui:*server* js)) (lambda (js) (qml:qjs |send| ui:*server* js)))
#-ios (lambda (js) (qml:q! |runJavaScript| ui:*browser* js nil)))
(defun webview/on-new-connection () (defun webview/on-new-connection ()
(clog-connection::handle-new-connection 'qml-webview nil)) (clog-connection::handle-new-connection 'qml-webview nil))

View file

@ -45,11 +45,6 @@
(shell (cc "cp -r ../clog-assets/static-files " *assets*)) (shell (cc "cp -r ../clog-assets/static-files " *assets*))
(shell (cc "cp -r ../clog-assets/*.asd " *assets*))) (shell (cc "cp -r ../clog-assets/*.asd " *assets*)))
#+ios
(with-open-file (s (merge-pathnames "static-files/js/boot.js" *assets*)
:direction :output :if-exists :append)
(write-line "ios = true;" s))
#+mobile #+mobile
(unless (find-swank) (unless (find-swank)
(error "Swank files missing, please see <LQML root>/slime/src/readme-sources.md")) (error "Swank files missing, please see <LQML root>/slime/src/readme-sources.md"))

View file

@ -1,12 +1,10 @@
import QtQuick 2.15 import QtQuick 2.15
import QtQuick.Controls 2.15 import QtQuick.Controls 2.15
import QtWebView 1.15 import QtWebView 1.15
import "." as Ext
Item { Item {
Loader { Ext.Server {}
active: (Qt.platform.os === "ios")
source: "Server.qml"
}
WebView { WebView {
id: browser id: browser
@ -16,22 +14,12 @@ Item {
visible: !busy.visible visible: !busy.visible
onLoadingChanged: { onLoadingChanged: {
if (Qt.platform.os !== "ios") {
if (loadRequest.status === WebView.LoadSucceededStatus) { if (loadRequest.status === WebView.LoadSucceededStatus) {
Lisp.call("clog:webview/on-new-connection") Lisp.call("clog:webview/on-new-connection")
} }
} }
} }
// hack to get notified from the browser, see 'boot.js'
onTitleChanged: {
if ((title !== "-") && (title !== "boot.html")) {
Lisp.call("clog:webview/on-message", title)
main.log(title)
}
}
}
Button { Button {
id: reload id: reload
anchors.bottom: parent.bottom anchors.bottom: parent.bottom

View file

@ -2,8 +2,6 @@ import QtQuick 2.15
import QtWebSockets 1.15 import QtWebSockets 1.15
import "." as Ext import "." as Ext
// for iOS only
Item { Item {
WebSocketServer { WebSocketServer {
id: server id: server

View file

@ -7,7 +7,7 @@ Try it
You can download an APK (android devices) of this example from DropBox: You can download an APK (android devices) of this example from DropBox:
[CLOG demo](https://www.dropbox.com/s/h5wy57niq4g12ec/CLOG-demo.apk?dl=0). [CLOG demo](https://www.dropbox.com/s/h5wy57niq4g12ec/CLOG-demo.apk?dl=0).
**Please note**: startup time on mobile is now greatly improved, by simply **Please note**: startup time on mobile is greatly improved here, by simply
skipping time consuming crypto initialization, not needed for a local skipping time consuming crypto initialization, not needed for a local
connection, like in this example. connection, like in this example.
@ -41,14 +41,11 @@ also needed on the desktop, if used with LQML).
Info Info
---- ----
This shows how to run a CLOG app locally on mobile. It uses two different This shows how to run a CLOG app locally on mobile. It uses a simple local
approaches, depending on the OS: **websocket-server**.
1. direct calls to the browser to run JS, and a small hack to call back to CLOG For android, please see [AndroidManifest.xml](platforms/android/AndroidManifest.xml)
on browser events for the `@xml/network_security_config` file needed to allow a local connection.
2. a simple local websocket-server; this is needed on iOS, where the above
approach doesn't work
The webview is the native one of the mobile device, which has some The webview is the native one of the mobile device, which has some
restrictions: it can't overlap with QML items, and things like swiping don't restrictions: it can't overlap with QML items, and things like swiping don't