2
votes

I've been trying to set up a websocket client using the Qt websocket class. Unfortunately I haven't been able to progress past the first step: connecting my client to a server.

I'm certain that the connection is possible because both the Go ws websocket client and the C++ easywsclient library can connect and interface with the server.

The relevant part of my code is the following:

  QWebSocket socket;
  socket.open(QUrl("ws://localhost:9999"));
  qDebug() << socket.error() << socket.errorString();

When running the program, I get the following (unhelpful) message:

QAbstractSocket::UnknownSocketError "Unknown error"

Is there any way to clarify the error and/or fix the problem?

P.S. While the easywsclient library can connect and interface with the server it only does a marginal job, which is why I'd rather use the Qt class.

1

1 Answers

1
votes

There is no error. The open() is asynchronous. Connect the connected(), disconnected(), error(), textMessageReceived() signals to the slots of your QObject-derived object. Or use some lambdas if it's a very basic application:

QWebSocket socket;
QObject::connect(&socket, &QWebSocket::connected, [] { qDebug() << "connected"; });
QObject::connect(&socket, &QWebSocket::error, [](QAbstractSocket::SocketError error) { qDebug() << error; });
socket.open(QUrl("ws://localhost:9999"));