Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions c++/qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ class AisStreamClient : public QObject
public:
explicit AisStreamClient(QObject* parent = nullptr);

private Q_SLOTS:
// private Q_SLOTS:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this remain a comment ?

void onConnected();
void onTextMessageReceived(QString message);
void onBinaryMessageReceived(const QByteArray& message);
void onSslErrors(const QList<QSslError>& errors);
void onError(QAbstractSocket::SocketError e);
void onPong(quint64 elapsedTime, const QByteArray &payload);

private:

void timerEvent(QTimerEvent *event) override;

private:
QWebSocket m_webSocket;

};

#include "qt.moc"
Expand All @@ -27,19 +33,43 @@ AisStreamClient::AisStreamClient(QObject* parent)
{
connect(&m_webSocket, &QWebSocket::connected, this, &AisStreamClient::onConnected);
connect(&m_webSocket, QOverload<const QList<QSslError>&>::of(&QWebSocket::sslErrors), this, &AisStreamClient::onSslErrors);
connect(&m_webSocket, &QWebSocket::pong, this, &AisStreamClient::onPong);
QSslConfiguration sslConfiguration;
m_webSocket.setSslConfiguration(sslConfiguration);
m_webSocket.open(QUrl("wss://stream.aisstream.io/v0/stream"));
}

void AisStreamClient::onPong(quint64 , const QByteArray &)
{
qDebug() << "pong ";
}

void AisStreamClient::timerEvent(QTimerEvent *)
{
qDebug() << "ping";
m_webSocket.ping();
}

void AisStreamClient::onConnected()
{
qDebug() << "WebSocket connected";
connect(&m_webSocket, &QWebSocket::textMessageReceived, this, &AisStreamClient::onTextMessageReceived);
connect(&m_webSocket,
static_cast<void (QWebSocket::*)(QAbstractSocket::SocketError)>(&QWebSocket::errorOccurred), this,
&AisStreamClient::onError);
connect(&m_webSocket, &QWebSocket::binaryMessageReceived, this, &AisStreamClient::onBinaryMessageReceived);
m_webSocket.sendTextMessage(QStringLiteral("{ \"APIKey\": \"<YOUR API KEY>\", \"BoundingBoxes\": [[[-11.0, 178.0], [30.0, 74.0]]]}"));
m_webSocket.sendTextMessage(QStringLiteral("{ \"APIKey\": \"<YOUR API KEY>\", \"FilterMessageTypes\": [\"PositionReport\"] ,\"BoundingBoxes\": [[[65, 17], [53, 23]]]}"));

// Need to ping pong to have the connection alive
startTimer(1000* 60 );
}

void AisStreamClient::onError(QAbstractSocket::SocketError e)
{
qDebug() << "Error " << e;
}


void AisStreamClient::onTextMessageReceived(QString message)
{
qDebug() << "Message received:" << message;
Expand Down