Tyleet
(Tyleet)
September 24, 2016, 11:28am
25
EDIT: I just tested my theory that it is an socketIO version problem since the legacy server is running 0.9. Set up some local servers on my pc one with 1.x and one for 0.9, 0.9 only throws warnings when Unreal tries to connect, 1.x accepts the connection.
So the current plugin is very bare bones, in terms of actual binding meat it’s only 40-50 lines of code , enough to have been done in a short presentation as a proof of concept:
#include "SocketIOClientPrivatePCH.h"
#include "SocketIOClientComponent.h"
USocketIOClientComponent::USocketIOClientComponent(const FObjectInitializer &init) : UActorComponent(init)
{
bWantsInitializeComponent = true;
bAutoActivate = true;
}
std::string StdString(FString UEString)
{
return std::string(TCHAR_TO_UTF8(*UEString));
}
FString FStringFromStd(std::string StdString)
{
return FString(StdString.c_str());
}
void USocketIOClientComponent::Connect(FString AddressAndPort)
{
if (!AddressAndPort.IsEmpty())
{
PrivateClient.connect(StdString(AddressAndPort));
}
else
{
PrivateClient.connect("http://localhost:3000");
}
}
void USocketIOClientComponent::Emit(FString Name, FString Data)
{
PrivateClient.socket()->emit(StdString(Name), StdString(Data));
//UE_LOG(LogTemp, Log, TEXT("Emit %s with %s"), *Name, *Data);
}
void USocketIOClientComponent::Bind(FString Name)
{
PrivateClient.socket()->on(StdString(Name), sio::socket::event_listener_aux(&](std::string const& name, sio::message::ptr const& data, bool isAck, sio::message::list &ack_resp) {
const FString SafeName = FStringFromStd(name);
const FString SafeData = FStringFromStd(data->get_string());
FFunctionGraphTask::CreateAndDispatchWhenReady(&, SafeName, SafeData]
{
On.Broadcast(SafeName, SafeData);
}, TStatId(), nullptr, ENamedThreads::GameThread);
}));
}
That is literally it. But it’s apparently useful, so I may have to look into extending it a bit
It should be a fairly easy modification to support namespaces. The current code uses
PrivateClient.socket()-> ...
which fetches the default namespace “/” and the method should just expand to add a namespace variable e.g.
FString Namespace = FString(TEXT("events"));
PrivateClient.socket(StdString(Namespace))-> ...
events.
Feel free to modify the source and make a pull request, otherwise it may take a bit of time before I make a pass on the plugin with the fixes. I’ve added a note about it in github issues on the repo.
Hi,
so I tried modifying your source code, but it seems I can’t get it to run. Even if I hardcode IP, namespace, eventname and data into the source nothing gets through to the server.
void USocketIOClientComponent::Connect(FString AddressAndPort, FString nSpace)
{
if (!AddressAndPort.IsEmpty())
{
Namespace = nSpace;
PrivateClient.connect("http://192.168.1.50:5000");//StdString(AddressAndPort));
UE_LOG(LogTemp, Log, TEXT("%s"), *nSpace);
}
...
void USocketIOClientComponent::Emit(FString Name, FString Data)
{
FString speed = FString(TEXT("100"));
FString nsp = FString(TEXT("/events"));
FString eve = FString(TEXT("unityFanSpeedEvent"));
PrivateClient.socket(StdString(nsp))->emit(StdString(eve),StdString(speed));
//PrivateClient.socket("/servo"/*StdString(Namespace)*/)->emit("enable");//StdString(Name), StdString(Data));
UE_LOG(LogTemp, Log, TEXT("Emit %s with %s"), *Name, *Data);
}
i also tried “events” or “events/” to make sure its not a problem with the namespace. Here is a code snippet from the Unity Project the Server was originally created for (written in C#):
Connect:
client = new Client("http://192.168.1.50:5000");
client.Error += SocketError;
client.Message += SocketMessage;
socket = client.Connect ("/events");
Emit:
public void EventFanSpeed(int speed) {
if (oldSpeed == speed)
return;
socket.Emit("unityFanSpeedEvent", "" + speed, null);
oldSpeed = speed;
}
Could it be that the Problem is that the server runs SocketIO 0.9?