WebRtc搭建Turn穿透服务 Peerjs


webetc视频通话搭建成功截图(Turn-Net穿透)

屏幕共享

 环境安装
sudo yum install -y make gcc cc gcc-c++ wget

sudo yum install -y openssl-devel libevent libevent-devel
Turn安装,使用github上的coturn构建:https://github.com/coturn/coturn
wget http://turnserver.open-sys.org/downloads/v4.5.1.2/turnserver-4.5.1.2.tar.gz

tar -xvzf turnserver-4.5.1.2.tar.gz

cd turnserver-4.5.1.2 && ./configure

make

sudo make install
不要使用git clone拉取,不然你会发现报C的错误

使用wget下载包,最后这是turn安装成功界面

测试是否安装成功,若有路径表示成功

which turnserver

进入配置文件夹

cd /usr/local/etc/

复制出turnserver.conf.default 为 turnserver.conf

cp turnserver.conf.default turnserver.conf

vim编辑配置文件,shift+g跳到最后一行加上以下内容

listening-ip=你的内网(ifconfig或去控制台查看)
relay-ip=你的内网
external-ip=你的外网
user=你的账号:你的密码
cli-password=你的密码(这个要加上,不然一会启动服务会报cli-password啥的)
min-port=49152
max-port=65535

然后先去你的腾讯云控制台把tcp和udp全部1-65535全部放开(只放3478也行,默认3478)

 然后启动服务

turnserver -v -r 你的公网id:3478 -a -o -c /usr/local/etc/turnserver.conf

查看是否在运行了

ps -ef|grep turnserver

启动成功

到官网测试下:https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/

输入turn://你的ip:3478、账号、密码

addserver后点下面的按钮

 看到relay和你的公网ip表示turn服务连接成功

最后是我们的前端vue代码

this.peerExa = new Peer(undefined, {
      host: "/",
      port: "3001",
      config: {
        iceServers: [
          {
            urls: "turn:ip:3478",
            username: "账号",
            credential: "密码",
          },
        ],
      },
    });

 参考:

peerjs文档

https://peerjs.com/

webrtc-turn测试连接

https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/

禁用webrtc的https安全检查

https://blog.csdn.net/yunzhonghefei/article/details/120290541

Turn服务器搭建

https://docs.wildfirechat.cn/webrtc/turn_server.html

https://www.jianshu.com/p/d65e369f44fb