Skip to content

Commit 05346a8

Browse files
committed
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.
1 parent f3828df commit 05346a8

7 files changed

Lines changed: 42 additions & 16 deletions

File tree

SocketIOClient.uplugin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"FileVersion": 3,
33
"Version": 1,
4-
"VersionName": "2.3.4",
4+
"VersionName": "2.3.5",
55
"EngineVersion": "5.0.0",
66
"FriendlyName": "Socket.IO Client",
77
"Description": "Real-time WebSocket networking via Socket.IO protocol usable from blueprints and c++.",

Source/SocketIOClient/Private/SocketIOClientComponent.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,16 @@ void USocketIOClientComponent::SetupCallbacks()
130130
URLParams = NativeClient->URLParams;
131131
}
132132

133-
NativeClient->OnConnectedCallback = [this](const FString& InSessionId)
133+
NativeClient->OnConnectedCallback = [this](const FString& InSocketId, const FString& InSessionId)
134134
{
135135
if (NativeClient.IsValid())
136136
{
137137
bIsConnected = true;
138+
SocketId = InSocketId;
138139
SessionId = InSessionId;
139140
bool bIsReconnection = bIsHavingConnectionProblems;
140141
bIsHavingConnectionProblems = false;
141-
OnConnected.Broadcast(SessionId, bIsReconnection);
142+
OnConnected.Broadcast(SocketId, SessionId, bIsReconnection);
142143

143144
}
144145
};

Source/SocketIOClient/Private/SocketIONative.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ void FSocketIONative::SetupInternalCallbacks()
454454
{
455455
bIsConnected = true;
456456
SessionId = USIOMessageConvert::FStringFromStd(PrivateClient->get_sessionid());
457+
SocketId = USIOMessageConvert::FStringFromStd(PrivateClient->socket(nsp)->get_socket_id());
457458

458459
if (VerboseLog)
459460
{
@@ -463,18 +464,17 @@ void FSocketIONative::SetupInternalCallbacks()
463464
{
464465
if (bCallbackOnGameThread)
465466
{
466-
const FString SafeSessionId = SessionId;
467-
FCULambdaRunnable::RunShortLambdaOnGameThread([&, SafeSessionId]
467+
FCULambdaRunnable::RunShortLambdaOnGameThread([&]
468468
{
469469
if (OnConnectedCallback)
470470
{
471-
OnConnectedCallback(SessionId);
471+
OnConnectedCallback(SocketId, SessionId);
472472
}
473473
});
474474
}
475475
else
476476
{
477-
OnConnectedCallback(SessionId);
477+
OnConnectedCallback(SocketId, SessionId);
478478
}
479479
}
480480
}

Source/SocketIOClient/Public/SocketIOClientComponent.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FSIOCEventSignature);
1212
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSIOCSocketEventSignature, FString, Namespace);
13-
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FSIOCOpenEventSignature, FString, SessionId, bool, bIsReconnection);
13+
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FSIOCOpenEventSignature, FString, SocketId, FString, SessionId, bool, bIsReconnection);
1414
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FSIOCCloseEventSignature, TEnumAsByte<ESIOConnectionCloseReason>, Reason);
1515
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FSIOCEventJsonSignature, FString, EventName, class USIOJsonValue*, EventData);
1616
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FSIOConnectionProblemSignature, int32, Attempts, int32, NextAttemptInMs, float, TimeSinceConnected);
@@ -141,6 +141,10 @@ class SOCKETIOCLIENT_API USocketIOClientComponent : public UActorComponent
141141
UPROPERTY(BlueprintReadOnly, Category = "SocketIO Connection Properties")
142142
FString SessionId;
143143

144+
/** Each new connection is assigned a random 20-characters identifier. This identifier is synced with the value on the client-side. */
145+
UPROPERTY(BlueprintReadOnly, Category = "SocketIO Connection Properties")
146+
FString SocketId;
147+
144148
UPROPERTY(BlueprintReadOnly, Category = "SocketIO Connection Properties")
145149
bool bIsHavingConnectionProblems;
146150

Source/SocketIOClient/Public/SocketIONative.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class SOCKETIOCLIENT_API FSocketIONative
4646
FSocketIONative(const bool bForceTLSMode = false, const bool bShouldVerifyTLSCertificate = false);
4747

4848
//Native Callbacks
49-
TFunction<void(const FString& SessionId)> OnConnectedCallback; //TFunction<void(const FString& SessionId)>
49+
TFunction<void(const FString& SocketId, const FString& SessionId)> OnConnectedCallback; //TFunction<void(const FString& SessionId)>
5050
TFunction<void(const ESIOConnectionCloseReason Reason)> OnDisconnectedCallback; //TFunction<void(const ESIOConnectionCloseReason Reason)>
5151
TFunction<void(const FString& Namespace)> OnNamespaceConnectedCallback; //TFunction<void(const FString& Namespace)>
5252
TFunction<void(const FString& Namespace)> OnNamespaceDisconnectedCallback; //TFunction<void(const FString& Namespace)>
@@ -71,6 +71,9 @@ class SOCKETIOCLIENT_API FSocketIONative
7171
/** When connected this session id will be valid and contain a unique Id. */
7272
FString SessionId;
7373

74+
/** Each new connection is assigned a random 20-characters identifier. This identifier is synced with the value on the client-side. */
75+
FString SocketId;
76+
7477
//This will remain valid even after we disconnect. Replaced on disconnect.
7578
FString LastSessionId;
7679

Source/SocketIOLib/Private/sio_socket.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ namespace sio
159159
void emit(std::string const& name, message::list const& msglist, std::function<void (message::list const&)> const& ack);
160160

161161
std::string const& get_namespace() const {return m_nsp;}
162+
163+
std::string const& get_socket_id() const { return m_socket_id; }
162164

163165
protected:
164166
void on_connected();
@@ -197,6 +199,8 @@ namespace sio
197199
bool m_connected;
198200
std::string m_nsp;
199201
message::ptr m_auth;
202+
203+
std::string m_socket_id;
200204

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

@@ -294,7 +298,7 @@ namespace sio
294298
void socket::impl::send_connect()
295299
{
296300
NULL_GUARD(m_client);
297-
packet p(packet::type_connect,m_nsp, m_auth);
301+
packet p(packet::type_connect, m_nsp, m_auth);
298302
m_client->send(p);
299303
m_connection_timer.reset(new asio::system_timer(m_client->get_io_service()));
300304
lib::error_code ec;
@@ -307,7 +311,7 @@ namespace sio
307311
NULL_GUARD(m_client);
308312
if(m_connected)
309313
{
310-
packet p(packet::type_disconnect,m_nsp);
314+
packet p(packet::type_disconnect, m_nsp);
311315
send_packet(p);
312316

313317
if(!m_connection_timer)
@@ -399,6 +403,13 @@ namespace sio
399403
{
400404
LOG("Received Message type (Connect)"<<std::endl);
401405

406+
const object_message* obj_ptr = static_cast<const object_message*>(p.get_message().get());
407+
const map<string, message::ptr>* values = &(obj_ptr->get_map());
408+
auto it = values->find("sid");
409+
if (it != values->end()) {
410+
m_socket_id = static_pointer_cast<string_message>(it->second)->get_string();
411+
}
412+
402413
this->on_connected();
403414
break;
404415
}
@@ -603,12 +614,17 @@ namespace sio
603614
return m_impl->get_namespace();
604615
}
605616

606-
void socket::on_connected()
607-
{
617+
std::string const& socket::get_socket_id() const
618+
{
619+
return m_impl->get_socket_id();
620+
}
621+
622+
void socket::on_connected()
623+
{
608624
m_impl->on_connected();
609-
}
610-
611-
void socket::on_close()
625+
}
626+
627+
void socket::on_close()
612628
{
613629
m_impl->on_close();
614630
}

Source/SocketIOLib/Public/sio_socket.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ namespace sio
7575
void emit(std::string const& name, message::list const& msglist = nullptr, std::function<void (message::list const&)> const& ack = nullptr);
7676

7777
std::string const& get_namespace() const;
78+
79+
std::string const& get_socket_id() const;
7880

7981
socket(client_impl_base*,std::string const&,message::ptr const&);
8082

0 commit comments

Comments
 (0)