TURN Protocol allows you to use a Relayed IP Address to exchange data between peers that are behind NATs.
To create a new Relayed IP Address on a TURN server, the client must first call the method Allocate, this method sends a Request to the TURN server to create a new Relayed IP Address, if the TURN server can create a new Relayed IP Address, the client will receive a successful response. The client will be able to communicate with other peers during the time defined in the Allocation's lifetime.
TsgcTURNClient oTURN = new TsgcTURNClient(this);
oTURN->Host = "turn.sgcwebsockets.com";
oTURN->Port = 3478;
oTURN->Allocate();
private void OnTURNAllocate(TObject *Sender, const TsgcSocketConnection *aSocket,
const TsgcSTUN_Message *aMessage, const TsgcTURN_ResponseAllocation *aAllocation)
{
DoLog("Relayed IP: " + aAllocation->RelayedIP + ". Relayed Port: " +
IntToStr(aAllocation->RelayedPort));
}
private void OnSTUNResponseError(TObject *Sender, const TsgcSTUN_Message *aMessage,
const TsgcSTUN_ResponseError *aError)
{
DoLog("Error: " + IntToStr(aError->Code) + " " + aError->Reason);
}
The lifetime can be updated to avoid expiration using the method Refresh. The Lifetime is the number of seconds to expire. If the value is zero the Allocation will be deleted.
oTURN->Refresh(600);