我正在编写一个简单的服务器,通过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),如果我向服务器发送请求,我会得到一些信息,例如socket 连接。
我尝试过这些:关闭防火墙,更改端口,更改浏览器,更改编译模式。
我不确定这是否涉及此问题,我正在通过msvc15和manully moc标头编译这些代码。
此代码由loadlibrary注入到其他应用程序中。
遵循服务器的完整源代码......
Server.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;
}
Server.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;
};
的DLLMain
#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;
}
答案 0 :(得分:1)
最后,它至少在Windows 10(msvc)上似乎是Qt 5.4.1的错误。
我可以通过websocket连接两个Qt应用程序,但不能让应用程序连接到普通浏览器。
所以,我使用另一个websocket库并且使用原始的Qt websocket。