(cobra client) send a websocket ping every 30s to keep the connection opened
This commit is contained in:
		| @@ -24,6 +24,7 @@ namespace ix | ||||
|     PublishTrackerCallback CobraConnection::_publishTrackerCallback = nullptr; | ||||
|     constexpr size_t CobraConnection::kQueueMaxSize; | ||||
|     constexpr CobraConnection::MsgId CobraConnection::kInvalidMsgId; | ||||
|     constexpr int CobraConnection::kPingIntervalSecs; | ||||
|  | ||||
|     CobraConnection::CobraConnection() : | ||||
|         _webSocket(new WebSocket()), | ||||
| @@ -228,6 +229,10 @@ namespace ix | ||||
|                     ss << "HTTP Status: "      << msg->errorInfo.http_status << std::endl; | ||||
|                     invokeErrorCallback(ss.str(), std::string()); | ||||
|                 } | ||||
|                 else if (msg->type == ix::WebSocketMessageType::Pong) | ||||
|                 { | ||||
|                     invokeEventCallback(ix::CobraConnection_EventType_Pong); | ||||
|                 } | ||||
|         }); | ||||
|     } | ||||
|  | ||||
| @@ -260,6 +265,7 @@ namespace ix | ||||
|         _webSocket->setUrl(url); | ||||
|         _webSocket->setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions); | ||||
|         _webSocket->setTLSOptions(socketTLSOptions); | ||||
|         _webSocket->setPingInterval(kPingIntervalSecs); | ||||
|     } | ||||
|  | ||||
|     // | ||||
|   | ||||
| @@ -30,7 +30,8 @@ namespace ix | ||||
|         CobraConnection_EventType_Closed = 3, | ||||
|         CobraConnection_EventType_Subscribed = 4, | ||||
|         CobraConnection_EventType_UnSubscribed = 5, | ||||
|         CobraConnection_EventType_Published = 6 | ||||
|         CobraConnection_EventType_Published = 6, | ||||
|         CobraConnection_EventType_Pong = 7 | ||||
|     }; | ||||
|  | ||||
|     enum CobraConnectionPublishMode | ||||
| @@ -215,6 +216,9 @@ namespace ix | ||||
|  | ||||
|         // Each pdu sent should have an incremental unique id | ||||
|         std::atomic<uint64_t> _id; | ||||
|  | ||||
|         // Frequency at which we send a websocket ping to the backing cobra connection | ||||
|         static constexpr int kPingIntervalSecs = 30; | ||||
|     }; | ||||
|  | ||||
| } // namespace ix | ||||
|   | ||||
| @@ -65,6 +65,10 @@ namespace ix | ||||
|                 { | ||||
|                     ss << "Published message " << msgId << " acked"; | ||||
|                 } | ||||
|                 else if (eventType == ix::CobraConnection_EventType_Pong) | ||||
|                 { | ||||
|                     ss << "Received websocket pong"; | ||||
|                 } | ||||
|  | ||||
|                 ix::IXCoreLogger::Log(ss.str().c_str()); | ||||
|         }); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user