背景是webrtc官方提供了一个测试界面,测试webrtc的turn、stun的联通性。

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

最近我自己部署了turnserver,然后就很好奇这个东西是怎么生效的,万一背后有个程序收集我部署的账号密码那不就很麻烦。
然后就看了一下里面的代码。

看了一下,大概就是建立一个连接,然后看看有没有错误。
理解原理之后,我重新写了一个更简单的检查界面…

输入参数

ys_server = [{"username": "yeshen_test", "credential": "yeshen_test", "urls": ["turn:localhost:3478"]}, {"username": "yeshen_test", "credential": "yeshen_test", "urls": ["stun:localhost:3478"]}
]

连接测试

pc = new RTCPeerConnection({iceServers: ys_server
});
pc.onicecandidate = iceCallback;
pc.onicegatheringstatechange = gatheringStateChange;
pc.onicecandidateerror = iceCandidateError;
pc.createOffer({offerToReceiveAudio: 1,offerToReceiveVideo: 1
}).then(gotDescription,noDescription
);

完整模拟代码

<!DOCTYPE html>
<html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1" /><title>serverless webrtc</title><meta name="author" content="hello@yeshen.org"><style type="text/css">div {padding: 8px;}#ice_use_time {font-size: 14px;line-height: 20px;background: #f1f1f1;}#ice_candidate {font-size: 14px;line-height: 20px;background: beige;margin-top: 13px;}#ice_error {font-size: 14px;line-height: 20px;background: white;margin-top: 13px;color: firebrick;margin-top: 13px;}</style>
</head><body onload="connect()"><div id="ice_use_time"></div><div id="ice_candidate"></div><div id="ice_error"></div><div id="rtc_offer"></div>
</body>
<script type="text/javascript">let begin;let pc;// Parse the uint32 PRIORITY field into its constituent parts from RFC 5245,// type preference, local preference, and (256 - component ID).// ex: 126 | 32252 | 255 (126 is host preference, 255 is component ID 1)function formatPriority(priority) {return [priority >> 24,(priority >> 8) & 0xFFFF,priority & 0xFF].join(' | ');}function iceCallback(event) {if (event.candidate) {if (event.candidate.candidate === '') {return;}const {candidate} = event;ice_callback = 'candidaten=>';ice_callback += "component:" + candidate.component;ice_callback += ",type:" + candidate.type;ice_callback += ",foundation:" + candidate.foundation;ice_callback += ",protocol:" + candidate.protocol;ice_callback += ",address:" + candidate.address;ice_callback += ",port:" + candidate.port;ice_callback += ",priority:" + formatPriority(candidate.priority);ice_callback += "\n";document.getElementById('ice_candidate').innerText += ice_callback;} else if (!('onicegatheringstatechange' in RTCPeerConnection.prototype)) {console.log('done if its done in the icegatheringstatechange callback.');pc.close();pc = null;}}function gatheringStateChange() {if (pc.iceGatheringState !== 'complete') {return;}const elapsed = ((window.performance.now() - begin) / 1000).toFixed(3);document.getElementById('ice_use_time').innerText = 'ice gathering complete elapsed : ' + elapsed;pc.close();pc = null;}function iceCandidateError(e) {msg = 'The server ' + e.url +' returned an error with code=' + e.errorCode + ':\n' +e.errorText + '\n';document.getElementById('ice_error').innerText += msg;}function gotDescription(desc) {begin = window.performance.now();pc.setLocalDescription(desc);console.log(desc);document.getElementById('rtc_offer').innerText += desc.sdp;}function noDescription(error) {msg = 'Error creating offer: ' + error;document.getElementById('ice_error').innerText += msg;}function connect() {ys_server = [{"username": "yeshen_test", "credential": "yeshen_test", "urls": ["turn:localhost:3478"]}, {"username": "yeshen_test", "credential": "yeshen_test", "urls": ["stun:localhost:3478"]}]console.log('config', ys_server);pc = new RTCPeerConnection({iceServers: ys_server});pc.onicecandidate = iceCallback;pc.onicegatheringstatechange = gatheringStateChange;pc.onicecandidateerror = iceCandidateError;pc.createOffer({offerToReceiveAudio: 1,offerToReceiveVideo: 1}).then(gotDescription,noDescription);}
</script></html>

弄下来还廷简单的,玩~

trickle_ice 原理相关推荐

  1. UUID的使用及其原理

    今天敲项目要用UUID,想起之前老师告诉UUID的使用,但没说具体的生成逻辑,于是我进行了百度 首先,UUID的使用: //生成随机的UUID String uuid = UUID.randomUUI ...

  2. etcd 笔记(01)— etcd 简介、特点、应用场景、常用术语、分布式 CAP 理论、分布式原理

    1. etcd 简介 etcd 官网定义: A highly-available key value store for shared configuration and service discov ...

  3. git原理及常见使用方法

    Git 原理入门-来自阮一峰 Git 是最流行的版本管理工具,也是程序员的必备技能之一. 即使天天使用它,很多人也未必了解它的原理.Git 为什么可以管理版本?git add.git commit这些 ...

  4. 微机原理—定时计数控制接口

    别看题目很高深,其实就是很简单的定时器和计数器而已. 通常用手机定个闹钟,就是定时器的使用. 工厂里通过传送带上安装传感器,传感器传输给计算机的信号用来计数. 这是一些很简单的应用,通过很小的一个芯片 ...

  5. 三层交换机原理:01路由器如何隔离广播域?

    前言: 当网络规模较大的时候,需要设备来隔离广播域,防止网络中因产生广播风暴而导致网络效率降低,而二层交换机不能隔离广播域,所以需要三层路由器设备来隔离广播域! 但三层路由器为什么能够隔离广播域,是如 ...

  6. CRF(条件随机场)与Viterbi(维特比)算法原理详解

    摘自:https://mp.weixin.qq.com/s/GXbFxlExDtjtQe-OPwfokA https://www.cnblogs.com/zhibei/p/9391014.html C ...

  7. BiLSTM-CRF学习笔记(原理和理解) 维特比

    https://www.zhihu.com/question/20136144 维特比详解 BiLSTM-CRF 被提出用于NER或者词性标注,效果比单纯的CRF或者lstm或者bilstm效果都要好 ...

  8. 【Learning Notes】线性链条件随机场(CRF)原理及实现

    1. 概述 条件随机场(Conditional Random Field, CRF)是概率图模型(Probabilistic Graphical Model)与区分性分类( Discriminativ ...

  9. Jieba分词原理与解析

    1 HMM模型 马尔科夫过程: 以天气判断为例:引出隐马尔科夫模型 于是我们可以将这种类型的过程建模为有一个隐藏的马尔科夫过程和一个与这个隐藏马尔科夫过程概率相关的并且可以观察到的状态集合.这就是本文 ...

最新文章

  1. DIV+CSS星号(*)选择器
  2. oracle 对象管理 01_用户及权限
  3. Docker的windows家庭版安装
  4. linux下安装微信wechat
  5. Cannot resolve de.codecentric:spring-boot-admin-starter-server:2.4.0-SNAPSHOT
  6. KUKA profesafe
  7. stylus之内置方法(Built-in Functions)
  8. 初步使用计算机说课,初步认识计算机说课稿
  9. gitlab windows安装_【Thrift】Windows编译Thrift源码及其依赖库
  10. 人工智能——框架表示法
  11. 如何将Eclipse 的JavaWeb工程部署到Tomcat的webapps目录下
  12. win10计算机的数字小键盘,Win10开机默认开启数字小键盘的方法
  13. 绩效考核如何尽量公正
  14. 关于 打印页面 图片被截断
  15. HTML 和 CSS 重构网页 (Steam主页)
  16. 跨账号迁移阿里云镜像
  17. 炒币疯狂的背后,如何解决区块链技术落地问题?
  18. uiautomatorviewer无法启动
  19. 消息队列的全双工通信
  20. 爬虫小项目之爬取赛尔号

热门文章

  1. Java 编程问题:二、对象、不变性和`switch`表达式
  2. C++ register 关键字
  3. Android supports-screens 屏幕适配
  4. .sh文件规则 .sh文件执行方法
  5. BSV 上的 zk-SNARKs
  6. 如何实现外网访问内网ip?公网端口映射或内网映射来解决
  7. 集成显卡和独立显卡哪个好 集成显卡与独立显卡区别
  8. 教师职业道德与专业发展
  9. 东网科技、Rancher Labs联合发布“容器+虚拟化”双引擎超融合平台HOR
  10. openCV minMaxLoc