autoreconnect

This commit is contained in:
Soph :3 2024-08-03 03:27:09 +03:00
parent 0480c2ddd0
commit e3ef3f76c7
Signed by: sophie
GPG key ID: EDA5D222A0C270F2

View file

@ -3,10 +3,17 @@ import QtQml 2.0
import QtWebSockets 1.1
Script {
//socket.active
property string scriptDirPath
property QtObject timer
property QtObject delay
property QtObject websocket
function init() {
script.startDetachedProcess("/usr/bin/python", [scriptDirPath + "/main.py"], 0, 0, 0, scriptDirPath);
}
websocket: WebSocket {
id: socket
@ -15,18 +22,37 @@ Script {
script.log("Received message: " + message);
}
onStatusChanged: {
if (socket.status == WebSocket.Error)
if (socket.status == WebSocket.Error) {
script.log("Error: " + socket.errorString);
else if (socket.status == WebSocket.Open)
} else if (socket.status == WebSocket.Open) {
script.log("Open!");
else if (socket.status == WebSocket.Closed)
} else if (socket.status == WebSocket.Closed) {
this.active = false;
delay.setTimeout(() => {
socket.active = true;
}, 2000);
script.log("Socket closed");
}
}
active: true
}
function init() {
script.startDetachedProcess("/usr/bin/python", [scriptDirPath + "/main.py"], 0, 0, 0, scriptDirPath);
delay: Timer {
id: timer
function setTimeout(cb, delayTime) {
timer.interval = delayTime;
timer.repeat = false;
timer.triggered.connect(cb);
timer.triggered.connect(function release() {
timer.triggered.disconnect(cb);
timer.triggered.disconnect(release);
});
timer.start();
}
}
timer: Timer {
@ -34,13 +60,14 @@ Script {
running: true
repeat: true
onTriggered: {
script.log(websocket.active);
if (websocket.active) {
var note = script.currentNote();
websocket.sendTextMessage(JSON.stringify({
name: note.name,
cursorPosition: script.noteTextEditCursorPosition(),
}))
"name": note.name,
"cursorPosition": script.noteTextEditCursorPosition()
}));
}
}
}