Skip to content

Commit

Permalink
add support for socket.id (#237)
Browse files Browse the repository at this point in the history
- Obtained during connect packet, set as member variable and forwarded to connect handlers
- Refactors onconnected callbacks to receive socket.id first, session second.
  • Loading branch information
getnamo committed Jul 20, 2022
1 parent f3828df commit 05346a8
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
2 changes: 1 addition & 1 deletion SocketIOClient.uplugin
Original file line number Diff line number Diff line change
@@ -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++.",
Expand Down
5 changes: 3 additions & 2 deletions Source/SocketIOClient/Private/SocketIOClientComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
};
Expand Down
8 changes: 4 additions & 4 deletions Source/SocketIOClient/Private/SocketIONative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion Source/SocketIOClient/Public/SocketIOClientComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ESIOConnectionCloseReason>, Reason);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FSIOCEventJsonSignature, FString, EventName, class USIOJsonValue*, EventData);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FSIOConnectionProblemSignature, int32, Attempts, int32, NextAttemptInMs, float, TimeSinceConnected);
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 4 additions & 1 deletion Source/SocketIOClient/Public/SocketIONative.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class SOCKETIOCLIENT_API FSocketIONative
FSocketIONative(const bool bForceTLSMode = false, const bool bShouldVerifyTLSCertificate = false);

//Native Callbacks
TFunction<void(const FString& SessionId)> OnConnectedCallback; //TFunction<void(const FString& SessionId)>
TFunction<void(const FString& SocketId, const FString& SessionId)> OnConnectedCallback; //TFunction<void(const FString& SessionId)>
TFunction<void(const ESIOConnectionCloseReason Reason)> OnDisconnectedCallback; //TFunction<void(const ESIOConnectionCloseReason Reason)>
TFunction<void(const FString& Namespace)> OnNamespaceConnectedCallback; //TFunction<void(const FString& Namespace)>
TFunction<void(const FString& Namespace)> OnNamespaceDisconnectedCallback; //TFunction<void(const FString& Namespace)>
Expand All @@ -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;

Expand Down
30 changes: 23 additions & 7 deletions Source/SocketIOLib/Private/sio_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ namespace sio
void emit(std::string const& name, message::list const& msglist, std::function<void (message::list const&)> 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();
Expand Down Expand Up @@ -197,6 +199,8 @@ namespace sio
bool m_connected;
std::string m_nsp;
message::ptr m_auth;

std::string m_socket_id;

std::map<unsigned int, std::function<void (message::list const&)> > m_acks;

Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -399,6 +403,13 @@ namespace sio
{
LOG("Received Message type (Connect)"<<std::endl);

const object_message* obj_ptr = static_cast<const object_message*>(p.get_message().get());
const map<string, message::ptr>* values = &(obj_ptr->get_map());
auto it = values->find("sid");
if (it != values->end()) {
m_socket_id = static_pointer_cast<string_message>(it->second)->get_string();
}

this->on_connected();
break;
}
Expand Down Expand Up @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions Source/SocketIOLib/Public/sio_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ namespace sio
void emit(std::string const& name, message::list const& msglist = nullptr, std::function<void (message::list const&)> 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&);

Expand Down

0 comments on commit 05346a8

Please sign in to comment.