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 import QtWebSockets 1.1
Script { Script {
//socket.active
property string scriptDirPath property string scriptDirPath
property QtObject timer property QtObject timer
property QtObject delay
property QtObject websocket property QtObject websocket
function init() {
script.startDetachedProcess("/usr/bin/python", [scriptDirPath + "/main.py"], 0, 0, 0, scriptDirPath);
}
websocket: WebSocket { websocket: WebSocket {
id: socket id: socket
@ -15,18 +22,37 @@ Script {
script.log("Received message: " + message); script.log("Received message: " + message);
} }
onStatusChanged: { onStatusChanged: {
if (socket.status == WebSocket.Error) if (socket.status == WebSocket.Error) {
script.log("Error: " + socket.errorString); script.log("Error: " + socket.errorString);
else if (socket.status == WebSocket.Open) } else if (socket.status == WebSocket.Open) {
script.log("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"); script.log("Socket closed");
} }
}
active: true active: true
} }
function init() { delay: Timer {
script.startDetachedProcess("/usr/bin/python", [scriptDirPath + "/main.py"], 0, 0, 0, scriptDirPath); 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 { timer: Timer {
@ -34,13 +60,14 @@ Script {
running: true running: true
repeat: true repeat: true
onTriggered: { onTriggered: {
script.log(websocket.active);
if (websocket.active) {
var note = script.currentNote(); var note = script.currentNote();
websocket.sendTextMessage(JSON.stringify({ websocket.sendTextMessage(JSON.stringify({
name: note.name, "name": note.name,
cursorPosition: script.noteTextEditCursorPosition(), "cursorPosition": script.noteTextEditCursorPosition()
}));
})) }
} }
} }