diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2014-07-16 14:33:03 +0200 |
---|---|---|
committer | Milian Wolff <milian.wolff@kdab.com> | 2014-07-30 01:31:54 +0200 |
commit | 5051411b92b4aca4ed5ec462e7b0e52af4d3951e (patch) | |
tree | 9f9ed9c1940db04ccf1a076b7e3471e7d1f73737 | |
parent | 94912cf26ba70a2cadd23f1c931395be64c11eac (diff) | |
download | qtwebchannel-5051411b92b4aca4ed5ec462e7b0e52af4d3951e.tar.gz |
Refactor JavaScript QWebChannel to take an external transport object.
This assimilates the JavaScript side to the QML/C++ side. We get rid
of the automagic WebSocket code. Instead, users pass in the WebSocket
from the outside, if they want to use that for communication. In the
QtWebKit/QtWebEngine cases, we will pass in our custom IPC objects.
Change-Id: I15e15b5130f99dc8b39dfbfa8cd3d8b2d34dbbc0
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@digia.com>
Reviewed-by: Lutz Schönemann <lutz.schoenemann@basyskom.com>
-rw-r--r-- | examples/standalone/index.html | 57 | ||||
-rw-r--r-- | src/webchannel/qwebchannel.js | 46 | ||||
-rw-r--r-- | tests/auto/qml/Client.qml | 3 |
3 files changed, 51 insertions, 55 deletions
diff --git a/examples/standalone/index.html b/examples/standalone/index.html index e1a74fb..3cce3ec 100644 --- a/examples/standalone/index.html +++ b/examples/standalone/index.html @@ -10,30 +10,47 @@ var output = document.getElementById("output"); output.innerHTML = output.innerHTML + message + "\n"; } - var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]); - new QWebChannel(baseUrl, function(channel) { - // make dialog object accessible globally - window.dialog = channel.objects.dialog; + window.onload = function() { + var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]); + output("Connecting to WebSocket server at " + baseUrl + "."); + var socket = new WebSocket(baseUrl); - document.getElementById("send").onclick = function() { - var input = document.getElementById("input"); - var text = input.value; - if (!text) { - return; - } + socket.onclose = function() + { + console.error("web channel closed"); + }; + socket.onerror = function(error) + { + console.error("web channel error: " + error); + }; + socket.onopen = function() + { + output("WebSocket connected, setting up QWebChannel."); + new QWebChannel(socket, function(channel) { + // make dialog object accessible globally + window.dialog = channel.objects.dialog; - output("Sent message: " + text); - input.value = ""; - dialog.receiveText(text); - } + document.getElementById("send").onclick = function() { + var input = document.getElementById("input"); + var text = input.value; + if (!text) { + return; + } + + output("Sent message: " + text); + input.value = ""; + dialog.receiveText(text); + } - dialog.sendText.connect(function(message) { - output("Received message: " + message); - }); + dialog.sendText.connect(function(message) { + output("Received message: " + message); + }); - dialog.receiveText("Client connected, ready to send/receive messages!"); - output("Connected to WebChannel, ready to send/receive messages!"); - }); + dialog.receiveText("Client connected, ready to send/receive messages!"); + output("Connected to WebChannel, ready to send/receive messages!"); + }); + } + } //END SETUP </script> <style type="text/css"> diff --git a/src/webchannel/qwebchannel.js b/src/webchannel/qwebchannel.js index 3274d65..0fae276 100644 --- a/src/webchannel/qwebchannel.js +++ b/src/webchannel/qwebchannel.js @@ -55,19 +55,26 @@ var QWebChannelMessageTypes = { response: 10, }; -// TODO: always expect an initialized transport object with a defined interface -// to be passed in, remove automagic WebSocket code -var QWebChannel = function(baseUrlOrSocket, initCallback) +var QWebChannel = function(transport, initCallback) { + if (typeof transport !== "object" || typeof transport.send !== "function") { + console.error("The QWebChannel expects a transport object with a send function and onmessage callback property." + + " Given is: transport: " + typeof(transport) + ", transport.send: " + typeof(transport.send)); + return; + } + var channel = this; + this.transport = transport; + this.send = function(data) { if (typeof(data) !== "string") { data = JSON.stringify(data); } - channel.socket.send(data); + channel.transport.send(data); } - this.onMessageReceived = function(message) + + this.transport.onmessage = function(message) { var data = message.data; if (typeof data === "string") { @@ -173,34 +180,7 @@ var QWebChannel = function(baseUrlOrSocket, initCallback) channel.send({type: QWebChannelMessageTypes.debug, data: message}); }; - this.onSocketReady = function(doneCallback) - { - channel.exec({type: QWebChannelMessageTypes.init}); - } - - if (typeof baseUrlOrSocket === 'object') { - this.socket = baseUrlOrSocket; - this.socket.send = function(data) - { - channel.socket.postMessage(data); - } - this.socket.onmessage = this.onMessageReceived - this.onSocketReady(); - } else { - ///TODO: use QWebChannel protocol, once custom protcols are supported by QtWebSocket - this.socket = new WebSocket(baseUrlOrSocket/*, "QWebChannel" */); - - this.socket.onopen = this.onSocketReady - this.socket.onclose = function() - { - console.error("web channel closed"); - }; - this.socket.onerror = function(error) - { - console.error("web channel error: " + error); - }; - this.socket.onmessage = this.onMessageReceived - } + channel.exec({type: QWebChannelMessageTypes.init}); }; function QObject(name, data, webChannel) diff --git a/tests/auto/qml/Client.qml b/tests/auto/qml/Client.qml index d15fd58..fad1412 100644 --- a/tests/auto/qml/Client.qml +++ b/tests/auto/qml/Client.qml @@ -59,10 +59,9 @@ Item { QtObject { id: clientTransport - property var send; property var onmessage; - function postMessage(message) + function send(message) { if (debug) { console.log("client posts message: ", message); |