diff options
-rw-r--r-- | examples/qmlapp/index.html | 6 | ||||
-rw-r--r-- | examples/qmlapp/qmlapp.qml | 2 | ||||
-rw-r--r-- | src/qwebchannel.h | 2 | ||||
-rw-r--r-- | src/qwebchannel.js | 16 |
4 files changed, 12 insertions, 14 deletions
diff --git a/examples/qmlapp/index.html b/examples/qmlapp/index.html index 2fc9715..b6a7aa9 100644 --- a/examples/qmlapp/index.html +++ b/examples/qmlapp/index.html @@ -4,20 +4,20 @@ <script> window.onload = function() { var textArea = document.querySelector("textarea"); - function debug(x) { + function output(x) { textArea.value = textArea.value + x + "\n"; } navigator.webChannel.subscribe( "incoming-call", function(message) { - debug(message); + output(message); } ); navigator.webChannel.execute( {a:"This is a request from HTML"}, function(response) { - debug(response.b); + output(response.b); } ); }; diff --git a/examples/qmlapp/qmlapp.qml b/examples/qmlapp/qmlapp.qml index b6049a6..bdd1b4c 100644 --- a/examples/qmlapp/qmlapp.qml +++ b/examples/qmlapp/qmlapp.qml @@ -9,7 +9,7 @@ Rectangle { id: webChannel onExecute: { - var data = JSON.parse(requestData ); + var data = JSON.parse(requestData); txt.text = data.a; response.send(JSON.stringify({b:'This is a response from QML'})); } diff --git a/src/qwebchannel.h b/src/qwebchannel.h index 59664d3..d32b8c5 100644 --- a/src/qwebchannel.h +++ b/src/qwebchannel.h @@ -97,12 +97,12 @@ public: signals: void baseUrlChanged(const QUrl &); void scriptUrlChanged(const QUrl &); + // To be able to access the object from QML, it has to be an explicit QObject* and not a subclass. void execute(const QString& requestData, QObject* response); void noPortAvailable(); public slots: void broadcast(const QString& id, const QString& data); - void writeResponseData(const QString& responseID, const QString& data); private slots: void onInitialized(); diff --git a/src/qwebchannel.js b/src/qwebchannel.js index 331359a..9981330 100644 --- a/src/qwebchannel.js +++ b/src/qwebchannel.js @@ -49,7 +49,7 @@ function sendRequest(url, onSuccess, onFailure) { var req = new XMLHttpRequest(); req.open("GET", url, true); - req.onreadystatechange = function requestStateChanged() { + req.onreadystatechange = function() { if (req.readyState != 4) return; if (req.status != 200 && req.status != 304) { @@ -63,15 +63,13 @@ function sendRequest(url, onSuccess, onFailure) function poll(url, callback) { + setTimeout(function() { sendRequest(url + "/" + (++uniqueIndex), - function pollSucceeded(object) { + function(object) { poll(url, callback); callback(object); - }, - function pollFailed() { - poll(url, callback); - } - ); + }, function() { poll(url, callback); }); + }, 0); } function init() { @@ -89,12 +87,12 @@ function init() { } navigator.webChannel = { - execute: function webChannelExec(message, onSuccess, onFailure) { + execute: function(message, onSuccess, onFailure) { init(); sendRequest(baseUrl + "/e/"+ JSON.stringify(message), onSuccess, onFailure); }, - subscribe: function webChannelSubscribe(id, callback) { + subscribe: function(id, callback) { init(); poll(baseUrl + "/s/" + id, callback); }, |