TURN 客户端

在 Delphi/C++Builder 中与 TURN 服务器通信。分配中继传输地址、管理权限和通道,然后与对称 NAT 后面的对等方交换数据 — 符合 RFC 8656 标准。

TsgcTURNClient

TURN 客户端 — 支持 Allocate、CreatePermission、ChannelBind、Send / Indication / Data、Refresh,所有操作均使用 RFC 8656 长期凭据签名。

组件类

TsgcTURNClient

支持平台

Windows, macOS, Linux, iOS, Android

版本

Professional / Enterprise

分配、设置权限、发送数据

设置 TURN 服务器/用户名/密码,调用 Allocate,然后对每个对等方调用 CreatePermission,再通过 Send / SendIndication 中继数据。

uses
  sgcP2P;

var
  TURN: TsgcTURNClient;
begin
  TURN := TsgcTURNClient.Create(nil);
  TURN.Host := 'turn.example.com';
  TURN.Port := 3478;
  TURN.Authentication.UserName := 'alice';
  TURN.Authentication.Password := 'secret';

  TURN.OnTURNAllocated := procedure(Sender: TObject;
    const aRelayedAddress, aRelayedPort: string)
  begin
    Memo1.Lines.Add('relay: ' + aRelayedAddress + ':' + aRelayedPort);
    TURN.CreatePermission('192.0.2.1');
    TURN.SendIndication('192.0.2.1', 5000, 'hello via TURN');
  end;

  TURN.Allocate;
end;
// uses: sgcP2P
TsgcTURNClient *TURN = new TsgcTURNClient(this);
TURN->Host = "turn.example.com";
TURN->Port = 3478;
TURN->Authentication->UserName = "alice";
TURN->Authentication->Password = "secret";
TURN->Allocate();

内部功能

符合 RFC 8656 标准的 TURN 客户端 — 当仅靠 STUN 无法穿透 NAT 时,为 WebRTC 提供中继层。

Allocate / Refresh

Allocate 发送带 REQUESTED-TRANSPORT(UDP、TCP)的分配请求并获取中继传输地址。Refresh 延长生命周期;Free 显式释放分配。

CreatePermission

CreatePermission(peerIp) 注册允许中继转发数据报的对等方。权限在 5 分钟后自动过期 — 请按需刷新。

ChannelBind

ChannelBind(channelNumber, peerEndpoint) 绕过 36 字节的 Send/Data 开销。组件自动编码 4 字节的 ChannelData 头部。

长期凭据

Authentication.UserName / Password 使用从 401 质询中提取的 realm 和 nonce 填充 MESSAGE-INTEGRITY。

TURN over TCP/TLS

在阻断 UDP 的环境中,将底层客户端的 Transport 设置为 TCP 或 TLS — 支持 RFC 6062 / 6056 传输选项。

WebRTC 就绪

TsgcRTCPeerConnectionTsgcTURNClient 作为其 IceServers 之一使用 — Allocate、CreatePermission 和 ChannelBind 的流程在后台透明进行。

规范与参考资料

该组件实现的协议权威来源。

文档与示例

直达组件参考文档、获取即用示例项目并下载试用版。

在线帮助 — TsgcTURNClient 该组件的完整属性、方法和事件参考。
示例项目 — Demos\35.P2P\03.TURN 即用示例项目,包含在 sgcWebSockets 包中 — 请从下方下载试用版。
技术文档(PDF) 功能介绍、快速入门、Delphi 与 C++ Builder 代码示例及原始参考资料 — 仅限此组件。
用户手册(PDF) 涵盖库中每个组件的综合手册。

准备好在 Delphi 中使用 TURN 了吗?

下载免费试用版,为您的 Delphi 应用程序添加基于 TURN 的中继功能。