3

I want to Connect My Node JS App With Qt(C++) program, I'm using Socket.io for node JS (The Server) and QWebSocket for the Qt Program (the client).

But after all my trials i don't get to work. it shows me the error from the client side:

QAbstractSocket::RemoteHostClosedError

and from the server side, no signs of incoming connections.

here is my source code for that purpose:

Node JS Server:

var io = require('socket.io')(8082);

io.on('connection', function (socket) {
  io.emit('this', 'Hey Welcome!');
   console.log("New connection!");

  socket.on('private message', function (from, msg) {
    console.log('I received a private message by ', from, ' saying ', msg);
  });

  socket.on('disconnect', function () {
    io.emit('user disconnected');
  });
});

Qt C++ client:

    #include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) : QWidget(parent),  ui(new Ui::Widget)
{
    ui->setupUi(this);

    webSocket  = new QWebSocket();

    webSocket->open(QUrl(("ws://localhost:8082")));

    connect(webSocket, SIGNAL(connected()), this, SLOT(isConnected()));
    connect(webSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslError(QList<QSslError>)));
    connect(webSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(logError(QAbstractSocket::SocketError)));
    connect(webSocket, SIGNAL(textMessageReceived(QString)), this, SLOT(newMessage(QString)));
    connect(webSocket, SIGNAL(textFrameReceived(QString,bool)), this, SLOT(newMessageBit(QString,bool)));
}

Widget::~Widget()
{
    delete ui;
}


void Widget::isConnected()
{
    webSocket->sendTextMessage("Hello From Qt!!!");
}

void Widget::logError(QAbstractSocket::SocketError err)
{
    qDebug() << "Error: ";
    qDebug() << err;
}

void Widget::sslError(QList<QSslError> errors)
{
    qDebug() << "SSLError: ";
    qDebug() << errors;
    webSocket->ignoreSslErrors(errors);
}


void Widget::newMessage(QString msg)
{
    qDebug() << msg;
}

void Widget::newMessageBit(QString msg, bool isLast)
{
    qDebug() << msg;
    qDebug() << isLast;
}

I'm also getting these errors(at runtime) from the debug console

QSslSocket: cannot resolve TLSv1_1_client_method
QSslSocket: cannot resolve TLSv1_2_client_method
QSslSocket: cannot resolve TLSv1_1_server_method
QSslSocket: cannot resolve TLSv1_2_server_method
QSslSocket: cannot resolve SSL_select_next_proto
QSslSocket: cannot resolve SSL_CTX_set_next_proto_select_cb
QSslSocket: cannot resolve SSL_get0_next_proto_negotiated
Xsmael
  • 3,624
  • 7
  • 44
  • 60

2 Answers2

3

Though I'm late to the party but still if someone is also facing this issue (excluding ssl errors), it might be useful. First thing first, socket.io is not compatible with standard websockets which are used by Qt(or c++, java, php or any other language). If you are using socket.io at server then you can only use socket.io specific client libraries not standard websocket libraries.

There are several socket.io specific client libraries in other languages -

Java: https://github.com/socketio/socket.io-client-java
C++: https://github.com/socketio/socket.io-client-cpp
Swift: https://github.com/socketio/socket.io-client-swift
Dart: https://github.com/rikulo/socket.io-client-dart
Python: https://github.com/miguelgrinberg/python-socketio
.Net: https://github.com/Quobject/SocketIoClientDotNet
raj33krish
  • 31
  • 5
1

Most likely the OpenSSL dynamic libraries on your system are not compatible with the ones that Qt was built with. What Qt version are you using? Since you have not specified the operating system it's hard to tell whether this is because the system itself provides an old verion of OpenSSL but I'd suggest looking up the symbols (for example SSL_get0_next_proto_negotiated) in your OpenSSL libraries with any of the dump tools available and if they are not there - either downgrade Qt or upgrade OpenSSL.

As stated here - from Qt 5.2 and up you need at least OpenSSL 1.0.0, older versions may fail.

UPDATE

As I suspected the errors you get for the SSL really are due to a bad OpenSSL binary. You can get the 1.0.0+ libraries for windows from the Shining Light Productions site, but that only fixes the OpenSSL errors. I took your example and added additional slots and it seems that the stateChanged signal is fired with the state QAbstractSocket::ConnectingState but nothing happens after that.

Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71
  • Thank you for your answer, but i have no Idea what is OpenSSL, i just heard about it, all i know is that it's for security, but i didn't specified in my code i wanted security, i don't know why Qt is trying to use it. I'm running Windows7 with Qt 5.4. I've never installed OpenSSL on my system; i guess i just have to install it. But if so, all other users will need to install it too to be able to use my progrem; Is it not possible to just disable the SSL ? – Xsmael Feb 18 '16 at 08:57
  • @Xsmael I am not so familiar with websockets but it seems that you are using a `ws` url instead of `wss` so yeah, the socket should not use SSL security. Not sure about socket.io though - maybe it enables SSL by default, I need to Google a bit. – Rudolfs Bundulis Feb 18 '16 at 10:58
  • @Xsmael - any chance you could use Wireshark to see what is going on? – Rudolfs Bundulis Feb 18 '16 at 11:03
  • @Xsmael - as far as I see in Wireshark, the initial GET request is not sent - do not know why. – Rudolfs Bundulis Feb 18 '16 at 17:32
  • Thank You so much for all your effort and time, i tried wireshark also,but did not help much,but apparently, there is an icompatibily between Qt and Socket.io (node js) concerning the websocket protocol implementation. So I tried other libraries i got one that works (ws). Also about the OpenSSL warnings you were right indeed, but apparently as you found out, they were not responsible for the failure of the communication. see http://stackoverflow.com/questions/26361145/qsslsocket-error-when-ssl-is-not-used as far as we don't use ssl but installing the right lib clears those warnings. Thanks! – Xsmael Feb 18 '16 at 18:07
  • Well something is very wrong here - I had a debug Qt build lying around and used that to debug - and it seems that the socket never enters a connected state. Do not know why :( – Rudolfs Bundulis Feb 18 '16 at 18:24
  • Yes indeed the same happens to me, regardless whether the OpenSSL is installed or not, i was even surprised when you said you reached the ConnectingSate. I think socket.io has implement the protocol differently or is outdated,(i saw on some post that it was) and WS(https://github.com/websockets/ws) is better and faster, so i think i'm going with it – Xsmael Feb 18 '16 at 18:34
  • @Xsmale - you did not have a slot for the stateChanged signal so that is why you did not get it:) – Rudolfs Bundulis Feb 18 '16 at 18:59
  • I did! just after you told me you added this slot. – Xsmael Feb 19 '16 at 05:08