在使用WebRTC進(jìn)行即時通訊時,需要使瀏覽器進(jìn)行P2P通訊,但是由于NAT環(huán)境的復(fù)雜性,并不是所有情況下都能進(jìn)行P2P,這時需要TURN Server來幫助客戶端之間轉(zhuǎn)發(fā)數(shù)據(jù)。rfc5766-turn-server是一個高性能的開源TURN Server實現(xiàn)。
以下是在EC2上使用Ubuntu操作系統(tǒng)安裝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
// 當(dāng)TURN Server用于WebRTC時,必須使用long-term credential mechanism
lt-cred-mech
// 增加一個用戶
user=username1:password1
// 設(shè)定realm
realm=mycompany.org
---------------------------------------
4. 啟動:
sudo turnserver -c /etc/turnserver.conf --daemon
5. 服務(wù)啟動后,在上一個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當(dāng)然也有STUN Server的能力,但是需要給它配置2個IP,以幫助探測客戶端所在NAT環(huán)境的行為,這里沒有做。