Qt Connect to WebSockets server
Problem
Could someone point me in the right direction as to getting Qt to Connect to a Node.JS running Socket.io server?
I've found examples of running Qt as a server, but none reading the data back.
Related Articles
Solution
In Qt there are atleast two ways of doing it. Either using blocking or non-blocking calls. If you use non-blocking calls the you "connect" your signals to the slots of the TcpSocket class you are using. The when some data has arrived you get an event/callback is triggered. This is not a complete example:
In your .h file declare the
#include
...
class MyClient : public QObject
{
std::unique_ptr tcpSocket_;
...
public slots:
void readTcpData();
...
then in your .cpp file do something like:
MyClient::MyClient()
{
tcpSocket_ = std::unique_ptr(new QTcpSocket(this));
connect(tcpSocket_.get(), SIGNAL(readyRead()), this, SLOT(readTcpData()));
}
void MyClient::readTcpData()
{
QByteArray rawData = tcpSocket_->readAll();
QString textData(rawData);
... do something with the received data.
}
To write data you can use tcpSocket_->write(...). So whenever data arrives the readReady() signal triggers the readTcpData() function and you know there is "some" data available to read from the socket. Hope it helps and answers your question. Good luck!
Discussion
View additional discussion.