#include "Client.h"
#include "Room.h"
const FString UMyComponent::SocketURL = TEXT("https://my-url.com");
struct FMyMessageStruct
{
int Id;
std::string Name;
std::string Text;
// You can rename fields!
MSGPACK_DEFINE_MAP(MSGPACK_NVP("id", Id), MSGPACK_NVP("name", Name), MSGPACK_NVP("text", Text));
};
void UMyComponent::Connect()
{
ClientInstance = MakeShared<Client>(SocketURL);
ClientInstance->JoinOrCreate<void>("room_name", {{"room_option", "option_value"}},
[this](TSharedPtr<MatchMakeError> Error, TSharedPtr<Room<void>> Room)
{
if (Error)
{
UE_LOG(/* oh dear! */)
return;
}
UE_LOG(/* connected! */)
this->RoomInstance = Room;
this->RoomInstance->OnMessage("message_type",
[](const msgpack::object& Message)
{
FMyMessageStruct Event = Message.as<FMyMessageStruct>();
// Do something with the event...!
});
});
}
Big thanks to Hung Hoang for making the Cocos2D C++ implementation of this client, which the Unreal Engine implementation is based on.
MIT