2024年webRTC(十):webrtc 实现web端对端视频(1),2024年最新前端性能优化方面

刷面试题

刷题的重要性,不用多说。对于应届生或工作年限不长的人来说,刷面试题一方面能够尽可能地快速自己对某个技术点的理解,另一方面在面试时,有一定几率被问到相同或相似题,另外或多或少也能够为自己面试增加一些自信心,可见适当的刷题是很有必要的。

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

  • 前端字节跳动真题解析

  • 【269页】前端大厂面试题宝典

最后平时要进行自我分析与评价,做好职业规划,不断摸索,提高自己的编程能力和抽象思维能力。大厂面试远没有我们想的那么困难,摆好心态,做好准备,你也可以的。

socket.on(‘bye’,(roomid,id)=>{

state = ‘joined_unbind’;

closePeerConnection();

console.log(“reveive bye message:state=”,state);

});

socket.on(‘disconnect’, (socket) => {

console.log(‘receive disconnect message!’, roomid);

if(!(state === ‘leaved’)){

closePeerConnection();

closeLocalMedia();

}

state = ‘leaved’;

});

socket.on(‘message’,(roomid,id,data)=>{

//媒体协商

if(data){

if(data.type === ‘offer’){

pc.setRemoteDescription(new RTCSessionDescription(data));

pc.createAnswer()

.then(getAnswer)

.catch(handleAnswerError);

}else if(data.type === ‘answer’){

console.log(“reveive client message=====>”,data);

pc.setRemoteDescription(new RTCSessionDescription(data));

}else if(data.type === ‘candidate’){

var candidate = new RTCIceCandidate({

sdpMLineIndex:data.label,

candidate:data.candidate

});

pc.addIceCandidate(candidate);

}else{

console.error(‘the message is invalid!’,data)

}

}

console.log(“reveive client message”,roomid,id,data);

});

//加入房间

socket.emit(‘join’,roomid);

return;

}

  • 创建本地流媒体链接

//创建本地流媒体链接

function createPeerConnection(){

console.log(‘create RTCPeerConnection!’);

if(!pc){

pc = new RTCPeerConnection(pcConfig);

pc.onicecandidate = (e) =>{

if(e.candidate){

sendMessage(roomid,{

type:‘candidate’,

label:e.candidate.sdpMLineIndex,

id:e.candidate.sdpMid,

candidate:e.candidate.candidate

});

}

}

pc.ontrack = (e)=>{

remoteVideo.srcObject = e.streams[0];

}

}

if(pc === null || pc === undefined){

console.error(‘pc is null or undefined!’);

return;

}

if(localStream === null || localStream === undefined){

console.error(‘localStream is null or undefined!’);

return;

}

if(localStream){

localStream.getTracks().forEach((track)=>{

pc.addTrack(track,localStream);

})

}

}

//使用turn穿越

var pcConfig={

‘iceServers’:[{

‘urls’:‘turn:121.41.76.43:3478’,

‘credential’:‘123456’,

‘username’:‘huang’

}]

}

  • 接收远端流通道

//接收远端流通道

function call(){

if(state === ‘joined_conn’){

if(pc){

var options = {

offerToReceiveAudio:1,

offerToReceiveVideo:1

}

pc.createOffer(options)

.then(getOffer)

.catch(handleOfferError);

}

}

}

  • 关闭本地媒体流链接

//关闭本地媒体流链接

function closePeerConnection(){

console.log(‘close RTCPeerConnection!’);

if(pc){

pc.close();

pc = null;

}

}

效果


在这里插入图片描述


源码


  • html
WebRTC PeerConnection

Connect Sig Server

Leave

Local:

Remote:

  • js

‘use strict’

var localVideo = document.querySelector(‘video#localvideo’);

var remoteVideo = document.querySelector(‘video#remotevideo’);

var btnConn = document.querySelector(‘button#connserver’);

var btnLeave = document.querySelector(‘button#leave’);

var localStream = null;

var roomid = ‘111111’;

var socket =null;

var state = ‘init’;

var pc = null;

var pcConfig={

‘iceServers’:[{

‘urls’:‘turn:121.41.76.43:3478’,

‘credential’:‘123456’,

‘username’:‘huang’

}]

}

function sendMessage(roomid,data){

if(socket){

socket.emit(‘message’,roomid,data);

}

}

function getAnswer(desc){

pc.setLocalDescription(desc);

sendMessage(roomid,desc);

}

function handleAnswerError(err){

console.error(‘Failed to get Answer!’,err);

}

function getOffer(desc){

pc.setLocalDescription(desc);

sendMessage(roomid,desc)

}

function handleOfferError(err){

console.error(‘Failed to get Offer!’,err);

}

//接收远端流通道

function call(){

if(state === ‘joined_conn’){

if(pc){

var options = {

offerToReceiveAudio:1,

offerToReceiveVideo:1

}

pc.createOffer(options)

.then(getOffer)

.catch(handleOfferError);

}

}

}

// 第一步:开始服务

function connSignalServer(){

//开启本地视频

start();

return true;

}

function conn(){

//1 触发socke连接

socket = io.connect();

//2 加入房间后的回调

socket.on(‘joined’,(roomid,id)=>{

state = ‘joined’;

createPeerConnection();

btnConn.disabled = true;

btnLeave.disabled =false;

console.log(“reveive joined message:state=”,state);

});

socket.on(‘otherjoin’,(roomid,id)=>{

if (state === ‘joined_unbind’) {

createPeerConnection();

}

state = ‘joined_conn’;

//媒体协商

call();

console.log(“reveive otherjoin message:state=”,state);

});

socket.on(‘full’,(roomid,id)=>{

console.log('receive full message ', roomid, id);

closePeerConnection();

closeLocalMedia();

state = ‘leaved’;

btnConn.disabled = false;

btnLeave.disabled = true;

console.log(“reveive full message:state=”,state);

alert(“the room is full!”);

});

socket.on(‘leaved’,(roomid,id)=>{

state = ‘leaved’;

socket.disconnect();

btnConn.disabled = false;

btnLeave.disabled = true;

console.log(“reveive leaved message:state=”,state);

});

socket.on(‘bye’,(roomid,id)=>{

state = ‘joined_unbind’;

closePeerConnection();

console.log(“reveive bye message:state=”,state);

});

socket.on(‘disconnect’, (socket) => {

console.log(‘receive disconnect message!’, roomid);

if(!(state === ‘leaved’)){

closePeerConnection();

closeLocalMedia();

}

state = ‘leaved’;

});

socket.on(‘message’,(roomid,id,data)=>{

//媒体协商

if(data){

if(data.type === ‘offer’){

pc.setRemoteDescription(new RTCSessionDescription(data));

pc.createAnswer()

.then(getAnswer)

.catch(handleAnswerError);

}else if(data.type === ‘answer’){

console.log(“reveive client message=====>”,data);

pc.setRemoteDescription(new RTCSessionDescription(data));

}else if(data.type === ‘candidate’){

var candidate = new RTCIceCandidate({

sdpMLineIndex:data.label,

candidate:data.candidate

});

pc.addIceCandidate(candidate);

}else{

console.error(‘the message is invalid!’,data)

}

}

console.log(“reveive client message”,roomid,id,data);

});

socket.emit(‘join’,roomid);

return;

}

// 扑捉本地视频

function getMediaStream(stream){

localStream =stream;

//2 =显示本地视频=

localVideo.srcObject = localStream;

//这个函数的调用时机特别重要 一定要在getMediaStream之后再调用,否则会出现绑定失败的情况

conn();

}

function handleError(err){

if(err){

console.error(“getUserMedia error:”,err);

}

}

// 第二步:采集本地视频

function start(){

if (!navigator.mediaDevices||

!navigator.mediaDevices.getUserMedia) {

console.log(“getUserMedia is not supported!”)

return;

} else {

//1 =配置音视频参数=

var constraints={

video:true,

audio: true

}

navigator.mediaDevices.getUserMedia(constraints)

.then(getMediaStream)

.catch(handleError)

}

}

//关闭流通道

function closeLocalMedia(){

if (localStream&&localStream.getTracks()) {

localStream.getTracks().forEach((track)=>{

track.stop();

});

}

localStream = null;

}

function leave(){

if(socket){

socket.emit(‘leave’,roomid);

}

//释放资源

closePeerConnection();

closeLocalMedia();

btnConn.disabled = false;

btnLeave.disabled = true;

}

//创建本地流媒体链接

框架相关

原生JS虽能实现绝大部分功能,但要么就是过于繁琐,要么就是存在缺陷,故绝大多数开发者都会首选框架开发方案。现阶段较热门是React、Vue两大框架,两者工作原理上存在共通点,也存在一些不同点,对于校招来说,不需要两个框架都学得特别熟,一般面试官会针对你简历中写的框架进行提问。

在框架方面,生命周期、钩子函数、虚拟DOM这些基本知识是必须要掌握的,在学习的过程可以结合框架的官方文档

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

Vue框架

知识要点:
1. vue-cli工程
2. vue核心知识点
3. vue-router
4. vuex
5. http请求
6. UI样式
7. 常用功能
8. MVVM设计模式

React框架

知识要点:
1. 基本知识
2. React 组件
3. React Redux
4. React 路由

(){

if(socket){

socket.emit(‘leave’,roomid);

}

//释放资源

closePeerConnection();

closeLocalMedia();

btnConn.disabled = false;

btnLeave.disabled = true;

}

//创建本地流媒体链接

框架相关

原生JS虽能实现绝大部分功能,但要么就是过于繁琐,要么就是存在缺陷,故绝大多数开发者都会首选框架开发方案。现阶段较热门是React、Vue两大框架,两者工作原理上存在共通点,也存在一些不同点,对于校招来说,不需要两个框架都学得特别熟,一般面试官会针对你简历中写的框架进行提问。

在框架方面,生命周期、钩子函数、虚拟DOM这些基本知识是必须要掌握的,在学习的过程可以结合框架的官方文档

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

Vue框架

知识要点:
1. vue-cli工程
2. vue核心知识点
3. vue-router
4. vuex
5. http请求
6. UI样式
7. 常用功能
8. MVVM设计模式

[外链图片转存中…(img-DL7d0Yz5-1715361947922)]

React框架

知识要点:
1. 基本知识
2. React 组件
3. React Redux
4. React 路由

[外链图片转存中…(img-REdP5N6V-1715361947923)]

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值