准备一台云服务器,没有的小伙伴可以去撸谷歌云300刀
我这里是Ubuntu18.04
git clone https://github.com/coturn/coturn.git
不懂git的请自行百度
cd coturn
./configure --prefix=/usr/local/coturn
编译后的文件目录为/usr/local/coturn
install is /usr/bin/install
pkill is /usr/bin/pkill
sqlite3 is /usr/bin/sqlite3
Use TMP dir /var/tmp
Compiler: cc
Library option -lsocket cannot be used
Library option -lrt cannot be used
Library option -lwldap32 cannot be used
Library option -lwldap64 cannot be used
Library option -lintl cannot be used
Library option -lnsl cannot be used
Sockets code is fine: sin_len field present
pthread barriers not found
Ignore IP_RECVERR
Library option -lcrypto cannot be used
ERROR: OpenSSL Crypto development libraries are not installed properly in required location.
Abort.
需要安装相关的依赖
Ubuntu执行
apt install libssl-dev
Centos执行
yum install openssl-devel
然后再次运行
./configure --prefix=/usr/local/coturn
官方回答:https://github.com/coturn/coturn/issues/122
Libevent2 development is not installed properly
ERROR: Libevent2 development libraries are not installed properly in required location.
ERROR: may be you have just too old libevent tool - then you have to upgrade it.
See the INSTALL file.
Abort.
执行以下代码
apt-get install libevent-dev
然后再次运行
./configure --prefix=/usr/local/coturn
查看是否生成Makefile文件,如果有代表成功
ll Makefile
执行make命令 -j代表是用多少线程执行,一般双核就写4 4核就写8
make -j 16
sudo make install
进入刚刚的目录/usr/local/coturn ,编辑文件
cd /usr/local/coturn
vim etc/turnserver.conf.default
按照上面的截图依次填入信息,除了云主机的IP外,其他的都可以随便设置改完后保存
如果出现以下错误
ERROR: Empty cli-password, and so telnet cli interface is disabled! Please set a non empty cli-password!错误
需要在vim etc/turnserver.conf.default 添加
cli-password=youpasswd
对应的github 官方issue
https://github.com/coturn/coturn/issues/361
openssl req -x509 -newkey rsa:2048 -keyout /etc/turn_server_pkey.pem -out /etc/turn_server_cert.pem -days 99999 -nodes
vim etc/turnserver.conf.default
把以下的注释取消掉,注意证书的路径根据自己生成的路径填写
cert=/etc/turn_server_cert.pem
pkey=/etc/turn_server_pkey.pem
这个是限制端口范围的,最好是设置下,并且阿里云安全组和防火墙也要添加对应规则
vim etc/turnserver.conf.default
min-port=49152
max-port=65535
这里怕有小白所以写的全部路径,其实相对路径就可以了,不懂的直接复制这句话
/usr/local/coturn/bin/turnserver -o -a -f -c /usr/local/coturn/etc/turnserver.conf.defaul -r Chengdu
如果是云主机的话需要打开对应的端口,如阿里云需要在安全组添加规则3478,UDP和TCP,需要同时开启并且如果云服务开启了防火墙需要添加防火墙规则,我这里是Ubuntu,执行以下命令
sudo ufw allow 3478
最好telnet一下看下端口是否是通的。
telnet 云主机ip 3478
如果是通的打开以下地址检测
https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/
var configuration = { iceServers: [
{urls: "stun:youip:5060"},
{
urls: "turn:youip:5060",
username: 'you user',
credential: 'you passwd',
}];
};
var pc = new RTCPeerConnection(configuration);
WebRTC参考文章:https://developer.mozilla.org/zh-CN/docs/Web/API/RTCConfiguration