-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Singleton Socket #138
Comments
This wasn't supported in Blueprint in the release when you asked so I had a quick look to see what would be needed implement the feature since it was often requested. I've added a new function which is globally available and this will use the correct context to initialize the component. You can then use it e.g. like this: Note that it's important to save the return value from the Made an early release for testing available here: https://github.com/getnamo/socketio-client-ue4/releases/tag/v1.1.0e. Let me know if that implementation works for your use case. |
added in 2f5ed34. Will land as part of 1.1.0 release. |
Thank you. When stable version will release ? |
When #126 gets fixed, but you can try it now with: https://github.com/getnamo/socketio-client-ue4/releases/tag/v1.1.0e. |
How can i use singleton socket connection with c++ ? |
@Sere1 for c++ I would recommend you stick with FSocketIONative e.g. if you wanted your own c++ game instance with a socket.io connection you'd do something like #include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "SocketIONative.h"
#include "SIOTestGameInstance.generated.h"
UCLASS()
class SIOCLIENT_API USIOTestGameInstance : public UGameInstance
{
GENERATED_BODY()
virtual void Init() override;
virtual void Shutdown() override;
TSharedPtr<FSocketIONative> Socket;
}; .cpp #include "SIOTestGameInstance.h"
#include "SocketIOClient.h"
void USIOTestGameInstance::Init()
{
Super::Init();
Socket= ISocketIOClientModule::Get().NewValidNativePointer();
Socket->Connect("http://localhost:3000", nullptr, nullptr);
Socket->OnEvent(TEXT("MyEvent"), [this](const FString& Event, const TSharedPtr<FJsonValue>& Message)
{
UE_LOG(LogTemp, Log, TEXT("Received: %s"), *USIOJConvert::ToJsonString(Message));
});
Socket->Emit(TEXT("MyEmit"), FString("hi"));
}
void USIOTestGameInstance::Shutdown()
{
Super::Shutdown();
if (Socket.IsValid())
{
ISocketIOClientModule::Get().ReleaseNativePointer(Socket);
Socket = nullptr;
}
} But if you really wanted a statically constructed SocketIO actor component in c++ you can now do: .h #include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "SocketIOClientComponent.h"
#include "SIOTestGameInstance.generated.h"
UCLASS()
class SIOCLIENT_API USIOTestGameInstance : public UGameInstance
{
GENERATED_BODY()
virtual void Init() override;
virtual void Shutdown() override;
UPROPERTY()
USocketIOClientComponent* SIOComponent;
}; .cpp #include "SIOTestGameInstance.h"
#include "SocketIOClient.h"
#include "SocketIOFunctionLibrary.h"
void USIOTestGameInstance::Init()
{
Super::Init();
//Store result in a UPROPERTY variable
SIOComponent = USocketIOFunctionLibrary::ConstructSocketIOComponent(this);
SIOComponent->Connect("http://localhost:3000", nullptr, nullptr);
SIOComponent->OnNativeEvent(TEXT("MyEvent"), [this](const FString& Event, const TSharedPtr<FJsonValue>& Message)
{
UE_LOG(LogTemp, Log, TEXT("Received: %s"), *USIOJConvert::ToJsonString(Message));
});
SIOComponent->EmitNative(TEXT("MyEmit"), FString("hi"));
}
void USIOTestGameInstance::Shutdown()
{
Super::Shutdown();
} Added examples to documentation: https://github.com/getnamo/socketio-client-ue4/blob/master/README.md#example-fsocketionative-custom-game-instance and https://github.com/getnamo/socketio-client-ue4/blob/master/README.md#example-c-static-construct-actor-component-inside-custom-game-instance |
I cant emit. Error C4800 const wchar_t |
Which version did you use and can you give me the whole error stack? (some surrounding code might help) |
His my coworker . Version 4.22 |
Sorry I mean which C++ implementation version of the two methods I listed (FSocketIONative or USocketIOClientComponent)? |
FSocketIONative |
Do you have Skype address we can show you our code. |
Just post a small snippet like ~3-5 lines around the line that causes the error (emit) + full error message |
Theres really spaghetti. If you have time we can share you our code with Skype |
Off the top of my head the error points to you sending a raw string when emit only has FString overloaded so make sure your second parameter is an FString, not a raw string.
update: just tested it, you do need to use FString wrapper e.g. FString() for the second param, but not the first due to ambiguity. Otherwise it will cast the second param as a boolean by default. This will be fixed in 1.1.0: #140 so you will be able to use TEXT() as second parameter then. you can see all the supported emits here: |
How can we use our socketio client as a singleton?
The text was updated successfully, but these errors were encountered: