生活随笔
收集整理的這篇文章主要介紹了
登录功能和公聊功能的实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1 登錄功能和公聊功能的實(shí)現(xiàn)
- 1.1 分析
- 1.2 代碼實(shí)現(xiàn)
1 登錄功能和公聊功能的實(shí)現(xiàn)
1.1 分析
問題:
- 如何設(shè)計(jì)客戶端和服務(wù)端之間的消息交互流程?
消息類型主要分為如下兩種:
-
控制消息:
- 與聊天內(nèi)容無關(guān),設(shè)計(jì)用戶狀態(tài)。
- 連接、斷開、踢人、禁言等。
-
文本消息:
- 與聊天內(nèi)容相關(guān),用戶可見消息。
- 公聊消息,私聊消息。
用戶登錄、注冊流程:
服務(wù)端認(rèn)證流程:
簡化設(shè)計(jì):
文本消息傳輸模型(公聊模式):
文本消息傳輸模型(私聊模式):
1.2 代碼實(shí)現(xiàn)
為了能夠正確解析多條一次性接收的數(shù)據(jù),需要對onDataReady()函數(shù)進(jìn)行修改:
客戶端中修改:
MainWin.h:
MainWinSlot.cpp修改后如下:
#include "MainWin.h"
#include <QMessageBox>
#include <QDebug>void MainWin
::initMember()
{m_client
.setHandler(this
);m_handlerMap
.insert("CONN", CONN_Handler
);m_handlerMap
.insert("DSCN", DSCN_Handler
);m_handlerMap
.insert("LIOK", LIOK_Handler
);m_handlerMap
.insert("LIER", LIER_Handler
);m_handlerMap
.insert("MSGA", MSGA_Handler
);
}void MainWin
::sendBtnClicked()
{QString text
= inputGrpBx
.title() + ":\n" + " " + inputEdit
.text() + "\n";TextMessage
tm("MSGA", text
);if( m_client
.send(tm
) ){inputEdit
.clear();}
}void MainWin
::logInOutBtnClicked()
{if( !m_client
.isValid() ){if( loginDlg
.exec() == QDialog
::Accepted
){QString usr
= loginDlg
.getUser().trimmed();QString pwd
= loginDlg
.getPwd();if( m_client
.connectTo("127.0.0.1", 8890) ){TextMessage
tm("LGIN", usr
+ '\r' + pwd
);m_client
.send(tm
);}else{QMessageBox
::critical(this
, "失敗", "連接不到遠(yuǎn)程服務(wù)端!");}}}else{m_client
.close();}
}void MainWin
::handle(QTcpSocket
& obj
, TextMessage
& message
)
{if( m_handlerMap
.contains(message
.type()) ){MSGHandler handler
= m_handlerMap
.value(message
.type());(this
->*handler
)(obj
, message
);}
}void MainWin
::CONN_Handler(QTcpSocket
&, TextMessage
&)
{}void MainWin
::MSGA_Handler(QTcpSocket
&, TextMessage
& message
)
{msgEditor
.appendPlainText(message
.data());
}void MainWin
::DSCN_Handler(QTcpSocket
&, TextMessage
&)
{setCtrlEnabled(false
);inputGrpBx
.setTitle("用戶名");
}void MainWin
::LIOK_Handler(QTcpSocket
&, TextMessage
& message
)
{setCtrlEnabled(true
);inputGrpBx
.setTitle(message
.data());
}void MainWin
::LIER_Handler(QTcpSocket
&, TextMessage
&)
{QMessageBox
::critical(this
, "錯(cuò)誤", "身份驗(yàn)證失敗!");m_client
.close();
}MainWin
::~MainWin()
{m_client
.close();
}
服務(wù)端代碼進(jìn)行如下修改:
ServerHandler.h修改如下:
ServerHandler.cpp修改后的內(nèi)容如下:
#include "ServerHandler.h"
#include <QDebug>ServerHandler
::ServerHandler()
{m_handlerMap
.insert("CONN", CONN_Handler
);m_handlerMap
.insert("DSCN", DSCN_Handler
);m_handlerMap
.insert("LGIN", LGIN_Handler
);m_handlerMap
.insert("MSGA", MSGA_Handler
);
}void ServerHandler
::handle(QTcpSocket
& obj
, TextMessage
& message
)
{if( m_handlerMap
.contains(message
.type()) ){MSGHandler handler
= m_handlerMap
.value(message
.type());(this
->*handler
)(obj
, message
);}
}void ServerHandler
::CONN_Handler(QTcpSocket
&, TextMessage
&)
{}void ServerHandler
::DSCN_Handler(QTcpSocket
& obj
, TextMessage
&)
{for(int i
=0; i
<m_nodeList
.length(); i
++){Node
* n
= m_nodeList
.at(i
);if( n
->socket
== &obj
){n
->socket
= NULL;break;}}
}void ServerHandler
::MSGA_Handler(QTcpSocket
&, TextMessage
& message
)
{const QByteArray
& ba
= message
.serialize();for(int i
=0; i
<m_nodeList
.length(); i
++){Node
* n
= m_nodeList
.at(i
);if( n
->socket
!= NULL ){n
->socket
->write(ba
);}}
}void ServerHandler
::LGIN_Handler(QTcpSocket
& obj
, TextMessage
& message
)
{QString data
= message
.data();int index
= data
.indexOf('\r');QString id
= data
.mid(0, index
);QString pwd
= data
.mid(index
+ 1);QString result
= "";index
= -1;for(int i
=0; i
<m_nodeList
.length(); i
++){if( id
== m_nodeList
.at(i
)->id
){index
= i
;break;}}if( index
== -1 ){Node
* newNode
= new
Node();if( newNode
!= NULL ){newNode
->id
= id
;newNode
->pwd
= pwd
;newNode
->socket
= &obj
;m_nodeList
.append(newNode
);result
= "LIOK";}else{result
= "LIER";}}else{Node
* n
= m_nodeList
.at(index
);if( pwd
== n
->pwd
){n
->socket
= &obj
;result
= "LIOK";}else{result
= "LIER";}}obj
.write(TextMessage(result
, id
).serialize());
}
參考資料:
QT實(shí)驗(yàn)分析教程
總結(jié)
以上是生活随笔為你收集整理的登录功能和公聊功能的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。