#include "client.h"
#include "ui_client.h"
Client::Client(QString qq, QString nickName, QString ipv4) :
ui(new Ui::Client)
{
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
ui->setupUi(this);
this->setWindowTitle(tr("来聊"));
ui->nameLabel->setText(nickName+"("+qq+")");
ui->comboBox->setCurrentIndex(0);
ui->toolButton_tx->setIcon(QIcon(":/icons/icon/icq_online.jpg"));
firm_list.clear();
id_list.clear();
chat_list.clear();
this->nickName = nickName;
this->ipv4 = ipv4;
this->qq = qq;
f_qq="";
f_name="";
search = new Search(ipv4, qq, nickName);
//网络服务器设定
tcpPort = 60001;
udpPort = 44444;
//address = QHostAddress::LocalHost;
address=DOMAIN;
clientSocket = new QTcpSocket(this);
serverSocket = new QTcpSocket(this);
udpSocket = new QUdpSocket(this);
udpSocket->bind(udpPort, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
server = new QTcpServer(this);
server->listen(QHostAddress::Any, 54545);
nextBlockSize = 0;
clientSocket->abort();
clientSocket->connectToHost(address, tcpPort);
//把自己添加为好友,防止自己添加自己
addNewFriendToList(ipv4, qq, nickName);
//////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
//定时器
connect(this, SIGNAL(toQuit()), this, SLOT(sendOffLineSlot()));
connect(ui->searchBtn, SIGNAL(clicked()), this, SLOT(searchFormSlot()));
//信号和槽
connect(server, SIGNAL(newConnection()), this, SLOT(newConnectionSlot()));
connect(ui->treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)),
this, SLOT(showChatForm(QTreeWidgetItem*, int)));
//connect(ui->treeWidget,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(showMenu()));
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readMessageSlot()));
connect(clientSocket, SIGNAL(connected()), this, SLOT(timerToCheckConnection()));
}
Client::~Client()
{
delete ui;
}
void Client::closeEvent(QCloseEvent *event)
{
qDebug()<<"I have closed";
emit toQuit();
sendOffLineSlot();
event->accept();
qApp->quit();
}
//心跳去测试是否还连接
void Client::timerToCheckConnection()
{
timer = new QTimer();
timer->start(3 * 1000);
timerID = timer->timerId();
connect(timer, SIGNAL(timeout()), this, SLOT(checkConnectionStateSLot()));
}
void Client::newConnectionSlot()
{
serverSocket = server->nextPendingConnection();
connect(serverSocket, SIGNAL(readyRead()), this, SLOT(readTCPMessageSlot()));
}
void Client::readTCPMessageSlot()
{
nextBlockSize=0;
QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
QDataStream in(serverSocket);
in.setVersion(QDataStream::Qt_4_8);
if (nextBlockSize == 0) {
if (serverSocket->bytesAvailable() < sizeof(quint16)) return;
in >> nextBlockSize;
}
if (serverSocket->bytesAvailable() < nextBlockSize) return;
quint8 type;
in >> type;
qDebug() << "tcp type = " << type;
////////////////////////////ZHELI ///////////////////////////
// QByteArray block;
// QDataStream out(&block, QIODevice::WriteOnly);
// out.setVersion(QDataStream::Qt_4_8);
// out << quint16(0) << quint8(ADD_FRIEND) << nickName << qq << ipv4;
// out.device()->seek(0);
// out << quint16(block.size() - sizeof(quint16));
// clientSocket->write(block);
/////////////////////////////////////////////////////////////////////
QSound sys("../sound/system.wav");
switch (type) {
case ADD_REQUEST:
sys.play();
in >> ip_s >> qq_s >> nickname_s;
if (QMessageBox::Yes == QMessageBox::question(this,
tr("添加好友提醒"),
tr("%1 想和你成为好友,您是否接受?").arg(ip_s),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes)) {
sendAddResult(quint8(AGREED));
//添加好友
addNewFriendToList(ip_s, qq_s, nickname_s);
} else {
sendAddResult(quint8(DENIED));
}
break;
default:
break;
}
}
void Client::readMessageSlot()
{
QByteArray datagram;
do {
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size());
} while (udpSocket->hasPendingDatagrams());
QDataStream in(&datagram, QIODevice::ReadOnly);
in.setVersion(QDataStream::Qt_4_8);
int type;
QString qq_in, name_in, message;
QString qq_to, name_to;
in >> type;
QSound msg("../sound/msg.wav");
QSound nudge("../sound/shake.wav");
switch (type) {
case Message:
msg.play();
in >> qq_in >> name_in >> qq_to >> name_to >> message;
openRelevantChatForm(name_in, qq_in, message);
break;
case Nudge:
nudge.play();
in >> qq_in >> name_in;
nudgeRelevantChatForm(name_in, qq_in);
break;
default:
break;
}
}
//打开聊天窗口
void Client::openRelevantChatForm(QString name_sender, QString qq_sender, QString mes)
{
bool nflag = false;
int n = ui->treeWidget->topLevelItemCount();
for (int i = 0; i < id_list.size(); i++) {
if (id_list.at(i) == qq_sender){
nflag = true;
break;
}
}
if (nflag == true){
for (int i = 0; i < n; i++) {
if (firm_list.at(i) == qq_sender) {
chat_list.at(i)->messagefirmSlot(qq_sender, mes, name_sender);
}
}
} else {
id_list << qq_sender;
for (int i = 0; i < n; i++) {
if (firm_list.at(i) == qq_sender) {
chat_list.at(i)->show();
chat_list.at(i)->messagefirmSlot(qq_sender, mes, name_sender);
connect(chat_list.at(i), SIGNAL(closebutton(QString)), this, SLOT(outputlist(QString)));
}
}
}
}
//震动窗口
void Client::nudgeRelevantChatForm(QString name_sender, QString qq_sender)
{
bool nflag = false;
int n = ui->treeWidget->topLevelItemCount();
for (int i = 0; i < id_list.size(); i++) {
if (id_list.at(i) == qq_sender){
nflag = true;
break;
}
}
if (nflag == true){
for (int i = 0; i < n; i++) {
if (firm_list.at(i) == qq_sender) {
chat_list.at(i)->getWindowAnimation(name_sender);
}
}
} else {
id_list << qq_sender;
for (int i = 0; i < n; i++) {
if (firm_list.at(i) == qq_sender) {
chat_list.at(i)->show();
chat_list.at(i)->getWindowAnimation(name_sender);
connect(chat_list.at(i), SIGNAL(closebutton(QString)), this, SLOT(outputlist(QString)));
}
}
}
}
//发送添加信息
void Client::sendAddResult(quint8 res)
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_8);
out << quint16(0) << res << ipv4 << qq << nickName;
out.device()->seek(0);
out << quint16(block.size() - sizeof(quint16));
serverSocket->write(block);
}
//发送用户上线后的状态信息
void Client::sendOnlineSlot()
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_8);
out << quint16(0) << quint8(LOG_IN) << nickName << qq << ipv4;
out.device()->seek(0);
out << quint16(block.size() - sizeof(quint16));
clientSocket->write(block);
ui->toolButton_tx->setIcon(QIcon(":/icons/icon/icq_online.jpg"));
}
//发送用户下线后的状态信息
void Client::sen
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
基于Qt开发的IM即时通讯软件 (101个子文件)
EmailRegister.aspx 6KB
VerifyCode.aspx 408B
Web.config 2KB
Web.config 1KB
client.cpp 13KB
chat.cpp 12KB
search.cpp 6KB
clientsocket.cpp 5KB
transfer.cpp 4KB
login.cpp 4KB
server.cpp 3KB
download.cpp 3KB
dbhelper.cpp 696B
main.cpp 377B
main.cpp 204B
DBHelper.cs 11KB
Users.cs 11KB
VerificationCode.cs 3KB
EmailRegister.aspx.cs 1KB
VerifyCode.aspx.cs 524B
MainPage.master.cs 277B
style.css 6KB
Plane.css 4KB
site.css 4KB
Login.css 4KB
Register.css 3KB
Index.css 3KB
Train.css 2KB
Travel.css 2KB
ui_server.h 4KB
client.h 2KB
chat.h 2KB
clientsocket.h 1KB
search.h 1KB
transfer.h 963B
login.h 952B
download.h 941B
ui_clientsocket.h 925B
server.h 825B
constant.h 446B
constant.h 415B
dbhelper.h 197B
icq_online.jpg 11KB
icq_offline.jpg 9KB
jquery-1.7.1.js 251KB
jquery-1.7.1.intellisense.js 150KB
jquery-1.7.1.min.js 92KB
jquery.min.js 89KB
global.js 17KB
jquery.easing.1.3.js 8KB
jquery.tabify.js 704B
piano.m4a 1.95MB
MainPage.master 1KB
Server.png 35KB
msn.png 22KB
Login.png 21KB
Bin(empty).png 19KB
search-icon-hi.png 18KB
Chat-icon.png 17KB
chat.png 16KB
未标题-1.png 16KB
E-Mail.png 16KB
20091201_2a0f6976569659175fd3PEFlPFnrDS1R.png 12KB
20091201_0c5e3cb96bf053f6f8f43xlWxvBTLEnr.png 11KB
Floppy.png 9KB
20091201_b321df1d9e60373380a0IQzepnVIU3HS.png 9KB
20091201_e59eb6d747cef0b2bc89nKJwRb2QIxXh.png 8KB
User.png 8KB
aio_favourite_cell_normal@2x.png 5KB
Header+Search_Offline_Hover@2x.png 4KB
Header+Search_Online_Normal@2x.png 4KB
textcolor.png 4KB
Files.png 3KB
toolbar_shake_hover@2x.png 3KB
toolbar_emoticon_hover@2x.png 3KB
toolbar_screenCapture_hover@2x.png 3KB
toolbar_history_hover@2x.png 3KB
titlebar_function_video_normal@2x.png 2KB
fileopen.png 2KB
titlebar_function_fileTransfer_hover@2x.png 2KB
toolbar_pictures_normal@2x.png 2KB
textbold.png 2KB
textunder.png 2KB
textitalic.png 1KB
aio_pic_anti.png 813B
Lailiao.pro 725B
LaiLiaoServer.pro 589B
icon.qrc 1KB
icon.qrc 94B
chat.ui 9KB
client.ui 5KB
login.ui 4KB
search.ui 4KB
server.ui 4KB
transfer.ui 3KB
download.ui 2KB
LaiLiaoServer.pro.user 17KB
Lailiao.pro.user 17KB
msg.wav 9KB
shake.wav 7KB
共 101 条
- 1
- 2
littlebinbin1992
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页