diff --git a/main.qml b/main.qml index 203cbd5..3c273df 100644 --- a/main.qml +++ b/main.qml @@ -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: { - var note = script.currentNote(); - - websocket.sendTextMessage(JSON.stringify({ - name: note.name, - cursorPosition: script.noteTextEditCursorPosition(), - - })) + script.log(websocket.active); + if (websocket.active) { + var note = script.currentNote(); + websocket.sendTextMessage(JSON.stringify({ + "name": note.name, + "cursorPosition": script.noteTextEditCursorPosition() + })); + } } }