本文讲解如何在 Ubuntu 系统上采用coturn搭建 p2p 打洞服务器,coturn 是一个开源的 stun 和 turn 服务器实现。
1. 安装依赖项
1 2 3
   | sudo apt-get install openssl sudo apt-get install libssl sudo apt-get install libevent-dev
   | 
 
2. 下载 coturn 源码编译安装
1 2
   | git clone https://github.com/coturn/coturn.git ./configure && make && make install
   | 
 
3. 编辑配置
安装完之后,编辑\usr\local\etc\turnserver.conf配置文件:
1 2 3 4 5
   | listening-device=eth0 listening-port=3478 external-ip=47.93.42.10     #公网IP user=jeff:123456                #用户名密码 realm=rtctest
   | 
 
4. 启动 coturn
1
   | turnserver -a -f -v -r rtctest
   | 
 
5. 客户端连接 coturn 服务器
webrtc::PeerConnectionFactoryInterface::CreatePeerConnection函数传入的RTCConfiguration 参数如下:
1 2 3 4 5 6 7 8 9
   | webrtc::PeerConnectionInterface::RTCConfiguration config; config.sdp_semantics = webrtc::SdpSemantics::kUnifiedPlan; config.enable_dtls_srtp = true; webrtc::PeerConnectionInterface::IceServer server; server.urls.push_back("stun://47.93.42.10:3478"); server.urls.push_back("turn://47.93.42.10:3478"); server.username = "jeff"; server.password = "123456"; config.servers.push_back(server);
   |