TURN server

TURN服务器

For most WebRTC applications to function a server is required for relaying the traffic between peers, since a direct socket is often not possible between the clients (unless they reside on the same local network). The common way to solve this is by using a TURN server. The term stands for Traversal Using Relays around NAT, and it is a protocol for relaying network traffic.

对于大多数WebRTC应用程序来说,由于客户端之间通常不可能使用直接套接字(除非它们位于同一本地网络上),因此需要服务器来在对等端之间中继流量。解决这一问题的常见方法是使用TURN服务器。该术语意思是围绕NAT使用中继的接力,它是一种用于中继网络流量的协议。

There are currently several options for TURN servers available online, both as self-hosted applications (like the open-source COTURN project) and as cloud provided services.

目前,TURN服务器有几种在线选项,既可以作为自托管应用程序(如开源COTURN项目),也可以作为云提供的服务。

Once you have a TURN server available online, all you need is the correct RTCConfiguration for your client application to use it. The following code snippet illustrates a sample configuration for a RTCPeerConnection where the TURN server has the hostname my-turn-server.mycompany.com and is running on port 19403. The configuration object also support the username and credentials properties for securing the access to the server. These are required when connecting to a TURN server.

一旦TURN服务器在线可用,所需要的就是客户端应用程序使用正确的RTCConfiguration。下面的代码片段说明了RTCPeerConnection的示例配置,其中TURN服务器的主机名为my-TURN-server.mycompany.com,并在端口19403上运行。配置对象还支持用于保护对服务器的访问的用户名和凭据属性。当连接到TURN服务器时,这些是必需的。

const iceConfiguration = {
    iceServers: [
        {
            urls: 'turn:my-turn-server.mycompany.com:19403',
            username: 'optional-username',
            credentials: 'auth-token'
        }
    ]
}

const peerConnection = new RTCPeerConnection(iceConfiguration);
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐