?
?
void widget::sendMsg(widget::MsgType type)
{
QByteArray array;
QDataStream stream(&array,QIODevice::WriteOnly);
stream<<type<<getUsr();
switch (type) {
case Msg:
if(ui->msgTextEdit->toPlainText()=="")
{
QMessageBox::warning(this,"警告","发送内容不能为空");
return;
}
stream<<getMsg();
break;
case UsrEnter:
break;
case UsrLeft:
break;
default:
break;
}
udpSocket->writeDatagram(array,QHostAddress::Broadcast,port);
}
void widget::ReceiveMessage()
{
qint64 size = udpSocket->pendingDatagramSize();
QByteArray array= QByteArray(size,0);
udpSocket->readDatagram(array.data(),size);
QDataStream stream(&array,QIODevice::ReadOnly);
int msgType;
QString usrName;
QString msg;
stream>>msgType;
QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
switch (msgType) {
case Msg:
{
stream>>usrName>>msg;
ui->msgBrowser->setTextColor(Qt::blue);
ui->msgBrowser->append("[" +usrName+ "]"+time);
ui->msgBrowser->append(msg);
break;
}
case UsrEnter:
stream>>usrName;
userEnter(usrName);
break;
case UsrLeft:
stream>>usrName;
userLeft(usrName,time);
break;
default:
break;
}
}
?
|