From e82b9c37609ab8f686a6dfd312bf0423bb740f35 Mon Sep 17 00:00:00 2001 From: Venugopal Shivashankar Date: Tue, 22 Dec 2015 13:28:10 +0100 Subject: Example: Modify JavaScript part of the HTML The baseUrl that is used to connect to a WebSocket server is derived from the query parameters set to the URL. These parameters are ignored by the QDesktopServices::openUrl implementations specific to Windows and OS X. Ubuntu uses the default implementation that retains the query parameters. This means the browser instance will fail to open the local file URL on Ubuntu because it includes the query parameters, so query parameters should never be set. Moreover, the example creates a QWebSocketServer instance, which is available at ws://localhost:12345, so the it is safe to hardcode the address in the JavaScript. The cleaner approach would be to use a URLHandler as described in http://doc.qt.io/qt-5/qdesktopservices.html#url-handlers, but that complicates the example. Change-Id: I5b5df2b7b816ce0bbfb16a85c036ed379616f04a Task-number: QTBUG-46541 Reviewed-by: Milian Wolff --- examples/webchannel/standalone/index.html | 5 ++++- examples/webchannel/standalone/main.cpp | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/webchannel/standalone/index.html b/examples/webchannel/standalone/index.html index 778a502..cc3d72d 100644 --- a/examples/webchannel/standalone/index.html +++ b/examples/webchannel/standalone/index.html @@ -11,7 +11,10 @@ output.innerHTML = output.innerHTML + message + "\n"; } window.onload = function() { - var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]); + if (location.search != "") + var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]); + else + var baseUrl = "ws://localhost:12345"); output("Connecting to WebSocket server at " + baseUrl + "."); var socket = new WebSocket(baseUrl); diff --git a/examples/webchannel/standalone/main.cpp b/examples/webchannel/standalone/main.cpp index 8005301..9c03370 100644 --- a/examples/webchannel/standalone/main.cpp +++ b/examples/webchannel/standalone/main.cpp @@ -132,7 +132,6 @@ int main(int argc, char** argv) // open a browser window with the client HTML page QUrl url = QUrl::fromLocalFile(BUILD_DIR "/index.html"); - url.setQuery(QStringLiteral("webChannelBaseUrl=") + server.serverUrl().toString()); QDesktopServices::openUrl(url); dialog.displayMessage(QObject::tr("Initialization complete, opening browser at %1.").arg(url.toDisplayString())); -- cgit v1.2.1