void ChatUDPSocket::readyRead (){ QByteArray buffer; buffer.resize(mUdpSocket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; mUdpSocket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort); qDebug() << "message from : " << sender.toString(); qDebug() << "message port : " << senderPort; qDebug() << "message : " << buffer; QList<QByteArray> packList = buffer.split(':'); QString pType(packList[0].trimmed().toLower()); QUuid pUuid(packList[1]); QString pContent(packList[2]); qDebug() << pType; if(pType == "request") { // request message QByteArray data; data.append("response:"); data.append(pUuid.toString()).append(":"); data.append("accepted"); mUdpSocket->writeDatagram(data, sender, ChatPort); mUdpSocket->flush(); emit receiveSuccess(pContent); } else if (pType == "response") { // response message if(pContent.trimmed().toLower() == "accepted") { emit sendSuccess(pUuid); } } }
void ChatService::listen(){ connect(mUdpService, SIGNAL(receiveError(QString)), this, SIGNAL(receiveError(QString))); connect(mUdpService, SIGNAL(receiveSuccess(QHostAddress,quint16,ChatMessage)), this, SIGNAL(receiveSuccess(QHostAddress,quint16,ChatMessage))); connect(mUdpService, SIGNAL(sendError(QUuid,QString)), this, SIGNAL(sendError(QUuid,QString))); connect(mUdpService, SIGNAL(sendSuccess(QUuid)), this, SIGNAL(sendSuccess(QUuid))); }
ChatForm::ChatForm(User *receiver, QWidget *parent) : QWidget(parent), ui(new Ui::ChatForm), receiver(receiver), sender(UserService::getService()->getMyself()), mChatRecords(ChatRecordService::getService()->getChatRecordsByUserUuid(receiver->getUuid(), ChatRecord::NotRead)), mIconService(IconService::getService()), mScreenshotsWidget(new ScreenshotsWidget) { ui->setupUi(this); /* recent friends */ UserService::getService()->insertRecentFriend(*receiver); /* end of recent friends */ setAttribute(Qt::WA_DeleteOnClose); this->mChatService = ChatService::getService(); this->mFileMsgService = FileMessageService::getService(); ui->userIcon->setIcon(this->receiver->getIcon()); ui->userIcon->setIconSize(QSize(40, 40)); ui->usernameLabel->setText(this->receiver->getName()); ui->signatrueLabel->setText(this->receiver->getInfo()); setStyleSheet("#ChatForm {background-color:#ffdbf8}"); connect(mChatService, SIGNAL(receiveSuccess(QHostAddress,quint16,ChatMessage)), this, SLOT(receiveSuccess(QHostAddress,quint16,ChatMessage))); connect(mChatService, SIGNAL(sendError(QUuid, QString)), this, SLOT(sendError(QUuid, QString))); connect(mChatService, SIGNAL(sendSuccess(QUuid)), this, SLOT(sendSuccess(QUuid))); connect(mFileMsgService, SIGNAL(received(QHostAddress,quint16,ChatMessage)), this, SLOT(fileMsgReceived(QHostAddress,quint16,ChatMessage))); updateChatRecordView(); loadSetting(); sendFileModel = new QStandardItemModel; receiveFileModel = new QStandardItemModel; QStringList heads; heads << "filename" << "progress" << "size" << "type" << "url" << "uuid"; sendFileModel->setHorizontalHeaderLabels(heads); receiveFileModel->setHorizontalHeaderLabels(heads); ui->sendFileTableView->setModel(sendFileModel); ui->receiveFileTableView->setModel(receiveFileModel); mFileSender = 0; mFileReceiver = 0; mFileMessage = 0; fileSendRow = 0; fileReceiveRow = 0; mShakeTimer = new QTimer(this); mShakeTimer->setInterval(30); mShakePosition = 0; shakeMaxLimitTimes = 12; shakeMaxLimitSpace = 5; connect(mShakeTimer, SIGNAL(timeout()),this, SLOT(shakeTimeOut())); connect(mScreenshotsWidget, SIGNAL(finishPixmap(QPixmap)), this, SLOT(screenshotsFinish(QPixmap))); connect(&mFacesDialog, SIGNAL(clicked(Image)), this, SLOT(facesClicked(Image))); }