webRTC

WebRTC (Web Real-Time Communications) 是一项实时通讯技术,它允许网络应用或者站点,在不借助中间媒介的情况下,建立浏览器之间点对点(Peer-to-Peer)的连接,实现视频流和(或)音频流或者其他任意数据的传输。WebRTC包含的这些标准使用户在无需安装任何插件或者第三方的软件的情况下,创建点对点(Peer-to-Peer)的数据分享和电话会议成为可能。

具体流程参考下图(图片来自 简书):

在本地做连接测试:

1.创建两个本地连接:

var localPeerConnection, remotePeerConnection, sendChannel, receiveChannel;
localPeerConnection = new RTCPeerConnection(null, {
optional: [{
RtpDataChannels: true
}]
});
remotePeerConnection = new RTCPeerConnection(null, {
optional: [{
RtpDataChannels: true
}]
});

2.注册icecndidate方法

localPeerConnection.onicecandidate = function(event) {
if (event.candidate) {
remotePeerConnection.addIceCandidate(event.candidate);
}
};
remotePeerConnection.onicecandidate = function(event) {
if (event.candidate) {
localPeerConnection.addIceCandidate(event.candidate);
}
};

3.注册发送端方法

sendChannel = localPeerConnection.createDataChannel("CHANNEL_NAME", {
reliable: false
}); sendChannel.onopen = function(event) {
var readyState = sendChannel.readyState;
if (readyState == "open") {
sendChannel.send("Hello");
}
};

4.注册接收端方法

remotePeerConnection.ondatachannel = function(event) {
receiveChannel = event.channel;
receiveChannel.onmessage = function(event) {
alert(event.data);
};
};

5.交换offer

localPeerConnection.createOffer(offerOptions).then(function(desc) {
localPeerConnection.setLocalDescription(desc);
remotePeerConnection.setRemoteDescription(desc);
remotePeerConnection.createAnswer(offerOptions).then(function(answera) {
remotePeerConnection.setLocalDescription(answera);
localPeerConnection.setRemoteDescription(answera);
});
});

这样连接就构建完成 ,成功后信息展示如下:

完整代码:

var localPeerConnection, remotePeerConnection, sendChannel, receiveChannel;
localPeerConnection = new RTCPeerConnection(null, {
optional: [{
RtpDataChannels: true
}]
});
all = localPeerConnection
localPeerConnection.onicecandidate = function(event) {
console.log("one", event)
if (event.candidate) {
remotePeerConnection.addIceCandidate(event.candidate);
}
}; sendChannel = localPeerConnection.createDataChannel("CHANNEL_NAME", {
reliable: false
}); sendChannel.onopen = function(event) {
var readyState = sendChannel.readyState;
if (readyState == "open") {
sendChannel.send("Hello");
}
}; remotePeerConnection = new RTCPeerConnection(null, {
optional: [{
RtpDataChannels: true
}]
}); remotePeerConnection.onicecandidate = function(event) {
console.log("two", event)
if (event.candidate) {
localPeerConnection.addIceCandidate(event.candidate);
}
}; remotePeerConnection.ondatachannel = function(event) {
receiveChannel = event.channel;
receiveChannel.onmessage = function(event) {
alert(event.data);
};
};
var offerOptions={
offerToRecceiveAudio: true,
offerToReceiveVideo: true
};
localPeerConnection.createOffer(offerOptions).then(function(desc) {
console.log("desc", desc)
localPeerConnection.setLocalDescription(desc);
remotePeerConnection.setRemoteDescription(desc);
remotePeerConnection.createAnswer(offerOptions).then(function(answera) {
console.log("answera", answera)
remotePeerConnection.setLocalDescription(answera);
localPeerConnection.setRemoteDescription(answera);
});
});

简单的webRTC连接测试的更多相关文章

  1. jsp实时显示后台批处理进度 - out分块,简单的长连接方式

    这两天在实现一个批处理操作,但是想让前台实时显示后台批处理进度,本想着用复杂一些的框架可以实现异步信息调用 但是鉴于是内部管理系统,且只有一两个人用到这个功能,所以做了一个简单的长连接方式的实时响应 ...

  2. websocket 70K连接测试

    websocket 70K连接测试 最近使用socket.io做了一个实时应用,实时性.稳定性还是很让人满意的.如果拿socket.io来做小型应用,综合效率应该是最高的.但是网上少有socket.i ...

  3. jmeter JDBC请求连接测试mysql数据库

    所有jmeter基本组件功能本文不做介绍.jmeter要链接mysql数据库,首先得下载mysql jdbc驱动包(注:驱动包的版本一定要与你数据库的版本匹配,驱动版本低于mysql版本有可能会导致连 ...

  4. 3-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案数据篇(安装配置数据库,使用Navicat for MySQL和手机APP 连接测试)

    2-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案数据篇(数据库简单说明) https://www.mysql.com/ 咱用安装版的 我把自己下载的放在了这里 现在 ...

  5. 简单网络搭建与测试 mininet

    简介 本实验是基于pox搭建简单的网络并测试网络的连通性,利用mininet代码创建一个交换机四个主机的拓扑,测试各主机之间的连通性以及h1.h4之间的带宽. 代码 实验代码如下所示,SingleSw ...

  6. EMQ ---100万线连接测试说明

    注解 EMQ 2.0 消息服务器默认设置,允许最大客户端连接是512,因为大部分操作系统 ‘ulimit -n’ 限制为1024. EMQ 消息服务器1.1.3版本,连接压力测试到130万线,8核心/ ...

  7. Go语言之从0到1实现一个简单的Redis连接池

    Go语言之从0到1实现一个简单的Redis连接池 前言 最近学习了一些Go语言开发相关内容,但是苦于手头没有可以练手的项目,学的时候理解不清楚,学过容易忘. 结合之前组内分享时学到的Redis相关知识 ...

  8. 自定义一个简单的JDBC连接池

    一.什么是JDBC连接池? 在传统的JDBC连接中,每次获得一个Connection连接都需要加载通过一些繁杂的代码去获取,例如以下代码: public static Connection getCo ...

  9. 转 Swoole】用swoole简单实现MySQL连接池

    MySQL连接池 在传统的网站开发中,比如LNMP模式,由Nginx的master进程接收请求然后分给多个worker进程,每个worker进程再链接php-fpm的master进程,php-fpm再 ...

随机推荐

  1. Codeforces Round #666 (Div. 2) Power Sequence、Multiples of Length 思维

    题目链接:Power Sequence 题意: 给你n个数vi,你可以对这个序列进行两种操作 1.可以改变其中任意个vi的位置,无成本 2.可以对vi进行加1或减1,每次操作成本为1 如果操作之后的v ...

  2. Codeforces Round #570 (Div. 3) B. Equalize Prices、C. Computer Game、D. Candy Box (easy version)、E. Subsequences (easy version)

    B题题意: 给你n个物品的价格,你需要找出来一个值b,使得每一个物品与这个b的差值的绝对值小于k.找到最大的b输出,如果找不到,那就输出-1 题解: 很简单嘛,找到上下限直接二分.下限就是所有物品中最 ...

  3. Codeforces Round#630 div2 A~C题解

                                                                                                        ...

  4. Codeforces Beta Round #19 D. Points

    Description Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a C ...

  5. Codeforces Round #649 (Div. 2) A. XXXXX (贪心)

    题意:有一个长度为\(n\)的数组,找一段最长子数组,使得其元素和为\(x\),如果存在,输出子数组的长度,否则输出\(-1\). 题解:这题我们要从元素和\(sum\)来考虑,首先,如果原数组的所有 ...

  6. 使用开源量子编程框架ProjectQ打印编译后的量子线路与绘制线路图

    技术背景 在量子计算领域,基于量子芯片的算法设计(或简称为量子算法)是基于量子线路来设计的,类似于传统计算中使用的与门和非门之类的逻辑门.因此研究一个量子线路输入后的编译(可以简化为数量更少的量子门组 ...

  7. HttpClient&&RestTemplate学习

    1. 什么是HttpClient HttpClient是Apache下面的子项目,可以提供高效的,最新的,功能丰富的支持HTTP协议的客户端编程工具包. 2. 为什么要学习HttpClient Htt ...

  8. 力扣567.字符串的排列—C语言实现

    题目 来源:力扣(LeetCode)

  9. OpenStack-知识点补充

    登录计算节点查看进程 [root@compute ~]# ps aux | grep kvm root 824 0.0 0.0 0 0 ? S< 10:19 0:00 [kvm-irqfd-cl ...

  10. spring再学习之设计模式

    今天我们来聊一聊,spring中常用到的设计模式,在spring中常用的设计模式达到九种. 第一种:简单工厂 三种工厂模式:https://blog.csdn.net/xiaoddt/article/ ...