【问题标题】:Qt5 WebSockets test app not connecting to test serviceQt5 WebSockets 测试应用程序未连接到测试服务
【发布时间】:2015-11-04 22:24:14
【问题描述】:

我想在ws://echo.websocket.org 中为测试服务打开一个qt websocket,但我收到了错误QAbstractSocket::RemoteHostClosedError 我将信号 error(QAbstractSocket::SocketError socketError) 连接到我的代码中的一个插槽,以便读取错误号,然后查找它 in here

我的代码是这样的

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Controller w;
    w.initializeWebSocket("ws://echo.websocket.org", true);
    w.show();

    return a.exec();
}






Controller::Controller(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
}

void Controller::initializeWebSocket(QString url, bool debug)
{
    m_webSocketURL = url;
    m_webSocketDebug = debug;
    if(m_webSocketDebug)
        std::cout << "WebSocket server: " << m_webSocketURL.toStdString() << std::endl;
    QObject::connect(&m_webSocket, SIGNAL(connected()), this, SLOT(onConnected()));
    QObject::connect(&m_webSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
    QObject::connect(&m_webSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError)));
    QObject::connect(&m_webSocket, SIGNAL(textMessageReceived(QString)), this, SLOT(onTextMessageReceived(QString)));
    m_webSocket.open(QUrl(m_webSocketURL));
}

void Controller::onConnected()
{
    if (m_webSocketDebug)
        std::cout << "WebSocket connected" << std::endl;
    m_webSocket.sendTextMessage(QStringLiteral("Rock it with HTML5 WebSocket"));
}

void Controller::onDisconnected()
{
    if (m_webSocketDebug)
        std::cout << "WebSocket disconnected" << std::endl;
}

void Controller::onError(QAbstractSocket::SocketError error)
{
    std::cout << error << std::endl;
}

void Controller::onTextMessageReceived(QString message)
{
    if (m_webSocketDebug)
        std::cout << "Message received:" << message.toStdString() << std::endl;
    m_webSocket.close();
}

我是 websockets 新手,所以我不知道问题出在哪里。谁能给点建议?

【问题讨论】:

  • 另外,在Qt项目中使用:qDebug()

标签: qt qtwebsockets


【解决方案1】:

在“ws://echo.websocket.org”打开 websocket 对我来说很好。

这些处理程序在我的项目中就足够了:

connect(&webSocket, SIGNAL(connected()), this, SLOT(onConnected()));
connect(&webSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
connect(&webSocket, SIGNAL(textMessageReceived(const QString&)), this, SLOT(onTextMessageReceived(const QString&)));

我也刚刚意识到我没有连接 error() 信号,但程序代码已经相当可靠一年多了,如果断开连接,则会恢复连接。也许我应该连接 error()对于不常见的奇怪情况也可以发出信号。

错误 QAbstractSocket::RemoteHostClosedError 可能是正确的。尽量在合理的时间内得到回声。我们在项目中使用的 websocket 农场将保持连接长达 50 分钟,因此我们在客户端和服务器之间进行 ping-pong 以在此期限到期之前保持连接有效。

    // you can try that immediately after opening the web socket and also using some QTimer
    m_webSocket.sendTextMessage("Pong!");

只要你在玩一些公共回声服务,试试看文本回复。

【讨论】:

    【解决方案2】:

    嗯,我验证了你的代码,它似乎工作正常。您给出的错误表明与主机相关的问题。这可能是由于防火墙、isp 或其他阻止/问题。

    WebSocket server: ws://echo.websocket.org
    WebSocket connected
    Message received:Rock it with HTML5 WebSocket
    WebSocket disconnected
    

    我想指出,最好保留一个指向 QWebSocket“对象”的指针。很方便的将m_webSocket声明为QWebSocket *,加上m_webSocket = new QWebSocket(this)。将对象视为对象是一种很好的做法。您不想不小心尝试直接“复制”一个 QWebSocket。此外,由于 Qt 的内部结构,如果这个“控制器”对象被销毁而 QWebSocket 仍然附加到其他对象(尽管我认为 Qt 已为此做好准备),您最终可能会遇到问题。

    【讨论】:

    • 我刚刚在具有不同网络的另一台计算机上尝试了它,它似乎工作正常。需要检查另一边发生了什么!
    猜你喜欢
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2020-12-18
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多