目录
前言
一、服务器的代码:
1、服务器的思路
2、具体服务器的代码示例
二、客户端的代码:
1、客户端的思路(和服务器类似)
2、具体客户端的代码示例
要是想了解QWebSocket的详细知识,还得移步到上一篇文章:
WebSocket 详解,以及用QWebSocket 实现服务端和客户端(含代码例子)-CSDN博客WebSocket 详解,以及用QWebSocket 实现服务端和客户端(含代码例子)-CSDN博客
本篇文章主要讲解如何利用QWebSocket 实现服务和客户之间的通讯
(1)首先创建了一个服务器的基类,主要实现了服务类的基本接口:
1、创建服务器:new QWebSocketServer
2、监听:listen
m_pWebSocketServer->listen(QHostAddress::LocalHost, mPort);//端口号
3、有新的连接,收到这个信号:QWebSocketServer::newConnection
4、获得新的客户端:nextPendingConnection
5、接收到信息时候,收到信号:QWebSocket::binaryMessageReceived
6、断开连接,收到信号:QWebSocket::disconnected注意:数据的接收和发送,有两种格式,二进制和文本的,具体按照实际需要的来选择;
(2)在服务器的基类上,封装一个具体使用的类,这个类,主要是添加了QThread,创建一个子线程来进行服务器的开启,监听和接收数据,不会影响主线程的事件。
接收和发送的数据,以二进制为例
(1)服务器基类:
服务器基类的头文件:
1)开启一个端口号为“9000”的服务器
2)监听的网路是:QHostAddress::Any
QHostAddress::Any表示服务端监听所有可用的网络接口。
它是一个特殊的IP地址,表示服务端可以接受来自任何IP地址的连接请求。
这通常用于在一个计算机上运行多个网络服务时,让服务端能够监听所有可用的网络接口,以便接受来自不同网络接口的连接请求。
注意:也可以监听具体的IP地址:
例如:QHostAddress(strLocalHostIp)
#ifndef WEBSOCKETSERVERBASE_H
#define WEBSOCKETSERVERBASE_H
#include
#include
QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
QT_FORWARD_DECLARE_CLASS(QWebSocket)
QT_FORWARD_DECLARE_CLASS(QString)
class WebsocketServerBase : public QObject
{
Q_OBJECT
public:
explicit WebsocketServerBase(QString serverName, quint16 port, QObject *parent = 0);
virtual ~WebsocketServerBase();
signals:
//客户端发来的数据
void sigProcessServerMessage(const QByteArray &data);
public slots:
//发送数据给客户端
void slotSendToAllClients(const QByteArray &data);
//启动websocket服务器
void slotStartServer();
private slots:
//处理新接入的连接
void slotNewConnection();
//处理链接断开的事件
void slotSocketDisconnected();
//接收数据,并转发
void slotProcessBinaryMessage(const QByteArray &message);
public:
//检测是否存在客户端
bool hadClients();
private:
QWebSocketServer *m_pWebSocketServer = nullptr;
QList m_clients;
unsigned short m_nPort = 9000;
QString m_strServerName = "server";
};
#endif // WEBSOCKETSERVERBASE_H
服务器基类的源文件:
#include "websocketserverbase.h"
#include
static QString getIdentifier(QWebSocket *peer)
{
return QStringLiteral("%1:%2").arg(peer->peerAddress().toString(),
peer->origin());
}
WebsocketServerBase::WebsocketServerBase(QString serverName, quint16 port, QObject *parent)
: QObject(parent)
,m_nPort(port)
,m_strServerName(serverName)
{
}
WebsocketServerBase::~WebsocketServerBase()
{
if(m_pWebSocketServer)
{
m_pWebSocketServer->close();
//m_pWebSocketServer->abort();
m_pWebSocketServer->deleteLater();
}
}
//接收到外部发来的信息,转发给客户端
void WebsocketServerBase::slotSendToAllClients(const QByteArray &data)
{
qDebug() << __FUNCTION__;
for (QWebSocket *pClient : qAsConst(m_clients)) {
qDebug() << "data: " << data;
pClient->sendBinaryMessage(data);
}
}
bool WebsocketServerBase::hadClients()
{
return m_clients.size()>0;
}
void WebsocketServerBase::slotStartServer()
{
if(m_pWebSocketServer)
return;
m_pWebSocketServer = new QWebSocketServer(m_strServerName, QWebSocketServer::NonSecureMode, this);
if (m_pWebSocketServer->listen(QHostAddress::Any, m_nPort))
{
connect(m_pWebSocketServer, &QWebSocketServer::newConnection, this, &WebsocketServerBase::slotNewConnection);
qDebug() << "WebSocket is start, port:" << m_nPort;
}
}
void WebsocketServerBase::slotNewConnection()
{
auto pSocket = m_pWebSocketServer->nextPendingConnection();
QTextStream(stdout) << getIdentifier(pSocket) << " connected!\n";
qDebug() << "client connected!";
pSocket->setParent(this);
//二进制数据的接收
connect(pSocket, &QWebSocket::binaryMessageReceived, this, &WebsocketServerBase::slotProcessBinaryMessage);
connect(pSocket, &QWebSocket::disconnected, this, &WebsocketServerBase::slotSocketDisconnected);
m_clients << pSocket;
}
void WebsocketServerBase::slotSocketDisconnected()
{
QWebSocket *pClient = qobject_cast(sender());
QTextStream(stdout) << getIdentifier(pClient) << " disconnected!\n";
if (pClient)
{
m_clients.removeAll(pClient);
pClient->deleteLater();
}
}
//接收客户端发来的数据,并转发出去
void WebsocketServerBase::slotProcessBinaryMessage(const QByteArray &data)
{
qDebug() << __FUNCTION__ << " data:" << data;
emit sigProcessServerMessage(data);
//test
//slotSendToAllClients(data);
}
(2)将服务器基类封装,改造下:(外面可以直接使用这个类进行通讯)
改造后服务器的头文件:
此处本例是使用回调函数将结果抛给上一层调用者,在qt里,完全可以用信号槽代替的。
#ifndef READERWEBSOCKETSERVER_H
#define READERWEBSOCKETSERVER_H
#include
#include "Singleton.h"
// 回调函数,将websocket的结果抛给上层
typedef void(*recvMsgToSerial)(const QByteArray &byteArray);
class WebsocketServerBase;
class ReaderWebsocketServer : public QObject, public Singleton
{
Q_OBJECT
friend class Singleton;
public:
explicit ReaderWebsocketServer(QObject *parent = 0);
virtual ~ReaderWebsocketServer();
public:
// 设置回调函数
void setCallBack(recvMsgToSerial pFunc, void* pUser = NULL);
// 接收串口发来数据,转发给客户端
bool sendData(const QByteArray &byteArray);
signals:
//转发数据给客户端
void sigSendToAllClients(const QByteArray &data);
private slots:
//处理客户端发来的数据,转发给需要的地方
void slotProcessServerMessage(const QByteArray &data);
private:
WebsocketServerBase* m_pWsServer = nullptr;
QThread* m_thdWsServer = nullptr;
void* m_pUser; // 返回回调的对象
recvMsgToSerial m_pRecvMsgToSerial; // 回调
};
#endif // READERWEBSOCKETSERVER_H
改造后服务器的源文件:
#include "readerwebsocketserver.h"
#include
#include
#include "websocketserverbase.h"
ReaderWebsocketServer::ReaderWebsocketServer(QObject *parent)
: QObject(parent)
{
m_thdWsServer = new QThread();
m_pWsServer = new WebsocketServerBase("reader", 9000);
m_pWsServer->moveToThread(m_thdWsServer);
connect(m_pWsServer, &WebsocketServerBase::sigProcessServerMessage, this, &ReaderWebsocketServer::slotProcessServerMessage);
connect(this, &ReaderWebsocketServer::sigSendToAllClients, m_pWsServer, &WebsocketServerBase::slotSendToAllClients);
connect(m_thdWsServer, &QThread::started, m_pWsServer, &WebsocketServerBase::slotStartServer);
connect(m_thdWsServer, &QThread::finished, m_pWsServer, &WebsocketServerBase::deleteLater);
connect(m_thdWsServer, &QThread::finished, m_thdWsServer, &WebsocketServerBase::deleteLater);
m_thdWsServer->start();
}
ReaderWebsocketServer::~ReaderWebsocketServer()
{
if(m_thdWsServer)
{
m_thdWsServer->quit();
m_thdWsServer->wait();
}
if(m_pWsServer)
{
m_pWsServer->deleteLater();
}
}
void ReaderWebsocketServer::setCallBack(recvMsgToSerial pFunc, void *pUser)
{
if (nullptr != pFunc)
m_pRecvMsgToSerial = pFunc;
if (nullptr != pUser)
m_pUser = pUser;
}
//接收串口发来数据,转发给客户端
bool ReaderWebsocketServer::sendData(const QByteArray &byteArray)
{
bool hadBnode = m_pWsServer->hadClients();
if (hadBnode)
{
emit sigSendToAllClients(byteArray);
}
}
//处理客户端发来的数据,转发给需要的地方
void ReaderWebsocketServer::slotProcessServerMessage(const QByteArray &byteArray)
{
qDebug() << __FUNCTION__ ;
m_pRecvMsgToSerial(byteArray);
}
(1)首先创建了一个客户端的基类,主要实现了客户端的基本接口:
(2)在客户端基类上,封装一个具体使用的类:(外面可以直接使用这个类进行通讯)
这个类,主要是添加了QThread 和QTimer,QThread 创建一个子线程来进行服务器的开启,监听和接收数据,不会影响主线程的事件;QTimer主要是发心跳包,实现断开重连机制;
接收和发送的数据,以二进制为例
(1)客户端基类:
客户端基类的头文件:
/*
* @Description: websocket客户端,用于与中间件通信
*/
#pragma once
#include
#include
class QTimer;
class QWebSocket;
class WebSocketBase : public QObject
{
Q_OBJECT
public:
WebSocketBase(QObject *parent = nullptr);
~WebSocketBase();
void setWebSocketUrl(QString strUrl="");
bool getConnectStatus();
int RecvFrom(QByteArray& byteArray);
signals:
void sigClientBinaryMessageReceived(const QByteArray &byteArray); //借用websocket的信号函数
public slots:
void slotCreateDataRecWS();//创建websocket连接
void slotSendBinaryMessageMessage(const QByteArray &byteArray);
void slotReconnect(); /*-<周期重连函数 */
void slotActiveReconnect();
private slots:
void slotOnConnected(); /*-
客户端基类的源文件:
#include "WebSocketBase.h"
#include
#include
#include
#include
WebSocketBase::WebSocketBase(QObject *parent) : QObject(parent)
,m_pDataRecvWS(nullptr)
,m_pTimer(nullptr)
,m_strURL("")
,m_bConnectStatus(false)
,m_byteArray("")
{
}
WebSocketBase::~WebSocketBase()
{
m_pTimer->stop();
m_pTimer->deleteLater();
m_pDataRecvWS->abort();
m_pDataRecvWS->deleteLater();
}
void WebSocketBase::setWebSocketUrl(QString strUrl)
{
m_strURL = strUrl;
if(m_strURL.isEmpty())
{
m_strURL = "127.0.0.1";
}
}
bool WebSocketBase::getConnectStatus()
{
return m_bConnectStatus;
}
int WebSocketBase::RecvFrom(QByteArray &byteArray)
{
byteArray = m_byteArray;
m_byteArray.clear();
return byteArray.size();
}
void WebSocketBase::slotCreateDataRecWS()
{
if(nullptr == m_pTimer)
{
m_pTimer = new QTimer();
}
qDebug() << "Server Address" << m_strURL;
if(m_pDataRecvWS == nullptr)
{
m_pDataRecvWS = new QWebSocket();
connect(m_pDataRecvWS, &QWebSocket::disconnected, this, &WebSocketBase::slotOnDisConnected);
connect(m_pDataRecvWS, &QWebSocket::binaryMessageReceived, this, &WebSocketBase::slotOnBinaryMessageReceived);
connect(m_pDataRecvWS, &QWebSocket::connected, this, &WebSocketBase::slotOnConnected);
connect(m_pTimer, &QTimer::timeout, this, &WebSocketBase::slotReconnect);
m_pDataRecvWS->open(QUrl(m_strURL));
}
}
void WebSocketBase::slotSendBinaryMessageMessage(const QByteArray &message)
{
if (m_pDataRecvWS)
m_pDataRecvWS->sendBinaryMessage(message);
}
void WebSocketBase::slotActiveReconnect()
{
qDebug("try to Active Reconnect!!!");
if(m_pDataRecvWS != nullptr)
{
m_bConnectStatus = false;
m_pDataRecvWS->abort();
qDebug("Exec Active Reconnect!");
m_pDataRecvWS->open(QUrl(m_strURL));
}
return;
}
void WebSocketBase::slotReconnect()
{
qDebug() << "try to reconnect:" << m_strURL;
m_pDataRecvWS->abort();
m_pDataRecvWS->open(QUrl(m_strURL));
}
void WebSocketBase::slotOnConnected()
{
qDebug("WebSocketBase websocket is already connect!");
m_bConnectStatus = true;
m_pTimer->stop();
qDebug() << "Address:" << m_strURL;
}
void WebSocketBase::slotOnDisConnected()
{
qDebug() << "Address is disconnected:" << m_strURL;
m_bConnectStatus = false;
m_pTimer->start(3000);/*-<当连接失败时,触发重连计时器,设置计数周期为3秒 */
}
void WebSocketBase::slotOnBinaryMessageReceived(const QByteArray& byteArray)
{
m_byteArray = byteArray;
}
(2)将客户端基类封装,改造下:(外面可以直接使用这个类进行通讯)
改造后客户端头文件:
/*
* @Description: websocket客户端,用于与中间件通信
*/
#pragma once
#include
#include
#include "Singleton.h"
class WebSocketBase;
class QTimer;
class WsReaderClient : public QObject, public Singleton
{
Q_OBJECT
friend class Singleton;
public:
WsReaderClient(QObject *parent = nullptr);
~WsReaderClient();
public:
void SendTo(const QByteArray &byteArray);
int RecvFrom(QByteArray& byteArray);
bool getConnectStatus();
signals:
//转发数据给server
void sigSendToServer(const QByteArray &byteArray);
public slots:
//接收服务器数据
void slotRecvServerData(const QByteArray &byteArray);
//发送服务器心跳包
void slotHeartBeatToServer();
private:
void readConfig();
private:
WebSocketBase* m_wsReaderClient;
QThread* m_thdReaderClient;
QTimer *m_pTimerReader;
int m_nHeartBeatTimeOutReader;
QString m_URL = "";
};
改造后客户端源文件:
#include "WsReaderClient.h"
#include
#include
#include
#include
#include
#include
#include "WebSocketBase.h"
WsReaderClient::WsReaderClient(QObject *parent)
: QObject(parent)
{
readConfig();
m_thdReaderClient = new QThread();
m_wsReaderClient = new WebSocketBase();
m_wsReaderClient->setWebSocketUrl(m_URL);
m_wsReaderClient->moveToThread(m_thdReaderClient);
connect(m_thdReaderClient, &QThread::started, m_wsReaderClient, &WebSocketBase::slotCreateDataRecWS);
connect(this, &WsReaderClient::sigSendToServer, m_wsReaderClient, &WebSocketBase::slotSendBinaryMessageMessage);
//connect(this, &WsReaderClient::sigReconnectServer, m_wsReaderClient, &WebSocketBase::slotActiveReconnect);
connect(m_thdReaderClient, &QThread::finished, m_wsReaderClient, &WebSocketBase::deleteLater);
connect(m_thdReaderClient, &QThread::finished, m_thdReaderClient, &QThread::deleteLater);
m_thdReaderClient->start();
m_pTimerReader = new QTimer(this);
connect(m_pTimerReader, &QTimer::timeout, this, &WsReaderClient::slotHeartBeatToServer);
//m_nHeartBeatTimeOutKeyBoard = 0;
m_pTimerReader->start(10*1000);
}
WsReaderClient::~WsReaderClient()
{
m_pTimerReader->stop();
m_pTimerReader->deleteLater();
if(m_wsReaderClient)
{
delete m_wsReaderClient;
m_wsReaderClient = nullptr;
}
if(m_pTimerReader)
{
delete m_pTimerReader;
m_pTimerReader = nullptr;
}
}
void WsReaderClient::slotHeartBeatToServer()
{
//todo
}
void WsReaderClient::readConfig()
{
// "/mnt/hgfs/SharedFiles/shanxi/Reader/linux_readerTest/bin/libReaderApi.so";
QString appPath = QCoreApplication::applicationDirPath();
qDebug() << "appPath=" << appPath;
QString path = appPath + "/ReaderConfig.ini";
QSettings settings(path, QSettings::IniFormat);
m_URL = settings.value("Communication/ipAddr").toString();
qDebug() << "m_URL=" << m_URL;
}
void WsReaderClient::SendTo(const QByteArray &data)
{
emit sigSendToServer(data);
}
int WsReaderClient::RecvFrom(QByteArray &byteArray)
{
return m_wsReaderClient->RecvFrom(byteArray);
}
bool WsReaderClient::getConnectStatus()
{
return m_wsReaderClient->getConnectStatus();
}