From 5051411b92b4aca4ed5ec462e7b0e52af4d3951e Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Wed, 16 Jul 2014 14:33:03 +0200 Subject: Refactor JavaScript QWebChannel to take an external transport object. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Lutz Schönemann --- src/webchannel/qwebchannel.js | 46 ++++++++++++------------------------------- 1 file changed, 13 insertions(+), 33 deletions(-) (limited to 'src') 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) -- cgit v1.2.1