Search code examples
c++websocketstatic-librariesstatic-linkingunreal-engine4

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!


Solution

  • 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.