void Client::readData() { QString line; while (_socket->canReadLine()) { line = _socket->readLine(); if (!line.isEmpty()) { line.resize(line.length() - 1); QStringList stringList = line.split(' '); if (!stringList.isEmpty()) { if (stringList[0] == NS_PING) sendMessage(line); else if (line == NS_NO_SUCH_CMD) _lastCmd = NO_CMD; else if (_lastCmd == NB_ASK_AUTH) logCmd(line); else if (_lastCmd == NB_USR_LOG) logEndCmd(line); else if (stringList[0] == NS_SALUT) salutCmd(stringList); else if (stringList[0] == NS_CMD_USR) getUsrCmd(line, stringList[1]); else if (line == NS_LOGIN_FAIL) { _socket->abort(); emit error("User identification fail"); } emit readyMessage(line); } } } }
Client::Client(QObject *window) : _lastCmd(NO_CMD) { _socket = new QTcpSocket(this); connect(_socket, SIGNAL(readyRead()), this, SLOT(readData())); connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError))); connect(_socket, SIGNAL(disconnected()), window, SLOT(disconnected())); connect(this, SIGNAL(readyMessage(QString)), window, SLOT(readMessage(QString))); connect(this, SIGNAL(readyBuddyMessage(const QString&, const QString&)), window, SLOT(handleBuddyMessage(const QString&, const QString&))); connect(this, SIGNAL(buddyStatus(const QString&, const QString&)), window, SLOT(changeBuddyStatus(const QString&, const QString&))); connect(this, SIGNAL(connected()), window, SLOT(connected())); connect(this, SIGNAL(error(QString)), window, SLOT(handleError(QString))); connectMe(); }
MainWindow::MainWindow():MApplicationWindow() { QString IPaddr="127.0.0.1"; int port=3425; page1=new HomePage(this); page2 =new LoginPage(this); page3=new RegistrationPage(this); page4=new SettingPage(this); page5 =new InfoPage(this); page6 = new ContactlistPage(); page7=new DialogPage(this); sock=QSharedPointer<QTcpSocket>( new QTcpSocket()); sock->connectToHost(IPaddr,port); thread1=new SendThread(sock); thread2=new RecvThread(sock); thread1->start(); thread2->start(); page1->appear(); //переходы со страницы home на все остальные QObject::connect(page1,SIGNAL(goLoginPage()),this,SLOT(GoLoginPage())); QObject::connect(page1,SIGNAL(exiting()),this,SLOT(close())); QObject::connect(page1,SIGNAL(goRegistrationPage()),page3,SLOT(appear())); QObject::connect(page1,SIGNAL(goSettingPage()),page4,SLOT(appear())); QObject::connect(page1,SIGNAL(goContactListPage()),this,SLOT(GoContactListPage())); QObject::connect(page2,SIGNAL(goHomePage()),this,SLOT(GoHomePage())); QObject::connect(page1,SIGNAL(goInfoPage()),page5,SLOT(appear())); QObject::connect(page2,SIGNAL(readySend(QString)),thread1,SLOT(writeToServer(QString))); QObject::connect(page3,SIGNAL(readySend(QString)),thread1,SLOT(writeToServer(QString))); QObject::connect(page7,SIGNAL(readySend(QString)),thread1,SLOT(writeToServer(QString))); QObject::connect(page4,SIGNAL(changeSettings()),this,SLOT(ApplyNewSettings())); QObject::connect(thread2,SIGNAL(readyMessage(QSharedPointer<IMessage>)),this,SLOT(ListenServer(QSharedPointer<IMessage>))); QObject::connect(page6,SIGNAL(goDialogPage(QString)),this,SLOT(GoDialogPage(QString))); //***Signal incomingMessage(QString) should be replaced real signal about incoming message for user.*** QObject::connect(this, SIGNAL(incomingMessage(QString)),page6, SLOT(displayMessage(QString))); //***************************************************************************************************** }
void RecvThread::readFromServer() { qCritical()<<"READ "; QByteArray array=socket->readAll(); QString buffer = array; qCritical()<<buffer; if (!buffer.contains("#/")) { return; } QStringList strList=buffer.split("#/"); if (strList.isEmpty()) return; QStringListIterator iter(strList); while(iter.hasNext()) { QString tmp=iter.next(); QSharedPointer<IMessage> mes(new Message(tmp)); emit readyMessage(mes); } }