简体   繁体   English

Qt 和 Node JS 通过 WebSockets 网络通信

[英]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).我想用 Qt(C++) 程序连接我的 Node JS 应用程序,我将 Socket.io 用于节点 JS(服务器)和 QWebSocket 用于 Qt 程序(客户端)。

But after all my trials i don't get to work.但经过我所有的试验,我无法开始工作。 it shows me the error from the client side:它向我显示了客户端的错误:

QAbstractSocket::RemoteHostClosedError QAbstractSocket::RemoteHostClosedError

and from the server side, no signs of incoming connections.从服务器端,没有传入连接的迹象。

here is my source code for that purpose:这是我为此目的的源代码:

Node JS Server:节点JS服务器:

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: Qt C++ 客户端:

    #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.虽然我迟到了,但如果有人也面临这个问题(不包括 ssl 错误),它可能会有用。 First thing first, socket.io is not compatible with standard websockets which are used by Qt(or c++, java, php or any other language).首先,socket.io 与 Qt(或 c++、java、php 或任何其他语言)使用的标准 websocket 不兼容。 If you are using socket.io at server then you can only use socket.io specific client libraries not standard websocket libraries.如果您在服务器上使用 socket.io,那么您只能使用 socket.io 特定的客户端库,而不是标准的 websocket 库。

There are several socket.io specific client libraries in other languages -其他语言有几个 socket.io 特定的客户端库 -

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.您系统上的 OpenSSL 动态库很可能与 Qt 构建时使用的库不兼容。 What Qt version are you using?你用的是什么Qt版本? 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.由于您尚未指定操作系统,因此很难判断这是否是因为系统本身提供了旧版本的 OpenSSL,但我建议使用任何可用的转储工具在您的 OpenSSL 库中查找符号(例如 SSL_get0_next_proto_negotiated)如果它们不存在 - 降级 Qt 或升级 OpenSSL。

As stated here - from Qt 5.2 and up you need at least OpenSSL 1.0.0, older versions may fail.如前所述这里-从Qt的5.2及以上则至少需要OpenSSL的1.0.0,旧版本可能会失败。

UPDATE更新

As I suspected the errors you get for the SSL really are due to a bad OpenSSL binary.正如我怀疑您为 SSL 获得的错误确实是由于错误的 OpenSSL 二进制文件造成的。 You can get the 1.0.0+ libraries for windows from the Shining Light Productions site, but that only fixes the OpenSSL errors.您可以从Shining Light Productions站点获取适用于 Windows 的 1.0.0+ 库,但这只能修复 OpenSSL 错误。 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.我拿了你的例子并添加了额外的插槽,似乎stateChanged信号是用状态QAbstractSocket::ConnectingState触发的,但之后没有任何反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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