简体   繁体   中英

Websockets on Unreal Engine 4?

I am getting started with Unreal Engine 4 . I come from Libgdx and I am familiarized using WebSockets clients in my games and NodeJS with 'ws' on the server.

How ever, I can't find information about Websockets and Unreal Engine 4.

I know that given that it is programmed with C++ you can add external static libraries to the unreal project.

Can I use this c++ websocket library?

https://github.com/zaphoyd/websocketpp

Will it run on Windows, Mac and console?

I am not an expert of c++ and static libraries. Please help, thanks!

You can follow this tutorial on TCP Sockets.

You will need to make some changes on the code, as it doesn't run on UE 4.10 (the tutorial is originally from 2014).

On the .h file define 2 timer handles:

FTimerHandle TimerHandle_Connection;
FTimerHandle TimerHandle_Socket;

On the .cpp file, inside StartTCPReceiver(...) change the line where the timer is set to:

GetWorldTimerManager().SetTimer(TimerHandle_Connection, this, &AYourClass::TCPConnectionListener, 0.01, true);

and on TCPConnectionListener(...) change the line where the timer is set to:

GetWorldTimerManager().ClearTimer(TimerHandle_Connection);//optional, only if you want to stop listening for new connections
GetWorldTimerManager().SetTimer(TimerHandle_Socket, this, &AYourClass::TCPSocketListener, 0.01, true);

(Another option would be to thread these functions instead of having them in timers)

Just in case, if you are new to UE, don't add the code directly on the IDE. Go to the Content Browser > Add New > New C++ Class. You can create a new class that inherits from Actor, and when you want to start to listen to connections, you spawn that Actor.

You can use any websocket or third party asset with unreal engine. You can simply add the headers in your Build.cs file using PrivateIncludePathModuleNames for example (there's also a public include path) and it takes an array of strings where each string is a folder essentially. If you want to add a library (lib) file you can just add it like this:

if (Target.Platform == UnrealTargetPlatform.Win32 ||
            Target.Platform == UnrealTargetPlatform.Win64)
{
            PublicSystemLibraries.Add("crypt32.lib");
}

You can also add full paths here. If you want to do a delayed load you can just use PublicDelayLoadedDlls.Add("name") <- I may have the syntax wrong on this one but it's easy to google it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM