//! [onTextMessageReceived] void EchoClient::onTextMessageReceived(QString message) { // only resume if greeting was successful if(!m_greetingFinished){ return; } qDebug() << "Message received:" << message; QJsonDocument jsonMessage = QJsonDocument::fromJson(message.toUtf8()); QJsonObject localObject = jsonMessage.object(); QJsonObject header = localObject.value("header").toObject(); int messageType = header.value("contentType").toInt(); switch (messageType) { case Greeting: if(header.value("userId").toString() == m_userId){ // greeting finished successfully, allow further communication m_greetingFinished = true; } else { m_greetingFinished = false; } break; case Ack_Server: break; case Ack_Destination: // update local db if(localObject.value("body").toObject().value("desc").toString() == "ChatData"){ // its a chat ack, lets update the chat emit updateChatStatus(localObject.value("body").toObject().value("id").toInt(), ReachedDestination); } break; case ChatData: // save data emit incomingChatData(localObject); // send ack-destination sendAcknowledgement(Ack_Destination, header.value("sender").toString(), header.value("severName").toString(), "ChatData", localObject.value("body").toObject().value("id").toString()); break; default: break; } }
void QXmppStream::_q_socketReadyRead() { d->dataBuffer.append(d->socket->readAll()); // handle whitespace pings if (!d->dataBuffer.isEmpty() && d->dataBuffer.trimmed().isEmpty()) { d->dataBuffer.clear(); handleStanza(QDomElement()); } // FIXME : maybe these QRegExps could be static? QRegExp startStreamRegex("^(<\\?xml.*\\?>)?\\s*<stream:stream.*>"); startStreamRegex.setMinimal(true); QRegExp endStreamRegex("</stream:stream>$"); endStreamRegex.setMinimal(true); // check whether we need to add stream start / end elements // // NOTE: as we may only have partial XML content, do not alter the stream's // state until we have a valid XML document! QByteArray completeXml = d->dataBuffer; const QString strData = QString::fromUtf8(d->dataBuffer); bool streamStart = false; if (d->streamStart.isEmpty() && strData.contains(startStreamRegex)) streamStart = true; else completeXml.prepend(d->streamStart); bool streamEnd = false; if (strData.contains(endStreamRegex)) streamEnd = true; else completeXml.append(streamRootElementEnd); // check whether we have a valid XML document QDomDocument doc; if (!doc.setContent(completeXml, true)) return; // remove data from buffer logReceived(strData); d->dataBuffer.clear(); // process stream start if (streamStart) { d->streamStart = startStreamRegex.cap(0).toUtf8(); handleStream(doc.documentElement()); } // process stanzas QDomElement nodeRecv = doc.documentElement().firstChildElement(); while (!nodeRecv.isNull()) { if (QXmppStreamManagementAck::isStreamManagementAck(nodeRecv)) handleAcknowledgement(nodeRecv); else if (QXmppStreamManagementReq::isStreamManagementReq(nodeRecv)) sendAcknowledgement(); else { handleStanza(nodeRecv); if(nodeRecv.tagName() == QLatin1String("message") || nodeRecv.tagName() == QLatin1String("presence") || nodeRecv.tagName() == QLatin1String("iq")) ++d->lastIncomingSequenceNumber; } nodeRecv = nodeRecv.nextSiblingElement(); } // process stream end if (streamEnd) disconnectFromHost(); }