Websockets on Unreal Engine 4?
Asked Answered
P

2

5

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!

Preachy answered 18/2, 2016 at 7:37 Comment(4)
Did you happen to notice while you were reading the readme on the github link you gave the author(s) claim it to be "Portable/cross platform (Posix/Windows, 32/64bit, Intel/ARM/PPC)"Darmit
so you think it will work?Preachy
UEWebsocket supports all the Unreal paltforms: github.com/feixuwu/UEWebsocketHemiplegia
I notice UE4.26 already includes thirdparty libraries libWebSockets , Asio, and WebRTC each of which may be used for communications.. See C:/Program Files/Epic Games/UE_4.26/Engine/Source/ThirdParty/Bradshaw
E
6

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.

Evident answered 20/2, 2016 at 18:24 Comment(3)
Hi jmcorallo. I have my TCP Listener in a PlayerController Class (like Rama recommended) and call Launch in the constructor. First: is this the right approach? and second: when I build the project my ue4editor crashes giving me this message: 'Access violation - code c0000005 (first/second chance not available)' and is pointing at the line where the first timer is set. Can you help me with that please?Snocat
Using of websocket will limit target platform on windows and html5.Is it true?Bladder
@NikolaLukic I honestly don't know. I only used this on Windows.Evident
G
1

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.

Granite answered 19/2, 2022 at 20:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.