Skip to content
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

Closed
doganarif opened this issue Jul 27, 2019 · 16 comments
Closed

Singleton Socket #138

doganarif opened this issue Jul 27, 2019 · 16 comments

Comments

@doganarif
Copy link

How can we use our socketio client as a singleton?

@getnamo
Copy link
Owner

getnamo commented Jul 28, 2019

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 Construct SocketIO Component function as a member variable of your blueprint or it will get garbage collected. There some some minor caveats when you use it inside e.g. a game instance, those notes and slightly more docs on this feature can be found here: https://github.com/getnamo/socketio-client-ue4/tree/dev-unstable#statically-constructed-socketioclient-component

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.

@getnamo
Copy link
Owner

getnamo commented Jul 28, 2019

added in 2f5ed34. Will land as part of 1.1.0 release.

@doganarif
Copy link
Author

Thank you. When stable version will release ?

@getnamo
Copy link
Owner

getnamo commented Jul 29, 2019

When #126 gets fixed, but you can try it now with: https://github.com/getnamo/socketio-client-ue4/releases/tag/v1.1.0e.

@Sere1
Copy link

Sere1 commented Jul 30, 2019

How can i use singleton socket connection with c++ ?

@getnamo
Copy link
Owner

getnamo commented Jul 30, 2019

@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

@Sere1
Copy link

Sere1 commented Aug 1, 2019

I cant emit. Error C4800 const wchar_t

@getnamo
Copy link
Owner

getnamo commented Aug 1, 2019

Which version did you use and can you give me the whole error stack? (some surrounding code might help)

@doganarif
Copy link
Author

His my coworker . Version 4.22

@getnamo
Copy link
Owner

getnamo commented Aug 1, 2019

Sorry I mean which C++ implementation version of the two methods I listed (FSocketIONative or USocketIOClientComponent)?

@doganarif
Copy link
Author

FSocketIONative

@doganarif
Copy link
Author

Do you have Skype address we can show you our code.

@getnamo
Copy link
Owner

getnamo commented Aug 1, 2019

Just post a small snippet like ~3-5 lines around the line that causes the error (emit) + full error message

@doganarif
Copy link
Author

Theres really spaghetti. If you have time we can share you our code with Skype

@getnamo
Copy link
Owner

getnamo commented Aug 1, 2019

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.

FString MyString = TEXT("hi");

Socket->Emit(TEXT("MyEmit"), MyString);

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:
https://github.com/getnamo/socketio-client-ue4/blob/master/Source/SocketIOClient/Public/SocketIONative.h#L124-L239

@getnamo
Copy link
Owner

getnamo commented Sep 12, 2019

@getnamo getnamo closed this as completed Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants