From sgcWebSockets 4.5.2, TURN protocol is supported on Server components.
TsgcTURNServer is the server that implements the TURN Protocol and allows to process requests from TURN clients. The component inherits from TsgcSTUNServer, so all methods and properties are available on TsgcTURNServer.
TURN Server supports Long-Term Authentication, Allocation, Permissions, Channel Data and more.
Usually TURN servers runs on UDP port 3478 and require Long-Term credentials, so in order to configure a TURN server, set the listening port (by default 3478) and start the server.
Configure the server
Set the property Active = True to start the STUN server.
oTURN := TsgcTURNServer.Create(nil); oTURN.Port := 3478; oTURN.TURNOptions.Authentication.Enabled := True; oTURN.TURNOptions.Authentication.LongTermCredentials.Enabled := True; oTURN.TURNOptions.Authentication.LongTermCredentials.Realm := 'esegece.com'; oTURN.Active := True; procedure OnSTUNRequestAuthorization(Sender: TObject; const aRequest: TsgcSTUN_Message; const aUsername, aRealm: string; var Password: string); begin if (aUsername = 'user') and (aRealm = 'esegece.com') then Password := 'password'; end;
All TURN operations revolve around allocations and all TURN messages are associated with an Allocation. An allocation consists of:
When a TURN client sends an Allocate request, this TURN message is processed by server and tries to create a new Relayed Transport Address. By default, if there is any available UDP port, it will create a new Relayed Address, but you can use OnTURNBeforeAllocate event to reject a new Allocation request.
procedure OnTURNBeforeAllocate(Sender: TObject; const aSocket: TsgcSocketConnection; const aIP: string; aPort: Word; var Reject: Boolean); begin if not (your own rules) then Reject := false; end;
If the process continues, the server creates a new allocation and the event OnTURNCreateAllocation is called. This event provides information about the Allocation through the class TsgcTURNAllocationItem.
procedure OnTURNCreateAllocation(Sender: TObject; const aSocket: TsgcSocketConnection; const Allocation: TsgcTURNAllocationItem); begin DoLog('New Allocation: ' + Allocation.RelayIP + ':' + IntToStr(Allocation.RelayPort)); end;
When the Allocation expires or is deleted receiving a Refresh Request from client with a lifetime of zero, the event OnTURNDeleteAllocation event is fired.
procedure OnTURNDeleteAllocation(Sender: TObject; const aSocket: TsgcSocketConnection; const Allocation: TsgcTURNAllocationItem); begin DoLog('Allocation Deleted: ' + Allocation.RelayIP + ':' + IntToStr(Allocation.RelayPort)); end;
When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.