2021-01-24 08:04:48 +00:00
|
|
|
|
#include "webserver.h"
|
2023-10-17 03:31:58 +00:00
|
|
|
|
#include "qthelper.h"
|
2021-01-24 08:04:48 +00:00
|
|
|
|
|
|
|
|
|
WebServer::WebServer(const QString &serverName, SslMode secureMode, QObject *parent) : QWebSocketServer(serverName, secureMode, parent)
|
|
|
|
|
{
|
2021-08-22 02:57:02 +00:00
|
|
|
|
connect(this, SIGNAL(newConnection()), this, SLOT(slot_newConnection()));
|
2021-01-24 08:04:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 02:57:02 +00:00
|
|
|
|
void WebServer::slot_newConnection()
|
2021-01-24 08:04:48 +00:00
|
|
|
|
{
|
|
|
|
|
QWebSocket *socket = this->nextPendingConnection();
|
|
|
|
|
WebClient *client = new WebClient(socket, this);
|
2021-08-22 02:57:02 +00:00
|
|
|
|
connect(client, SIGNAL(disconnected(QString, int)), this, SLOT(slot_disconnected(QString, int)));
|
|
|
|
|
connect(client, SIGNAL(error(QString, int, QString)), this, SIGNAL(error(QString, int, QString)));
|
2021-01-24 08:04:48 +00:00
|
|
|
|
connect(client, SIGNAL(sendData(QString, int, QString)), this, SIGNAL(sendData(QString, int, QString)));
|
|
|
|
|
connect(client, SIGNAL(receiveData(QString, int, QString)), this, SIGNAL(receiveData(QString, int, QString)));
|
|
|
|
|
|
2021-08-22 02:57:02 +00:00
|
|
|
|
emit connected(client->getIP(), client->getPort());
|
2021-01-24 08:04:48 +00:00
|
|
|
|
//连接后加入链表
|
|
|
|
|
clients.append(client);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-22 02:57:02 +00:00
|
|
|
|
void WebServer::slot_disconnected(const QString &ip, int port)
|
2021-01-24 08:04:48 +00:00
|
|
|
|
{
|
|
|
|
|
WebClient *client = (WebClient *)sender();
|
2021-08-22 02:57:02 +00:00
|
|
|
|
emit disconnected(ip, port);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
//断开连接后从链表中移除
|
|
|
|
|
clients.removeOne(client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool WebServer::start()
|
|
|
|
|
{
|
2021-04-13 01:36:22 +00:00
|
|
|
|
bool ok = listen(QHostAddress(AppConfig::WebListenIP), AppConfig::WebListenPort);
|
2021-01-24 08:04:48 +00:00
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebServer::stop()
|
|
|
|
|
{
|
|
|
|
|
remove();
|
|
|
|
|
this->close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebServer::writeData(const QString &ip, int port, const QString &data)
|
|
|
|
|
{
|
|
|
|
|
foreach (WebClient *client, clients) {
|
|
|
|
|
if (client->getIP() == ip && client->getPort() == port) {
|
|
|
|
|
client->sendData(data);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebServer::writeData(const QString &data)
|
|
|
|
|
{
|
|
|
|
|
foreach (WebClient *client, clients) {
|
|
|
|
|
client->sendData(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebServer::remove(const QString &ip, int port)
|
|
|
|
|
{
|
|
|
|
|
foreach (WebClient *client, clients) {
|
|
|
|
|
if (client->getIP() == ip && client->getPort() == port) {
|
|
|
|
|
client->abort();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebServer::remove()
|
|
|
|
|
{
|
|
|
|
|
foreach (WebClient *client, clients) {
|
|
|
|
|
client->abort();
|
|
|
|
|
}
|
|
|
|
|
}
|