From 05346a82d32181b1b151b2336922b387bb30a4d4 Mon Sep 17 00:00:00 2001 From: getnamo Date: Wed, 20 Jul 2022 11:59:22 -0700 Subject: [PATCH] add support for socket.id (#237) - Obtained during connect packet, set as member variable and forwarded to connect handlers - Refactors onconnected callbacks to receive socket.id first, session second. --- SocketIOClient.uplugin | 2 +- .../Private/SocketIOClientComponent.cpp | 5 ++-- .../SocketIOClient/Private/SocketIONative.cpp | 8 ++--- .../Public/SocketIOClientComponent.h | 6 +++- Source/SocketIOClient/Public/SocketIONative.h | 5 +++- Source/SocketIOLib/Private/sio_socket.cpp | 30 ++++++++++++++----- Source/SocketIOLib/Public/sio_socket.h | 2 ++ 7 files changed, 42 insertions(+), 16 deletions(-) diff --git a/SocketIOClient.uplugin b/SocketIOClient.uplugin index 0c459a6..984d4c4 100644 --- a/SocketIOClient.uplugin +++ b/SocketIOClient.uplugin @@ -1,7 +1,7 @@ { "FileVersion": 3, "Version": 1, - "VersionName": "2.3.4", + "VersionName": "2.3.5", "EngineVersion": "5.0.0", "FriendlyName": "Socket.IO Client", "Description": "Real-time WebSocket networking via Socket.IO protocol usable from blueprints and c++.", diff --git a/Source/SocketIOClient/Private/SocketIOClientComponent.cpp b/Source/SocketIOClient/Private/SocketIOClientComponent.cpp index d519f7e..348d73a 100644 --- a/Source/SocketIOClient/Private/SocketIOClientComponent.cpp +++ b/Source/SocketIOClient/Private/SocketIOClientComponent.cpp @@ -130,15 +130,16 @@ void USocketIOClientComponent::SetupCallbacks() URLParams = NativeClient->URLParams; } - NativeClient->OnConnectedCallback = [this](const FString& InSessionId) + NativeClient->OnConnectedCallback = [this](const FString& InSocketId, const FString& InSessionId) { if (NativeClient.IsValid()) { bIsConnected = true; + SocketId = InSocketId; SessionId = InSessionId; bool bIsReconnection = bIsHavingConnectionProblems; bIsHavingConnectionProblems = false; - OnConnected.Broadcast(SessionId, bIsReconnection); + OnConnected.Broadcast(SocketId, SessionId, bIsReconnection); } }; diff --git a/Source/SocketIOClient/Private/SocketIONative.cpp b/Source/SocketIOClient/Private/SocketIONative.cpp index 1666ae0..ad2dff8 100644 --- a/Source/SocketIOClient/Private/SocketIONative.cpp +++ b/Source/SocketIOClient/Private/SocketIONative.cpp @@ -454,6 +454,7 @@ void FSocketIONative::SetupInternalCallbacks() { bIsConnected = true; SessionId = USIOMessageConvert::FStringFromStd(PrivateClient->get_sessionid()); + SocketId = USIOMessageConvert::FStringFromStd(PrivateClient->socket(nsp)->get_socket_id()); if (VerboseLog) { @@ -463,18 +464,17 @@ void FSocketIONative::SetupInternalCallbacks() { if (bCallbackOnGameThread) { - const FString SafeSessionId = SessionId; - FCULambdaRunnable::RunShortLambdaOnGameThread([&, SafeSessionId] + FCULambdaRunnable::RunShortLambdaOnGameThread([&] { if (OnConnectedCallback) { - OnConnectedCallback(SessionId); + OnConnectedCallback(SocketId, SessionId); } }); } else { - OnConnectedCallback(SessionId); + OnConnectedCallback(SocketId, SessionId); } } } diff --git a/Source/SocketIOClient/Public/SocketIOClientComponent.h b/Source/SocketIOClient/Public/SocketIOClientComponent.h index 3779d83..7ec2e98 100644 --- a/Source/SocketIOClient/Public/SocketIOClientComponent.h +++ b/Source/SocketIOClient/Public/SocketIOClientComponent.h @@ -10,7 +10,7 @@ DECLARE_DYNAMIC_MULTICAST_DELEGATE(FSIOCEventSignature); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSIOCSocketEventSignature, FString, Namespace); -DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FSIOCOpenEventSignature, FString, SessionId, bool, bIsReconnection); +DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FSIOCOpenEventSignature, FString, SocketId, FString, SessionId, bool, bIsReconnection); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSIOCCloseEventSignature, TEnumAsByte, Reason); DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FSIOCEventJsonSignature, FString, EventName, class USIOJsonValue*, EventData); DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FSIOConnectionProblemSignature, int32, Attempts, int32, NextAttemptInMs, float, TimeSinceConnected); @@ -141,6 +141,10 @@ class SOCKETIOCLIENT_API USocketIOClientComponent : public UActorComponent UPROPERTY(BlueprintReadOnly, Category = "SocketIO Connection Properties") FString SessionId; + /** Each new connection is assigned a random 20-characters identifier. This identifier is synced with the value on the client-side. */ + UPROPERTY(BlueprintReadOnly, Category = "SocketIO Connection Properties") + FString SocketId; + UPROPERTY(BlueprintReadOnly, Category = "SocketIO Connection Properties") bool bIsHavingConnectionProblems; diff --git a/Source/SocketIOClient/Public/SocketIONative.h b/Source/SocketIOClient/Public/SocketIONative.h index 1428850..fc920c9 100644 --- a/Source/SocketIOClient/Public/SocketIONative.h +++ b/Source/SocketIOClient/Public/SocketIONative.h @@ -46,7 +46,7 @@ class SOCKETIOCLIENT_API FSocketIONative FSocketIONative(const bool bForceTLSMode = false, const bool bShouldVerifyTLSCertificate = false); //Native Callbacks - TFunction OnConnectedCallback; //TFunction + TFunction OnConnectedCallback; //TFunction TFunction OnDisconnectedCallback; //TFunction TFunction OnNamespaceConnectedCallback; //TFunction TFunction OnNamespaceDisconnectedCallback; //TFunction @@ -71,6 +71,9 @@ class SOCKETIOCLIENT_API FSocketIONative /** When connected this session id will be valid and contain a unique Id. */ FString SessionId; + /** Each new connection is assigned a random 20-characters identifier. This identifier is synced with the value on the client-side. */ + FString SocketId; + //This will remain valid even after we disconnect. Replaced on disconnect. FString LastSessionId; diff --git a/Source/SocketIOLib/Private/sio_socket.cpp b/Source/SocketIOLib/Private/sio_socket.cpp index 4d0f68b..715d018 100644 --- a/Source/SocketIOLib/Private/sio_socket.cpp +++ b/Source/SocketIOLib/Private/sio_socket.cpp @@ -159,6 +159,8 @@ namespace sio void emit(std::string const& name, message::list const& msglist, std::function const& ack); std::string const& get_namespace() const {return m_nsp;} + + std::string const& get_socket_id() const { return m_socket_id; } protected: void on_connected(); @@ -197,6 +199,8 @@ namespace sio bool m_connected; std::string m_nsp; message::ptr m_auth; + + std::string m_socket_id; std::map > m_acks; @@ -294,7 +298,7 @@ namespace sio void socket::impl::send_connect() { NULL_GUARD(m_client); - packet p(packet::type_connect,m_nsp, m_auth); + packet p(packet::type_connect, m_nsp, m_auth); m_client->send(p); m_connection_timer.reset(new asio::system_timer(m_client->get_io_service())); lib::error_code ec; @@ -307,7 +311,7 @@ namespace sio NULL_GUARD(m_client); if(m_connected) { - packet p(packet::type_disconnect,m_nsp); + packet p(packet::type_disconnect, m_nsp); send_packet(p); if(!m_connection_timer) @@ -399,6 +403,13 @@ namespace sio { LOG("Received Message type (Connect)"<(p.get_message().get()); + const map* values = &(obj_ptr->get_map()); + auto it = values->find("sid"); + if (it != values->end()) { + m_socket_id = static_pointer_cast(it->second)->get_string(); + } + this->on_connected(); break; } @@ -603,12 +614,17 @@ namespace sio return m_impl->get_namespace(); } - void socket::on_connected() - { + std::string const& socket::get_socket_id() const + { + return m_impl->get_socket_id(); + } + + void socket::on_connected() + { m_impl->on_connected(); - } - - void socket::on_close() + } + + void socket::on_close() { m_impl->on_close(); } diff --git a/Source/SocketIOLib/Public/sio_socket.h b/Source/SocketIOLib/Public/sio_socket.h index 3c2e625..c75ec31 100644 --- a/Source/SocketIOLib/Public/sio_socket.h +++ b/Source/SocketIOLib/Public/sio_socket.h @@ -75,6 +75,8 @@ namespace sio void emit(std::string const& name, message::list const& msglist = nullptr, std::function const& ack = nullptr); std::string const& get_namespace() const; + + std::string const& get_socket_id() const; socket(client_impl_base*,std::string const&,message::ptr const&);