Cits Cits - 2 months ago 12
C++ Question

Unable use any QWebSocket client basic example

Im unable to use any of the QWebSockets client examples listed here:
The application tries to open the localhost url, the socket gets stuck in QAbstractSocket::ConnectingState and

qDebug() << m_webSocket.error();

returns QAbstractSocket::UnknownSocketError. Of course the 'connected' signal is never emitted.
I have qt 5.3 in a osx lion. I've got OpenSSL 1.0.1i and

QSslSocket::supportsSsl() ;

returns true.

Im really out of ideas so if you got any ill be extremely grateful.

Thx in advance.


Firstly, I don't think ssl is the issue in this case.

I just ran the examples and it just worked fine. Which means there could be one of the following common problems (or there could be others I don't know about).

  • The local port number 1234 is being used by other process. Which means, try editing EchoClient client(QUrl(QStringLiteral("ws://localhost:1234"))); and replace 1234 with a number like 8888 or 9001 etc should solve the problem. Notice, you have to change port in both client and server program in that case. (Read next point to fully understand in case you don't understand what I am saying.)
  • Run the client program while the corresponding server program is running. For example, when you are running echoclient, you are supposed to keep the echoserver running. If you are in windows or linux, you can do this by opening two Qt Creator process and running the echoserver first and then echoclient. If you are in Mac, you can't create two instances of Qt Creator (at least in Yosemite, I don't know about osx lion). You have to run the echoserver (and don't close the console), then open the echoclient project and Set echoclient as Active Project and run.

  • (though not likely) if you are opening a project, copying the cpp files from website and pasting it in your project and then compiling (instead of downloading it form Examples tab of your Qt Creator), then you may be missing configuration of .pro file. Just add websockets with QT += core in the .pro file of the project.

If these are not the cases, then you can let me know and you can try posting in the Qt forum too in that case.