/** Starts to read on connected. */ void AJAXChat::connectFinished() { QNetworkReply *reply = static_cast<QNetworkReply *>(QObject::sender()); if (reply->errorString().isEmpty() || reply->errorString() == "Unknown error") { emit connectFinished(true); readTimer->start(); } else { qDebug(QString("Connect error: " + reply->errorString()).toUtf8()); emit connectFinished(false); readTimer->stop(); } }
void AJAXChat::connectToServer(const QString &server, const QString &userName, const QString &password, const QString &channel, const QString &forumLoginUrl) { _server = server; // setup request parameters QUrl params; params.addQueryItem("login", "login"); params.addQueryItem("redirect", server); params.addQueryItem("username", userName); params.addQueryItem("password", password); params.addQueryItem("channelName", channel); params.addQueryItem("lang", "en"); params.addQueryItem("submit", "Login"); // if the forum login url is set, then login to the forum and then go to the chat, // otherwise, directly go to the chat QString connectUrl = forumLoginUrl.isEmpty() ? server : forumLoginUrl; QUrl url(connectUrl); QNetworkRequest request(url); // content type is needed or you will get runtime debug warnings. This kind of header is needed or // the chat will just asks the bot to logout request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); QNetworkReply *reply = net->post(request, params.encodedQuery()); connect(reply, SIGNAL(finished()), this, SLOT(connectFinished())); connect(reply, SIGNAL(finished()), reply, SLOT(deleteLater())); }
//get user information and pass into thread void AwesumeClient::on_btnConnect_clicked() { if (ui->btnConnect->text() == "Connect") { QString hostname = ui->hostname->text(); QString user = ui->username->text(); QString password = ui->password->text(); thread = new ConnectThread(hostname, user, password); connect(thread, SIGNAL(finished()), this, SLOT(connectFinished())); thread->start(); ui->btnConnect->setText("Disconnect"); } else if (ui->btnConnect->text() == "Disconnect") { serverDisconnected(); ui->btnConnect->setText("Connect"); ui->hostname->clear(); ui->username->clear(); ui->password->clear(); } SCORE->setInterval(100); connect(SCORE, &QTimer::timeout, this, &AwesumeClient::timerHit); SCORE->start(); }