I'm opening a url from Qt/c++ client like,
m_webSocket = new QWebSocket();
m_webSocket->open("wss://192.123.1.44:8087");
I want catch any errors in connection. How do I do that?
I've connected to signal QWebSocket:error(QAbstractSocket::SocketError error)
, but I never get it triggered even if my server is not running.
Edit: I'm connecting to error signal as below,
m_webSocket = new QWebSocket();
connect(m_webSocket, SIGNAL(error(QAbstractSocket::SocketError error)), this, SLOT(onWebSocketError(QAbstractSocket::SocketError error)));
m_webSocket->open(url);
This seems to be not working.
Once a connection has been established between the client and the server, an open event is fired from the Web Socket instance. Error are generated for mistakes, which take place during the communication. It is marked with the help of onerror event. Onerror is always followed by termination of connection.
Solution 1Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.
You can check if a WebSocket is connected by doing either of the following: Specifying a function to the WebSocket. onopen event handler property, or; Using addEventListener to listen to the open event.
The most common cause of Websocket error is when you connect to DSS through a proxy. Websockets is a fairly recent protocol and many enterprise proxies do not support it. The websocket connection will not establish and you will see this message.
Connect to the QWebSocket error signal, before opening the socket.
QWebSocket* pWebSocket = new QWebSocket;
connect(pWebSocket, &QWebSocket::error, [=](QAbstractSocket::SocketError error)
{
// Handle error here...
qDebug() << pWebSocket->errorString();
}
pWebSocket->open("wss://192.123.1.44:8087");
Note that this connection uses a lambda function, which requires C++ 11. Connection to a slot in the usual manner will also work.
Without C++ 11, use a standard (Qt 5) connection: -
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass(QUrl url);
protected slots:
void Error(QAbstractSocket::SocketError error);
private:
QWebSocket* pWebSocket;
};
MyClass::MyClass(QUrl url)
{
QWebSocket* pWebSocket = new QWebSocket;
connect(pWebSocket, &QWebSocket::error, pMyClass, &MyClass::Error);
m_webSocket->open(url);
}
As QObject::connect returns a QMetaObjectConnection, which contains an overloaded bool operator, you can check the return from the call to connect, to ensure that a valid signal and slot was found, and valid arguments provided: -
// Old-style connection
if(!connect(m_webSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onWebSocketError(QAbstractSocket::SocketError))) )
{
qDebug() << "Failed to connect to QWebSocket::error" << endl;
}
However, this is largely redundant, as a failed connection usually produces debug output informing you of this issue, from within Qt's connect call.
NOTE, in the old-style connection syntax, the argument list does not name the arguments, only the type is provided. Adding a name will produce a failed connection.
As described in the documentation:
Note: Signal error is overloaded in this class. To connect to this signal by using the function pointer syntax, Qt provides a convenient helper for obtaining the function pointer as shown in this example:
connect(webSocket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
[=](QAbstractSocket::SocketError error){ /* ... */ });
Of course, instead of lambda function, you could connect to a normal one, but the QOverload
part is important.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With