在使用WebRTC進行即時通訊時,需要使瀏覽器進行P2P通訊,但是由于NAT環境的復雜性,并不是所有情況下都能進行P2P,這時需要TURN Server來幫助客戶端之間轉發數據。rfc5766-turn-server是一個高性能的開源TURN Server實現。
以下是在EC2上使用Ubuntu操作系統安裝rfc5766-turn-server:
1. 下載安裝包:
$ wget http://ftp.cn.debian.org/debian/pool/main/r/rfc5766-turn-server/rfc5766-turn-server_3.2.4.4-1_amd64.deb
2. 安裝:
$ sudo apt-get update
$ sudo apt-get install gdebi-core
$ sudo gdebi rfc5766-turn-server_3.2.4.4-1_amd64.deb
安裝完后,在/usr/share/doc/rfc5766-turn-server下有很多文檔可參考。
3. 配置:
$ sudo vi /etc/turnserver.conf
---------------------------------------
// 配置IP,EC2下需要配置listening-ip和external-ip
listening-ip=172.31.4.37
external-ip=54.223.149.60
// 當TURN Server用于WebRTC時,必須使用long-term credential mechanism
lt-cred-mech
// 增加一個用戶
user=username1:password1
// 設定realm
realm=mycompany.org
---------------------------------------
4. 啟動:
sudo turnserver -c /etc/turnserver.conf --daemon
5. 服務啟動后,在上一個WebRTC示例中更改iceServers后測試:
"iceServers": [{
"url": "stun:stun.l.google.com:19302"
}, {
"url": "turn:54.223.149.60",
"username": "username1",
"credential": "password1"
}]
更多安裝信息在:http://turnserver.open-sys.org/downloads/v3.2.4.4/INSTALL
rfc5766-turn-server當然也有STUN Server的能力,但是需要給它配置2個IP,以幫助探測客戶端所在NAT環境的行為,這里沒有做。