summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/qmlapp/qmlapp.qml6
-rw-r--r--src/qwebchannel.cpp16
-rw-r--r--src/qwebchannel.js14
3 files changed, 18 insertions, 18 deletions
diff --git a/examples/qmlapp/qmlapp.qml b/examples/qmlapp/qmlapp.qml
index c8d6cae..06f9cdc 100644
--- a/examples/qmlapp/qmlapp.qml
+++ b/examples/qmlapp/qmlapp.qml
@@ -10,13 +10,15 @@ Rectangle {
useSecret: false
onRequest: {
var data = JSON.parse(request);
+ txt.text = data.a;
response.send(JSON.stringify({b:'This is a response from QML'}));
}
}
WebView {
id: webView
- anchors.top: parent.top
+ anchors.top: txt.bottom
+ height: 200
settings.localContentCanAccessRemoteUrls: true
settings.developerExtrasEnabled: true
url: "index.html?baseUrl=" + webChannel.baseUrl
@@ -24,6 +26,6 @@ Rectangle {
Text {
id: txt
- anchors.top: webView.bottom
+ anchors.top: parent.top
}
}
diff --git a/src/qwebchannel.cpp b/src/qwebchannel.cpp
index baae149..7159879 100644
--- a/src/qwebchannel.cpp
+++ b/src/qwebchannel.cpp
@@ -55,7 +55,7 @@ class QWebChannelResponder : public QObject {
Q_OBJECT
public:
- QWebChannelResponder(QTcpSocket* s, int id)
+ QWebChannelResponder(QTcpSocket* s, const QString& id)
: QObject(0)
, socket(s)
, uid(id)
@@ -72,14 +72,13 @@ public slots:
"Content-Type: text/javascript\r\n"
"socket: Close\r\n"
"\r\n"
- "navigator.webChannel.callback(%1,").arg(uid).toUtf8());
+ "navigator.webChannel.callback('%1',").arg(uid).toUtf8());
}
void write(const QString& data)
{
if (!socket || !socket->isOpen())
return;
- qWarning() << __func__;
socket->write(data.toUtf8());
}
@@ -101,7 +100,7 @@ public slots:
private:
QPointer<QTcpSocket> socket;
- int uid;
+ QString uid;
};
class QWebChannelPrivate : public QObject {
@@ -129,7 +128,6 @@ public:
, secret("42")
, starting(false)
{
- server->moveToThread(thread);
thread->start();
connect(server, SIGNAL(newConnection()), this, SLOT(service()));
}
@@ -157,7 +155,6 @@ void QWebChannelPrivate::service()
{
if (!server->hasPendingConnections())
return;
- qWarning() << __func__;
QTcpSocket* socket = server->nextPendingConnection();
socket->waitForReadyRead();
QString firstLine = socket->readLine();
@@ -172,7 +169,6 @@ void QWebChannelPrivate::service()
path = path.left(indexOfQM);
int indexOfHash = query.indexOf('#');
if (indexOfHash > 0) {
- qWarning() << indexOfHash << query;
hash = query.mid(indexOfHash + 1);
query = query.left(indexOfHash);
}
@@ -215,10 +211,8 @@ void QWebChannelPrivate::service()
}
if (method == "GET") {
- int id = pathElements[1].toInt();
QString message = QStringList(pathElements.mid(2)).join("/");
- qWarning() << QUrl::fromPercentEncoding(message.toUtf8());
- QWebChannelResponder* responder = new QWebChannelResponder(socket, id);
+ QWebChannelResponder* responder = new QWebChannelResponder(socket, pathElements[1]);
responder->moveToThread(thread);
emit request(QUrl::fromPercentEncoding(message.toUtf8()), responder);
}
@@ -248,7 +242,6 @@ void QWebChannelPrivate::init()
}
baseUrl = QString("http://localhost:%1/%2").arg(port).arg(secret);
- qWarning() << baseUrl;
emit initialized();
}
@@ -324,7 +317,6 @@ void QWebChannel::setMaxPort(int p)
void QWebChannel::onInitialized()
{
- qWarning() << __func__;
emit baseUrlChanged(d->baseUrl);
}
diff --git a/src/qwebchannel.js b/src/qwebchannel.js
index 450ee2e..0c3714e 100644
--- a/src/qwebchannel.js
+++ b/src/qwebchannel.js
@@ -41,7 +41,7 @@
navigator.webChannel = {
requests: {},
- id: 1000,
+ id: "",
init: function() {
this.queryVariables = {};
var search = location.search.substr(1).split("&");
@@ -52,10 +52,16 @@ navigator.webChannel = {
},
exec: function(message, onSuccess, onFailure) {
var element = document.createElement("script");
- var id = (this.id++);
+ function S4() {
+ return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
+ }
+ function guid() {
+ return (S4()+S4()+"."+S4()+"."+S4()+"."+S4()+"."+S4()+S4()+S4());
+ }
+
+ var id = guid();
element.async = true;
- element.type = "text/javascript";
- element.src = this.queryVariables.baseUrl + "/"+ id + "/ " + JSON.stringify(message);
+ element.src = this.queryVariables.baseUrl + "/"+ id + "/" + JSON.stringify(message);
document.body.innerHTML = element.src;
this.requests[id] = { element: element, onSuccess: onSuccess, onFailure: onFailure };
document.head.appendChild(element);