简体   繁体   中英

Qt and Node JS network communication through WebSockets

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM