Update to v0.2.1 for 4.13
-added proper binary send/receive support
-added functioning onconnect/disconnect events, no longer require
special events on your server
-added on namespace connect/disconnect events
-added on fail event
Grab it at the github repo releases url
Code changes found in this merged commit
Absolutely, I’m using this plugin in C++ for my own projects. Don’t have a C++ how to atm, but here’s a quick rundown:
To use the C++ code from the plugin add it as a dependency module in your project build.cs
PublicDependencyModuleNames.AddRange(new string] { "Core", "CoreUObject", "Engine", "InputCore", "SocketIOClient" });
Add the component to your actor or reference it from another component by getting it on begin play e.g.
SIOComponent = Cast<USocketIOClientComponent>(this->GetOwner()->GetComponentByClass(USocketIOClientComponent::StaticClass()));
if (!SIOComponent)
{
UE_LOG(LogTemp, Warning, TEXT("No sister socket IO component found"));
return;
}
else
{
UE_LOG(LogTemp, Log, TEXT("Found SIOComponent: %s"), *SIOComponent->GetDesc());
}
Connect:
//get a reference or add as subobject in your actor
USocketIOClientComponent* SIOComponent;
//the component will autoconnect, but you may wish to change the url before it does that via
SIOComponent->AddressAndPort = FString("http://127.0.0.1:3000"); //change your address
//you can also disable auto connect and connect it at your own time via
SIOComponent->ShouldAutoConnect = false;
SIOComponent->Connect(); // or SIOComponent->Connect("http://127.0.0.1:3000");
String emit:
SIOComponent->Emit(FString(TEXT("myevent")), FString(TEXT("some data or stringified json"));
note that namespaces are supported as a third optional FString parameter.
Raw Data emit:
TArray<uint8> Buffer;
//fill buffer with your data
SIOComponent->EmitBuffer(FString(TEXT("myBinarySendEvent")), Buffer.GetData(), Buffer.Num());
to receive events you can bind lambdas which makes things awesomely easy e.g.
SIOComponent->BindDataLambdaToEvent(&](const FString& Name, const FString& Data)
{
//do something with your string data
}, FString(TEXT("myStringReceiveEvent")));
Raw data receive:
SIOComponent->BindBinaryMessageLambdaToEvent(&](const FString& Name, const TArray<uint8>& Buffer)
{
//Do something with your buffer
}, FString(TEXT("myBinaryReceiveEvent")));
or if you want to deal with raw socket.io message data (this plugin doesn’t have automatic UE json support yet)
SIOComponent->BindRawMessageLambdaToEvent(&](const FString& Name, const sio::message::ptr&)
{
//do something with your sio::message::ptr data
}, FString(TEXT("myArbitraryReceiveEvent")));
see https://github.com/socketio/socket.io-client-cpp/blob/master/src/sio_message.h for details on how to deal with raw sio messages.
I’ll hopefully consolidate the lambda types later on to handle UEJson format instead which will allow you to skip the stringification step on both sides if you want to build json objects. TBC!