Skip to content

Commit 5be8492

Browse files
committedDec 25, 2019
(cobra client) send a websocket ping every 30s to keep the connection opened
1 parent 33e7271 commit 5be8492

9 files changed

+36
-3
lines changed
 

‎docs/CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Changelog
22
All changes to this project will be documented in this file.
33

4+
## [7.6.5] - 2019-12-24
5+
6+
(cobra client) send a websocket ping every 30s to keep the connection opened
7+
48
## [7.6.4] - 2019-12-22
59

610
(client) error handling, quote url in error case when failing to parse one
711
(ws) ws_cobra_publish: register callbacks before connecting
812
(doc) mention mbedtls in supported ssl server backend
913

10-
1114
## [7.6.3] - 2019-12-20
1215

1316
(tls) add a simple description of the TLS configuration routine for debugging

‎ixcobra/ixcobra/IXCobraConnection.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace ix
2424
PublishTrackerCallback CobraConnection::_publishTrackerCallback = nullptr;
2525
constexpr size_t CobraConnection::kQueueMaxSize;
2626
constexpr CobraConnection::MsgId CobraConnection::kInvalidMsgId;
27+
constexpr int CobraConnection::kPingIntervalSecs;
2728

2829
CobraConnection::CobraConnection() :
2930
_webSocket(new WebSocket()),
@@ -228,6 +229,10 @@ namespace ix
228229
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
229230
invokeErrorCallback(ss.str(), std::string());
230231
}
232+
else if (msg->type == ix::WebSocketMessageType::Pong)
233+
{
234+
invokeEventCallback(ix::CobraConnection_EventType_Pong);
235+
}
231236
});
232237
}
233238

@@ -260,6 +265,7 @@ namespace ix
260265
_webSocket->setUrl(url);
261266
_webSocket->setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions);
262267
_webSocket->setTLSOptions(socketTLSOptions);
268+
_webSocket->setPingInterval(kPingIntervalSecs);
263269
}
264270

265271
//

‎ixcobra/ixcobra/IXCobraConnection.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ namespace ix
3030
CobraConnection_EventType_Closed = 3,
3131
CobraConnection_EventType_Subscribed = 4,
3232
CobraConnection_EventType_UnSubscribed = 5,
33-
CobraConnection_EventType_Published = 6
33+
CobraConnection_EventType_Published = 6,
34+
CobraConnection_EventType_Pong = 7
3435
};
3536

3637
enum CobraConnectionPublishMode
@@ -215,6 +216,9 @@ namespace ix
215216

216217
// Each pdu sent should have an incremental unique id
217218
std::atomic<uint64_t> _id;
219+
220+
// Frequency at which we send a websocket ping to the backing cobra connection
221+
static constexpr int kPingIntervalSecs = 30;
218222
};
219223

220224
} // namespace ix

‎ixcobra/ixcobra/IXCobraMetricsThreadedPublisher.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ namespace ix
6565
{
6666
ss << "Published message " << msgId << " acked";
6767
}
68+
else if (eventType == ix::CobraConnection_EventType_Pong)
69+
{
70+
ss << "Received websocket pong";
71+
}
6872

6973
ix::IXCoreLogger::Log(ss.str().c_str());
7074
});

‎ixwebsocket/IXWebSocketVersion.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
#pragma once
88

9-
#define IX_WEBSOCKET_VERSION "7.6.4"
9+
#define IX_WEBSOCKET_VERSION "7.6.5"

‎ws/ws_cobra_publish.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ namespace ix
9191
spdlog::info("Published message id {} acked", msgId);
9292
messageAcked = true;
9393
}
94+
else if (eventType == ix::CobraConnection_EventType_Pong)
95+
{
96+
spdlog::info("Received websocket pong");
97+
}
9498
});
9599

96100
conn.connect();

‎ws/ws_cobra_subscribe.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ namespace ix
100100
{
101101
spdlog::error("Published message hacked: {}", msgId);
102102
}
103+
else if (eventType == ix::CobraConnection_EventType_Pong)
104+
{
105+
spdlog::info("Received websocket pong");
106+
}
103107
});
104108

105109
while (true)

‎ws/ws_cobra_to_sentry.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ namespace ix
245245
{
246246
spdlog::error("Published message hacked: {}", msgId);
247247
}
248+
else if (eventType == ix::CobraConnection_EventType_Pong)
249+
{
250+
spdlog::info("Received websocket pong");
251+
}
248252
});
249253

250254
while (true)

‎ws/ws_cobra_to_statsd.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ namespace ix
160160
{
161161
spdlog::error("Published message hacked: {}", msgId);
162162
}
163+
else if (eventType == ix::CobraConnection_EventType_Pong)
164+
{
165+
spdlog::info("Received websocket pong");
166+
}
163167
});
164168

165169
while (true)

0 commit comments

Comments
 (0)
Please sign in to comment.