gpt4 book ai didi

c++ - QWebSocketServer 仍在连接

转载 作者:行者123 更新时间:2023-11-30 05:33:57 24 4
gpt4 key购买 nike

我正在编写一个简单的服务器,它通过 QWebSocketServer 将一些数据传递给 javascript(与 qt doc 基本相同...)。

还有,请看这里:

void DebuggerServer::onNewConnection() {
puts("C");
QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();

connect(pSocket, &QWebSocket::textMessageReceived, this, &DebuggerServer::processMessage);
connect(pSocket, &QWebSocket::disconnected, this, &DebuggerServer::socketDisconnected);

printf("onNewConnection");

m_clients << pSocket;
}

我尝试在此处 puts("C");,但我什么也没得到。

在我的浏览器(最新版本的 chrome)中,如果我向服务器发送请求,我会得到一些信息,比如套接字正在连接

我试过这些东西:关闭防火墙、更改端口、更改浏览器、更改编译模式。

我不确定这是否涉及这个问题,我正在通过 msvc15 和手动 moc header 编译这些代码。

此代码由 loadlibrary 注入(inject)到其他应用程序中。

服务器的完整源代码如下...

服务器.cpp

#include "Server.h"
#include "Inspector.h"

#include "Server.moc"

#include <QtCore/QCoreApplication>

const int port = 5642;
DebuggerServer *server;

void StartServer() {
server = new DebuggerServer(port);
}

QT_USE_NAMESPACE

DebuggerServer::DebuggerServer(quint16 port, QObject *parent) :
QObject(parent),
m_pWebSocketServer(Q_NULLPTR),
m_clients()
{
m_pWebSocketServer = new QWebSocketServer(QStringLiteral("mvInspector"),
QWebSocketServer::NonSecureMode,
this);
puts("A");
if (m_pWebSocketServer->listen(QHostAddress::Any, port))
{
puts("B");
connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
this, &DebuggerServer::onNewConnection);
}
}


DebuggerServer::~DebuggerServer() {
m_pWebSocketServer->close();
qDeleteAll(m_clients.begin(), m_clients.end());
}

template<class T>
QString toStr(QList<T> y) {
QString rst;
for (T x : y)
rst += "\n" + QString(x);
return rst;
}

#define ASSERT_N(x) if (frame.length() != x) rst = "ERRON, wrong number of arguments."; else

void DebuggerServer::processMessage(QString message) {
QWebSocket *pSender = qobject_cast<QWebSocket *>(sender());
QStringList frame = message.split("\n");
QString rst;

rst += frame[0];
if (frame[0] == "getRootWindows") {
ASSERT_N(1)
rst += toStr(getRootWindows());
}
if (frame[0] == "getChildren") {
ASSERT_N(2)
rst += toStr(getChildren(frame[1].toInt()));
}
if (frame[0] == "getPropertyName") {
ASSERT_N(2)
rst += toStr(getPropertyName(frame[1].toInt()));
}
if (frame[0] == "getProperty") {
ASSERT_N(3)
rst += getProperty(frame[1].toInt(), frame[2].toStdString().c_str()).toBase64();
}
if (frame[0] == "getClassname") {
ASSERT_N(2)
rst += getClassname(frame[1].toInt());
}
if (frame[0] == "getClassname") {
ASSERT_N(2)
rst += getObjectname(frame[1].toInt());
}
printf(rst.toStdString().c_str());
pSender->sendTextMessage(rst);
}

void DebuggerServer::socketDisconnected()
{
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
if (pClient) {
m_clients.removeAll(pClient);
pClient->deleteLater();
}
printf("socketDisconnected");
}

void DebuggerServer::onNewConnection() {
puts("C");
QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();

connect(pSocket, &QWebSocket::textMessageReceived, this, &DebuggerServer::processMessage);
connect(pSocket, &QWebSocket::disconnected, this, &DebuggerServer::socketDisconnected);

printf("onNewConnection");

m_clients << pSocket;
}

服务器.h

#pragma once
#include <QtCore/qobject.h>
#include <QtWebSockets/qwebsocketserver>
#include <QtWebSockets/QWebSocket>

void StartServer();

class DebuggerServer : public QObject {
Q_OBJECT
public:
explicit DebuggerServer(quint16 port, QObject *parent = Q_NULLPTR);
virtual ~DebuggerServer();

private Q_SLOTS:
void onNewConnection();
void processMessage(QString message);
void socketDisconnected();

private:
QWebSocketServer *m_pWebSocketServer;
QList<QWebSocket *> m_clients;
};

主程序

#include <Windows.h>

#include "../Server.h"

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved){
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
StartServer();
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;

}

最佳答案

最后,至少在 windows 10 (msvc) 上,它似乎是 Qt 5.4.1 的一个错误。

我可以让两个 Qt 应用程序通过 websocket 连接,但不能让 At 应用程序与普通浏览器连接。

所以,我使用另一个 websocket 库并尊重原始的 Qt websocket。

关于c++ - QWebSocketServer 仍在连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34511274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com