summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMilian Wolff <mail@milianw.de>2014-03-21 16:10:09 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-06-08 15:40:31 +0200
commitdb645f9178716a61ba4caeef0dade292460967ef (patch)
tree6d23e8b6e88859b631845929a6e825a059b39638 /src
parent6bd414a1f74f5a0ebfc1f4abcc140f7b3b536e1b (diff)
downloadqtwebsockets-db645f9178716a61ba4caeef0dade292460967ef.tar.gz
Add a QUrl serverUrl method to QWebSocketServer.
It is useful in many places, esp. when constructing clients that should connect to the server we created. Note that the returned host address will be LocalHost instead of Any, to make sure clients can connect to the server even on Windows. This should hopefully resolve the CI issues. Change-Id: I3c400ad4d785ea398cf1a1bd113c0833fda9e3bd Reviewed-by: Kurt Pattyn <pattyn.kurt@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/websockets/qwebsocketserver.cpp38
-rw-r--r--src/websockets/qwebsocketserver.h1
2 files changed, 39 insertions, 0 deletions
diff --git a/src/websockets/qwebsocketserver.cpp b/src/websockets/qwebsocketserver.cpp
index 20aa009..2d8f26b 100644
--- a/src/websockets/qwebsocketserver.cpp
+++ b/src/websockets/qwebsocketserver.cpp
@@ -495,6 +495,44 @@ quint16 QWebSocketServer::serverPort() const
}
/*!
+ Returns a URL clients can use to connect to this server if the server is listening for connections.
+ Otherwise an invalid URL is returned.
+
+ \sa serverPort(), serverAddress(), listen()
+ */
+QUrl QWebSocketServer::serverUrl() const
+{
+ QUrl url;
+
+ if (!isListening()) {
+ return url;
+ }
+
+ switch (secureMode()) {
+ case NonSecureMode:
+ url.setScheme(QStringLiteral("ws"));
+ break;
+ #ifndef QT_NO_SSL
+ case SecureMode:
+ url.setScheme(QStringLiteral("wss"));
+ break;
+ #endif
+ }
+
+ url.setPort(serverPort());
+
+ if (serverAddress() == QHostAddress(QHostAddress::Any)) {
+ // NOTE: On Windows at least, clients cannot connect to QHostAddress::Any
+ // so in that case we always return LocalHost instead.
+ url.setHost(QHostAddress(QHostAddress::LocalHost).toString());
+ } else {
+ url.setHost(serverAddress().toString());
+ }
+
+ return url;
+}
+
+/*!
Sets the maximum number of pending accepted connections to \a numConnections.
WebSocketServer will accept no more than \a numConnections incoming connections before
nextPendingConnection() is called.
diff --git a/src/websockets/qwebsocketserver.h b/src/websockets/qwebsocketserver.h
index 1f89ca9..4e76cd4 100644
--- a/src/websockets/qwebsocketserver.h
+++ b/src/websockets/qwebsocketserver.h
@@ -90,6 +90,7 @@ public:
quint16 serverPort() const;
QHostAddress serverAddress() const;
+ QUrl serverUrl() const;
SslMode secureMode() const;