There is an XMLHttpRequest to a server located at the address localhost: port ( http://127.0.0.1:port/ )
If the get request is successful (correct), the server returns a response in the form:
{ "meta": { "code": <code> }, ... } If there is no such command on the server, code = 404.
If successful, code = 200.
- Accessing the server when the Internet is connected is excellent.
- Accessing the server when the Internet is turned off is also excellent (the application was launched after the Internet was turned off).
- If at the start of the application the Internet was present and all requests to the server were excellent, then after the Internet was broken (and physically - the network cable was disconnected, and programmatically - the network interface was disconnected), the requests to the local server ceased.
Code example:
import QtQuick 2.4 import QtQuick.Window 2.2 Window { id: root visible: true property int it: 0 Timer { id: timer interval: 1000 onTriggered: it++; repeat: true } onItChanged: { if(it>=5) { sendCommand() it = 0 } } function sendCommand() { var request = new XMLHttpRequest(); timer.running = false; request.onreadystatechange = function() { timer.running = true; if (request.readyState === XMLHttpRequest.HEADERS_RECEIVED) { console.debug('HEADERS_RECEIVED'); } else if(request.readyState === XMLHttpRequest.DONE) { var object = JSON.parse(request.responseText.toString()); console.debug('DONE'); console.debug(JSON.stringify(object, null, 2)); } } request.open('GET', 'http://localhost:13112/GetCloseTime', false); request.send(null); } Component.onCompleted: timer.start(); } Conclusion in the console to the Internet break:
qml: HEADERS_RECEIVED qml: DONE qml: { "meta": { "code": 200 }, "task": "None", "time": { "hours": 20, "minutes": 0 } } After the break:
qrc:/main.qml:34: JSON.parse: Parse error Has anyone encountered such a problem or is there a way to get around it?