Skip to content

Commit

Permalink
Added path support
Browse files Browse the repository at this point in the history
  • Loading branch information
brittanjt committed Aug 9, 2020
1 parent a42cace commit e62ef11
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 17 deletions.
21 changes: 13 additions & 8 deletions Source/SocketIOClient/Private/SocketIOClientComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void USocketIOClientComponent::UninitializeComponent()
NativeClient = nullptr;
}

//UE_LOG(SocketIOLog, Log, TEXT("UninitializeComponent() call"));
//UE_LOG(LogTemp, Log, TEXT("UninitializeComponent() call"));
Super::UninitializeComponent();
}

Expand Down Expand Up @@ -362,15 +362,20 @@ bool USocketIOClientComponent::CallBPFunctionWithMessage(UObject* Target, const
#endif

void USocketIOClientComponent::Connect(const FString& InAddressAndPort, USIOJsonObject* Query /*= nullptr*/, USIOJsonObject* Headers /*= nullptr*/)
{
ConnectWithPath(InAddressAndPort, TEXT("socket.io"), Query, Headers);
}

void USocketIOClientComponent::ConnectWithPath(const FString& InAddressAndPort, const FString& Path, USIOJsonObject* Query /*= nullptr*/, USIOJsonObject* Headers /*= nullptr*/)
{
//Check if we're limiting this component
if(bLimitConnectionToGameWorld)
{
if (bLimitConnectionToGameWorld)
{
UWorld* World = GEngine->GetWorldFromContextObject(this, EGetWorldErrorMode::LogAndReturnNull);
if (World)
{
bool bIsGameWorld = (World->IsGameWorld() || World->IsPreviewWorld());
if(!bIsGameWorld)
if (!bIsGameWorld)
{
UE_LOG(SocketIOLog, Log, TEXT("USocketIOClientComponent::Connect attempt in non-game world blocked by bLimitConnectionToGameWorld."));
return;
Expand Down Expand Up @@ -399,13 +404,13 @@ void USocketIOClientComponent::Connect(const FString& InAddressAndPort, USIOJson
NativeClient->MaxReconnectionAttempts = MaxReconnectionAttempts;
NativeClient->ReconnectionDelay = ReconnectionDelayInMs;
NativeClient->VerboseLog = bVerboseConnectionLog;
ConnectNative(InAddressAndPort, QueryFJson, HeadersFJson);

ConnectNativeWithPath(InAddressAndPort, Path, QueryFJson, HeadersFJson);
}

void USocketIOClientComponent::ConnectNative(const FString& InAddressAndPort, const TSharedPtr<FJsonObject>& Query /*= nullptr*/, const TSharedPtr<FJsonObject>& Headers /*= nullptr*/)
void USocketIOClientComponent::ConnectNative(const FString& InAddressAndPort, const FString& Path, const TSharedPtr<FJsonObject>& Query /*= nullptr*/, const TSharedPtr<FJsonObject>& Headers /*= nullptr*/)
{
NativeClient->Connect(InAddressAndPort, Query, Headers);
NativeClient->Connect(InAddressAndPort, Query, Headers, Path);
}

void USocketIOClientComponent::Disconnect()
Expand Down
2 changes: 1 addition & 1 deletion Source/SocketIOClient/Private/SocketIONative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ FSocketIONative::FSocketIONative()
ClearCallbacks();
}

void FSocketIONative::Connect(const FString& InAddressAndPort, const TSharedPtr<FJsonObject>& Query /*= nullptr*/, const TSharedPtr<FJsonObject>& Headers /*= nullptr*/)
void FSocketIONative::Connect(const FString& InAddressAndPort, const TSharedPtr<FJsonObject>& Query /*= nullptr*/, const TSharedPtr<FJsonObject>& Headers /*= nullptr*/, const FString& Path)
{
std::string StdAddressString = USIOMessageConvert::StdString(InAddressAndPort);
if (InAddressAndPort.IsEmpty())
Expand Down
17 changes: 17 additions & 0 deletions Source/SocketIOClient/Public/SocketIOClientComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ class SOCKETIOCLIENT_API USocketIOClientComponent : public UActorComponent
USIOJsonObject* Query = nullptr,
USIOJsonObject* Headers = nullptr);

/**
* Connect to a socket.io server, optional method if auto-connect is set to true.
* Query and headers are defined by a {'stringKey':'stringValue'} SIOJson Object
*
* @param AddressAndPort the address in URL format with port
* @param Query http query as a SIOJsonObject with string keys and values
* @param Headers http header as a SIOJsonObject with string keys and values
*
*/
UFUNCTION(BlueprintCallable, Category = "SocketIO Functions")
void ConnectWithPath(const FString& InAddressAndPort,
const FString& Path,
USIOJsonObject* Query = nullptr,
USIOJsonObject* Headers = nullptr);

/**
* Disconnect from current socket.io server. This is an asynchronous action,
* subscribe to OnDisconnected to know when you can safely continue from a
Expand Down Expand Up @@ -249,11 +264,13 @@ class SOCKETIOCLIENT_API USocketIOClientComponent : public UActorComponent
* Query and headers are defined by a {'stringKey':'stringValue'} FJsonObjects
*
* @param AddressAndPort the address in URL format with port
* @param Path the path of the socketio server (ex: "socket.io"
* @param Query http query as a FJsonObject with string keys and values
* @param Headers http header as a FJsonObject with string keys and values
*
*/
void ConnectNative( const FString& InAddressAndPort,
const FString& Path,
const TSharedPtr<FJsonObject>& Query = nullptr,
const TSharedPtr<FJsonObject>& Headers = nullptr);

Expand Down
3 changes: 2 additions & 1 deletion Source/SocketIOClient/Public/SocketIONative.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ class SOCKETIOCLIENT_API FSocketIONative
void Connect(
const FString& InAddressAndPort,
const TSharedPtr<FJsonObject>& Query,
const TSharedPtr<FJsonObject>& Headers);
const TSharedPtr<FJsonObject>& Headers,
const FString& Path = "socket.io");

/**
* Join a desired namespace. Keep in mind that emitting to a namespace will auto-join it
Expand Down
5 changes: 3 additions & 2 deletions Source/SocketIOLib/Private/internal/sio_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ namespace sio
sync_close();
}

void client_impl::connect(const string& uri, const map<string,string>& query, const map<string, string>& headers)
void client_impl::connect(const string& uri, const map<string,string>& query, const map<string, string>& headers, const std::string& path)
{
//reset connection attempts to last set
m_reconn_attempts = m_reconn_attempts_when_closed;
Expand Down Expand Up @@ -127,6 +127,7 @@ namespace sio
m_query_string=move(query_str);

m_http_headers = headers;
m_path = path;

this->reset_states();
m_client.get_io_service().dispatch(lib::bind(&client_impl::connect_impl,this,uri,m_query_string));
Expand Down Expand Up @@ -244,7 +245,7 @@ namespace sio
} else {
ss<<uo.get_host();
}
ss<<":"<<uo.get_port()<<"/socket.io/?EIO=4&transport=websocket";
ss<<":"<<uo.get_port()<<"/" << m_path << "/?EIO=4&transport=websocket";
if(m_sid.size()>0){
ss<<"&sid="<<m_sid;
}
Expand Down
3 changes: 2 additions & 1 deletion Source/SocketIOLib/Private/internal/sio_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace sio

// Client Functions - such as send, etc.
void connect(const std::string& uri, const std::map<std::string, std::string>& queryString,
const std::map<std::string, std::string>& httpExtraHeaders);
const std::map<std::string, std::string>& httpExtraHeaders, const std::string& path = "socket.io");

sio::socket::ptr const& socket(const std::string& nsp);

Expand Down Expand Up @@ -215,6 +215,7 @@ namespace sio
std::string m_sid;
std::string m_base_url;
std::string m_query_string;
std::string m_path;
std::map<std::string, std::string> m_http_headers;

unsigned int m_ping_interval;
Expand Down
14 changes: 10 additions & 4 deletions Source/SocketIOLib/Private/sio_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ using std::stringstream;
namespace sio
{
client::client():
m_impl(new client_impl())
m_impl(new client_impl()),
m_path("socket.io")
{
}

Expand Down Expand Up @@ -88,18 +89,18 @@ namespace sio

void client::connect(const std::string& uri)
{
m_impl->connect(uri, {}, {});
m_impl->connect(uri, {}, {}, m_path);
}

void client::connect(const std::string& uri, const std::map<string,string>& query)
{
m_impl->connect(uri, query, {});
m_impl->connect(uri, query, {}, m_path);
}

void client::connect(const std::string& uri, const std::map<std::string,std::string>& query,
const std::map<std::string,std::string>& http_extra_headers)
{
m_impl->connect(uri, query, http_extra_headers);
m_impl->connect(uri, query, http_extra_headers, m_path);
}

socket::ptr const& client::socket(const std::string& nsp)
Expand Down Expand Up @@ -147,6 +148,11 @@ namespace sio
{
m_impl->set_reconnect_delay_max(millis);
}

void client::set_path(const std::string& path)
{
m_path = path;
}

void client::stop()
{
Expand Down
3 changes: 3 additions & 0 deletions Source/SocketIOLib/Public/sio_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ namespace sio
void set_reconnect_delay(unsigned millis);

void set_reconnect_delay_max(unsigned millis);

void set_path(const std::string& path);

sio::socket::ptr const& socket(const std::string& nsp = "");

Expand All @@ -91,6 +93,7 @@ namespace sio
void operator=(client const& cl){}

client_impl* m_impl;
std::string m_path;
};

}
Expand Down

0 comments on commit e62ef11

Please sign in to comment.