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.
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 <QtNetwork>
...
class MyClient : public QObject
{
std::unique_ptr<QTcpSocket> tcpSocket_;
...
public slots:
void readTcpData();
...
then in your .cpp file do something like:
MyClient::MyClient()
{
tcpSocket_ = std::unique_ptr<QTcpSocket>(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!
You can have a look at QWebSockets, which implements websockets using Qt. I also have build both client and server socket.io classes, which will be open sourced soon.