qt通过websocket和html通信,QT Websocket实现服务器客户端通讯(服务端部分)

接上文QT Websocket实现服务器客户端通讯(客户端部分)web

https://blog.csdn.net/qq_39573345/article/details/80682232

服务器

Serverdialog.cc

websocket

#include "Serverdialog.hpp"

#include

#include

#include

#include

#include

#include

#include

ServerDialog::ServerDialog( QWidget *parent)

: QWidget(parent)

{

//qhboxLayout1部分代码

QLabel* monitorLabel= new QLabel("监听端口");

m_monitorSpinBox = new QSpinBox();

m_monitorSpinBox->setRange(0,65535);

m_startButton = new QPushButton("启动服务");

m_stopButton = new QPushButton("中止服务");

m_stopButton->setEnabled(false);

QHBoxLayout *qhboxLayout1 = new QHBoxLayout;

qhboxLayout1->addWidget(monitorLabel);

qhboxLayout1->addWidget(m_monitorSpinBox);

qhboxLayout1->addWidget(m_startButton);

qhboxLayout1->addWidget(m_stopButton);

//qhboxLayout2部分代码

QLabel* sendLabel=new QLabel("发送消息");

QHBoxLayout *qhboxLayout2 = new QHBoxLayout;

qhboxLayout2->addWidget(sendLabel);

//qhboxLayout3部分代码

m_sendTextedit = new QTextEdit;

m_sendTextedit->setEnabled(false);

m_sendTextedit->setFixedHeight(50);

m_sendButton= new QPushButton("发送");

m_sendButton->setEnabled(false);

m_sendButton->setFixedHeight(50);

QHBoxLayout *qhboxlayout3 = new QHBoxLayout;

qhboxlayout3->addWidget(m_sendTextedit,2);

qhboxlayout3->addWidget(m_sendButton,1);

//qvboxlayout411

QLabel* receiveLabel = new QLabel("接收消息");

m_receiveTextEdit = new QTextEdit();

m_receiveTextEdit->setReadOnly(true);

QVBoxLayout *qvboxlayout411 = new QVBoxLayout;

qvboxlayout411->addWidget(receiveLabel);

qvboxlayout411->addWidget(m_receiveTextEdit);

//qhboxlayout412

m_cleanButton = new QPushButton("清除");

m_cleanButton->setEnabled(false);

QHBoxLayout *qhboxlayout412 = new QHBoxLayout;

qhboxlayout412->addStretch();

qhboxlayout412->addWidget(m_cleanButton);

//qvboxlayout41

QVBoxLayout *qvboxlayout41 = new QVBoxLayout;

qvboxlayout41->addLayout(qvboxlayout411);

qvboxlayout41->addLayout(qhboxlayout412);

//qvboxlayout42

QLabel* linkclientLabel=new QLabel("链接客户端");

m_linkclientListWidget=new QListWidget;

QVBoxLayout* qvboxlayout42 = new QVBoxLayout;

qvboxlayout42->addWidget(linkclientLabel);

qvboxlayout42->addWidget(m_linkclientListWidget);

//qvboxlayout4

QHBoxLayout *qhboxlayout4 = new QHBoxLayout;

qhboxlayout4->addLayout(qvboxlayout41,2);

qhboxlayout4->addLayout(qvboxlayout42,1);

//mainlayout

QVBoxLayout *mainLayout = new QVBoxLayout;

mainLayout->addLayout(qhboxLayout1,1);

mainLayout->addLayout(qhboxLayout2,0.5);

mainLayout->addLayout(qhboxlayout3,1);

mainLayout->addLayout(qhboxlayout4,3);

this->setLayout(mainLayout);

this->setWindowTitle("Websocket Server");

m_WebSocketServer=new QWebSocketServer("server",QWebSocketServer::NonSecureMode);

connect(m_WebSocketServer,SIGNAL(newConnection()),this,SLOT(onNewConnection()));

connect(m_startButton,SIGNAL(clicked(bool)),this,SLOT(onStartButtonClick()));

connect(m_stopButton,SIGNAL(clicked(bool)),this,SLOT(onStopButtonClick()));

connect(m_cleanButton,SIGNAL(clicked(bool)),this,SLOT(onCleanButtonClick()));

connect(m_sendButton,SIGNAL(clicked(bool)),this,SLOT(onSendButtonClick()));

}

ServerDialog::~ServerDialog()

{

}

void ServerDialog::onStartButtonClick(){

int i_port = m_monitorSpinBox->text().toInt();

m_WebSocketServer->listen(QHostAddress::Any,i_port);

m_startButton->setEnabled(false);

m_stopButton->setEnabled(true);

qDebug()<isListening();

qDebug()<serverPort();

qDebug()<serverAddress();

}

void ServerDialog::onStopButtonClick(){

m_startButton->setEnabled(true);

m_stopButton->setEnabled(false);

m_WebSocketServer->close();

}

void ServerDialog::onSendButtonClick(){

QString msg = m_sendTextedit->document()->toPlainText();

pSocket->sendTextMessage(msg);

}

void ServerDialog::onCleanButtonClick(){

m_receiveTextEdit->clear();

}

//链接上以后

void ServerDialog::onNewConnection(){

qDebug() << "hello";

m_startButton->setEnabled(false);

m_stopButton->setEnabled(true);

m_sendTextedit->setEnabled(true);

m_sendButton->setEnabled(true);

m_cleanButton->setEnabled(true);

pSocket = m_WebSocketServer->nextPendingConnection();

connect(pSocket,SIGNAL(textMessageReceived(QString)),this,SLOT(processTextMessage(QString)));

connect(pSocket,SIGNAL(disconnected()),this,SLOT(socketDisconnected()));

QString item = pSocket->peerAddress().toString();

m_linkclientListWidget->addItem(item);

m_clients << pSocket;

}

//收到消息并显示

void ServerDialog::processTextMessage(QString message){

QString time = current_date_time->currentDateTime().toString("yyyy.MM.dd hh:mm:ss.zzz ddd");

QString item = pSocket->peerAddress().toString();

m_receiveTextEdit->append(time +""+ item + "\n" + message);

}

//链接断开的操做

void ServerDialog::socketDisconnected(){

m_startButton->setEnabled(true);

m_stopButton->setEnabled(false);

m_sendButton->setEnabled(false);

m_sendTextedit->setEnabled(false);

m_receiveTextEdit->setEnabled(false);

m_cleanButton->setEnabled(false);

}Serverdialog.hpp

#ifndef WEBSOCKEDSERVERDIALOG_HPP

#define WEBSOCKEDSERVERDIALOG_HPP

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include "qwebsocketserver.hpp"

#include "qwebsocket.hpp"

#include

class ServerDialog : public QWidget

{

Q_OBJECT

public:

explicit ServerDialog(QWidget *parent = 0);

~ServerDialog();

public slots:

void onStartButtonClick();

void onStopButtonClick();

void onSendButtonClick();

void onCleanButtonClick();

Q_SIGNALS:

void closed();

private Q_SLOTS:

void onNewConnection();

void processTextMessage(QString message);

void socketDisconnected();

//void processBinaryMessage(QByteArray message);

private:

QSpinBox* m_monitorSpinBox;

QPushButton* m_startButton;

QPushButton* m_stopButton;

QTextEdit* m_sendTextedit;

QPushButton* m_sendButton;

QListWidget* m_linkclientListWidget;

QPushButton* m_cleanButton;

QTextEdit* m_receiveTextEdit;

QWebSocketServer * m_WebSocketServer;

QList m_clients;

bool m_debug;

QWebSocket *pSocket;

QDateTime *current_date_time;

};

#endif // WEBSOCKEDSERVERDIALOG_HPP

main.ccapp

#include "Serverdialog.hpp"

#include

#include

#include

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

QDir::setCurrent(QApplication::applicationDirPath());

QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));//set QString codec

ServerDialog w;

w.show();

return a.exec();

}WeebsockedServer.pro 这里建立项目的时候多打了个e,不过不影响=.=

#-------------------------------------------------

#

# Project created by QtCreator 2018-05-10T10:16:25

#

#-------------------------------------------------

QT += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = WeebsockedServer

TEMPLATE = app

# The following define makes your compiler emit warnings if you use

# any feature of Qt which has been marked as deprecated (the exact warnings

# depend on your compiler). Please consult the documentation of the

# deprecated API in order to know how to port your code away from it.

DEFINES += QT_DEPRECATED_WARNINGS QT_VERSION_LESS_FIVE

INCLUDEPATH += $$PWD/../QtWebsocket

# You can also make your code fail to compile if you use deprecated APIs.

# In order to do so, uncomment the following line.

# You can also select to disable deprecated APIs only up to a certain version of Qt.

#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \

main.cc \

Serverdialog.cc

HEADERS += \

Serverdialog.hpp

DESTDIR = $$PWD/../Bin

LIBS +=-L$$DESTDIR -lQtWebsocket

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值