0

テストサービスへのqt websocketを開きたかったのですws://echo.websocket.orgが、エラー番号を読み取るためにコード内のスロットにQAbstractSocket::RemoteHostClosedError 信号を接続しているというエラーが表示され、ここerror(QAbstractSocket::SocketError socketError)でそれを探します

私のコードは次のようになります

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();
}

私はウェブソケットを初めて使用するので、どこに問題があるのか​​ わかりません。誰でもアドバイスできますか?

4

2 に答える 2

1

Opening websocket at "ws://echo.websocket.org" works for me just fine.

These handlers are sufficient in my project:

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&)));

I also just realized that I don't connect error() signal yet the program code is quite reliable for more than a year already and in case of disconnect there is a connection restore kick in. Maybe I should connect error() signal as well for infrequent strange cases.

The error QAbstractSocket::RemoteHostClosedError can be just a correct thing to get. Try to get the echo within reasonable time. The websocket farm we use in our project is holding the connection for up to 50 minutes so we do ping-pong between the client and the server to keep the connection live before this period expires.

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

Try that and see the text reply as long as you are playing some public echo service.

于 2015-11-05T02:13:55.840 に答える