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!

chelo_c
  • 1,501
  • 2
  • 19
  • 29
  • 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)" – ChiefTwoPencils Feb 18 '16 at 08:07
  • so you think it will work? – chelo_c Feb 18 '16 at 08:56
  • UEWebsocket supports all the Unreal paltforms: https://github.com/feixuwu/UEWebsocket – SushiHangover Dec 28 '19 at 22:44
  • 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/` – zipzit Apr 17 '21 at 16:31

1 Answers1

5

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.

jmcorallo
  • 324
  • 4
  • 11
  • 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? – gerric May 21 '16 at 00:29
  • Using of websocket will limit target platform on windows and html5.Is it true? – Nikola Lukic Feb 01 '17 at 21:05
  • @NikolaLukic I honestly don't know. I only used this on Windows. – jmcorallo Feb 04 '17 at 23:48