【发布时间】:2016-05-28 01:32:16
【问题描述】:
我想将我的 Node JS 应用程序与 Qt(C++) 程序连接起来,我将 Socket.io 用于节点 JS(服务器),将 QWebSocket 用于 Qt 程序(客户端)。
但经过我所有的试验,我没有开始工作。它向我显示了来自客户端的错误:
QAbstractSocket::RemoteHostClosedError
从服务器端看,没有传入连接的迹象。
这是我的源代码:
节点 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++ 客户端:
#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;
}
我也从调试控制台收到这些错误(在运行时)
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
【问题讨论】:
标签: c++ node.js qt websocket network-programming