使⽤QtQWebSocketServer与QWebSocket写的聊天室(c++)使⽤Qwebsocket和Qwebsocketserver写的聊天室 借鉴了SpecofDust⼤神的代码。
服务器代码
Serverdialog.h
#pragma once
#include<QtWidgets/QWidget>
#include<QWebSocketServer>
#include<QWebSocket>
#include<QHash>
#include<QString>
#include"ui_Serverdialog.h"
class Serverdialog :public QWidget
{
Q_OBJECT
public:
Serverdialog(QWidget *parent = Q_NULLPTR);
private Q_SLOTS:
void onNewConnection();//客户端连接后执⾏槽
void processTextMessage(QString message);//客户端发送信息后处理槽
void socketDisconnected();//客户端断开后槽
void huoqumingzi(QByteArray message);//客户端断开后服务器所要处理的事情
Q_SIGNALS:
void closed();
private:
Ui::ServerdialogClass ui;
int i;
QString mingzi;
QString mingzi1;
QString name1;//换⾏需要
QMetaObject::Connection Connection1;
QWebSocketServer* m_WebSocketServer;
QHash <QString, QWebSocket*> m_clients;
bool m_debug;
QWebSocket* pSocket;
QDateTime* current_date_time;
private slots:
void onStartButtonClick();//UI界⾯开始按钮槽
void onStopButtonClick();//UI界⾯关闭按钮槽
void onSendButtonClick();//UI界⾯发送信息按钮槽
void SetName(QString name);//存储客户端信息函数
void listRowChanged(int row);//私法选择槽
};
Serverdialog.cpp
#include"Serverdialog.h"
#include<QWebSocketServer>
#include<QMetaObject>
Serverdialog::Serverdialog(QWidget *parent)
:QWidget(parent)
{
i =0;
m_WebSocketServer =new QWebSocketServer("server", QWebSocketServer::NonSecureMode);
connect(m_WebSocketServer,SIGNAL(newConnection()),this,SLOT(onNewConnection()));
ui.setupUi(this);
}
void Serverdialog::onStartButtonClick(){
connect(ui.m_linkclientListWidget,SIGNAL(currentRowChanged(int)),this,SLOT(listRowChanged(int)));//私聊int i_port = ui.m_monitorSpinBox->text().toInt();
m_WebSocketServer->listen(QHostAddress::Any, i_port);
ui.m_startButton->setEnabled(false);
ui.m_stopButton->setEnabled(true);
qDebug()<< m_WebSocketServer->isListening();
qDebug()<< m_WebSocketServer->serverPort();
qDebug()<< m_WebSocketServer->serverAddress();
}
void Serverdialog::onNewConnection(){
qDebug()<<"hello";
i++;
ui.zaixianNumber->display(i);
ui.m_startButton->setEnabled(false);
ui.m_stopButton->setEnabled(true);
ui.m_sendTextedit->setEnabled(true);
ui.m_sendButton->setEnabled(true);
pSocket = m_WebSocketServer->nextPendingConnection();///
connect(pSocket,SIGNAL(disconnected()),this,SLOT(socketDisconnected()));
Connection1 =connect(pSocket,SIGNAL(textMessageReceived(QString)),this,SLOT(SetName(QString))); }
void Serverdialog::onStopButtonClick(){
ui.m_startButton->setEnabled(true);
ui.m_stopButton->setEnabled(false);
disconnect(ui.m_linkclientListWidget,SIGNAL(currentRowChanged(int)),this,SLOT(listRowChanged(int)));
ui.m_linkclientListWidget->clear();
ui.m_linkclientListWidget->addItem("hall");
ui.zaixianNumber->display(0);
m_WebSocketServer->close();
}
void Serverdialog::SetName(QString name){
mingzi1 = name;
ui.m_linkclientListWidget->addItem(name);
disconnect(Connection1);
connect(pSocket,SIGNAL(textMessageReceived(QString)),this,SLOT(processTextMessage(QString))); connect(pSocket,SIGNAL(binaryMessageReceived(QByteArray)),this,SLOT(huoqumingzi(QByteArray)));/// m_clients.insert(mingzi1, pSocket);
QHash<QString, QWebSocket*>::const_iterator mi;
for(mi = stBegin(); mi != stEnd(); mi++){
QString str = QString::number(i);
QByteArray bytes = Utf8();
mi.value()->sendBinaryMessage(bytes);
}
QHash<QString, QWebSocket*>::const_iterator mi11;
for(mi11 = stBegin(); mi11 != stEnd(); mi11++){
QString s;
QHash<QString, QWebSocket*>::const_iterator mi2;
for(mi2 = stBegin(); mi2 != stEnd(); mi2++){
if(mi2 != stBegin())
s = s +"/"+ mi2.key();
else
s = mi2.key();
}
mi11.value()->sendTextMessage(s);
}
}
void Serverdialog::onSendButtonClick(){
QString msg = ui.m_sendTextedit->document()->toPlainText();
QString key = name1;
QString time = current_date_time->currentDateTime().toString("yyyy.MM.dd hh: ddd");
if(name1 =="hall"){
QHash<QString, QWebSocket*>::const_iterator mi12;
for(mi12 = stBegin(); mi12 != stEnd(); mi12++){
mi12.value()->sendTextMessage(time +"server:"+"\n"+ msg);
}
}
else if(ains(key))
{
m_clients.value(key)->sendTextMessage(time +"server:"+"\n"+ msg);
}
}
void Serverdialog::processTextMessage(QString message){
QString str;
str = message.section(",",1,1);
QHash<QString, QWebSocket*>::iterator mi;
mi = m_clients.find(str);
if(mi != d())
mi.value()->sendTextMessage(message);
else{
QHash<QString, QWebSocket*>::const_iterator mi12;
for(mi12 = stBegin(); mi12 != stEnd(); mi12++){
mi12.value()->sendTextMessage(message);
}
}
ui.m_receiveTextEdit->append(message);
}
void Serverdialog::socketDisconnected(){
ui.m_startButton->setEnabled(false);
ui.m_stopButton->setEnabled(true);
ui.m_sendButton->setEnabled(true);
ui.m_sendTextedit->setEnabled(true);
ui.m_receiveTextEdit->setEnabled(true);
}
void Serverdialog::huoqumingzi(QByteArray message){
disconnect(ui.m_linkclientListWidget,SIGNAL(currentRowChanged(int)),this,SLOT(listRowChanged(int))); ui.m_linkclientListWidget->clear();
ui.m_linkclientListWidget->addItem("hall");
connect(ui.m_linkclientListWidget,SIGNAL(currentRowChanged(int)),this,SLOT(listRowChanged(int))); mingzi = message;
ve(mingzi);
i--;
QHash<QString, QWebSocket*>::const_iterator mi1;//迭代器
for(mi1 = stBegin(); mi1 != stEnd(); mi1++){
QString str = QString::number(i);
QByteArray bytes = Utf8();
mi1.value()->sendBinaryMessage(bytes);// 1 2 3 4 5 6 7
}
//哈希表 key value
ui.zaixianNumber->display(i);
QHash<QString, QWebSocket*>::const_iterator mi11;
for(mi11 = stBegin(); mi11 != stEnd(); mi11++){
QString s;
QHash<QString, QWebSocket*>::const_iterator mi2;
for(mi2 = stBegin(); mi2 != stEnd(); mi2++){
if(mi2 != stBegin())
s = s +"/"+ mi2.key();
else
s = mi2.key();
}
mi11.value()->sendTextMessage(s);
}
QHash<QString, QWebSocket*>::const_iterator mi;
for(mi = stBegin(); mi != stEnd(); mi++){
QString mz = mi.key();
ui.m_linkclientListWidget->addItem(mz);
}
void Serverdialog::listRowChanged(int row){
name1 = ui.m_linkclientListWidget->item(row)->text();
}
ui_Serverdialog.h ///这⼀部分使⽤QT设计师⽣成的(包括部件名称等等) /********************************************************************************
** Form generated from reading UI file 'Serverdialog.ui'
**
** Created by: Qt User Interface Compiler version 5.9.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_SERVERDIALOG_H
#define UI_SERVERDIALOG_H
#include<QtCore/QVariant>
#include<QtWidgets/QAction>
#include<QtWidgets/QApplication>
#include<QtWidgets/QButtonGroup>
#include<QtWidgets/QHBoxLayout>
#include<QtWidgets/QHeaderView>
#include<QtWidgets/QLCDNumber>
#include<QtWidgets/QLabel>
#include<QtWidgets/QListWidget>
#include<QtWidgets/QPushButton>
#include<QtWidgets/QSpinBox>
#include<QtWidgets/QTextEdit>
#include<QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_ServerdialogClass
{
public:
QLabel *sendLabel;
QTextEdit *m_sendTextedit;
QPushButton *m_sendButton;
QLabel *receiveLabel;
QTextEdit *m_receiveTextEdit;
QLabel *linkclientLabel;
QListWidget *m_linkclientListWidget;
QWidget *layoutWidget;
QHBoxLayout *horizontalLayout;
QLabel *monitorLabel;
QSpinBox *m_monitorSpinBox;
QPushButton *m_startButton;
QPushButton *m_stopButton;
QWidget *layoutWidget1;
QHBoxLayout *horizontalLayout_2;
QLabel *zaixianlab;
QLCDNumber *zaixianNumber;
void setupUi(QWidget *ServerdialogClass)
{
if(ServerdialogClass->objectName().isEmpty())
ServerdialogClass->setObjectName(QStringLiteral("ServerdialogClass"));
ServerdialogClass->resize(573,400);
sendLabel =new QLabel(ServerdialogClass);
sendLabel->setObjectName(QStringLiteral("sendLabel"));
websocket和socketsendLabel->setGeometry(QRect(20,80,72,15));
m_sendTextedit =new QTextEdit(ServerdialogClass);
m_sendTextedit->setObjectName(QStringLiteral("m_sendTextedit"));
m_sendTextedit->setGeometry(QRect(20,100,331,41));
m_sendButton =new QPushButton(ServerdialogClass);
m_sendButton->setObjectName(QStringLiteral("m_sendButton"));
m_sendButton->setGeometry(QRect(380,100,171,41));
receiveLabel =new QLabel(ServerdialogClass);
receiveLabel->setObjectName(QStringLiteral("receiveLabel"));
receiveLabel->setGeometry(QRect(20,150,72,15));
m_receiveTextEdit =new QTextEdit(ServerdialogClass);
m_receiveTextEdit->setObjectName(QStringLiteral("m_receiveTextEdit"));
m_receiveTextEdit->setGeometry(QRect(20,170,331,221));
linkclientLabel =new QLabel(ServerdialogClass);
linkclientLabel->setObjectName(QStringLiteral("linkclientLabel"));
linkclientLabel->setGeometry(QRect(380,150,81,16));
m_linkclientListWidget =new QListWidget(ServerdialogClass);
new QListWidgetItem(m_linkclientListWidget);
m_linkclientListWidget->setObjectName(QStringLiteral("m_linkclientListWidget")); m_linkclientListWidget->setGeometry(QRect(380,170,171,221));
layoutWidget =new QWidget(ServerdialogClass);
layoutWidget->setObjectName(QStringLiteral("layoutWidget"));
layoutWidget->setGeometry(QRect(20,10,541,30));
horizontalLayout =new QHBoxLayout(layoutWidget);
horizontalLayout->setSpacing(6);
horizontalLayout->setContentsMargins(11,11,11,11);
horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
horizontalLayout->setContentsMargins(0,0,0,0);
monitorLabel =new QLabel(layoutWidget);
monitorLabel->setObjectName(QStringLiteral("monitorLabel"));
horizontalLayout->addWidget(monitorLabel);
m_monitorSpinBox =new QSpinBox(layoutWidget);
m_monitorSpinBox->setObjectName(QStringLiteral("m_monitorSpinBox"));
horizontalLayout->addWidget(m_monitorSpinBox);
m_startButton =new QPushButton(layoutWidget);
m_startButton->setObjectName(QStringLiteral("m_startButton"));
horizontalLayout->addWidget(m_startButton);
m_stopButton =new QPushButton(layoutWidget);
m_stopButton->setObjectName(QStringLiteral("m_stopButton"));
horizontalLayout->addWidget(m_stopButton);
layoutWidget1 =new QWidget(ServerdialogClass);
layoutWidget1->setObjectName(QStringLiteral("layoutWidget1"));
layoutWidget1->setGeometry(QRect(20,50,133,25));
horizontalLayout_2 =new QHBoxLayout(layoutWidget1);
horizontalLayout_2->setSpacing(6);
horizontalLayout_2->setContentsMargins(11,11,11,11);
horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
horizontalLayout_2->setContentsMargins(0,0,0,0);
zaixianlab =new QLabel(layoutWidget1);
zaixianlab->setObjectName(QStringLiteral("zaixianlab"));
horizontalLayout_2->addWidget(zaixianlab);
zaixianNumber =new QLCDNumber(layoutWidget1);
zaixianNumber->setObjectName(QStringLiteral("zaixianNumber"));
horizontalLayout_2->addWidget(zaixianNumber);
retranslateUi(ServerdialogClass);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论