webRTC(十四),前端架构组件Room功能详解

本文介绍了如何使用WebRTC技术结合socket.io实现实时聊天和视频通话,包括创建PeerConnection、处理媒体协商、数据通道的创建和消息传递等关键步骤。
摘要由CSDN通过智能技术生成

}

.send{

margin-top: 20px;

}

Connect Sig Server

Leave

Local:

Remote:

Chat:

Send

  • 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 chat = document.querySelector(‘textarea#chat’);

var send_txt = document.querySelector(‘textarea#sendtext’);

var btnSend = document.querySelector(‘button#send’);

var localStream = null;

var roomid = ‘555555’;

var socket =null;

var state = ‘init’;

var pc = null;

var dc = 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 reveivemsg(e){

var msg = e.data;

console.log(‘recreived msg is :’+e.data);

if(msg){

chat.value += ‘->’ + msg + ‘\r\n’;

}else{

console.error(‘recreived msg is null’);

}

}

function dataChannelStateChange(){

var readyState=dc.readyState;

if(readyState===‘open’){

send_txt.disabled =false;

btnSend.disabled=false;

}else{

send_txt.disabled = true;

btnSend.disabled =true;

}

}

function dataChannelError(error){

console.log(“Data Channel Error:”, error);

}

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();

}

var dataChannelOptions = {

ordered: true, //保证到达顺序

};

//文本聊天

dc=pc.createDataChannel(‘dataChannel’,dataChannelOptions);

dc.onmessage=reveivemsg;

dc.onopen = dataChannelStateChange;

dc.onclose = dataChannelStateChange;

dc.onerror = dataChannelError;

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)=>{

console.log(" message=====>",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;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

img
img

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V:vip1024c 备注前端获取(资料价值较高,非无偿)
img

最后

四轮技术面+一轮hr面结束,学习到了不少,面试也是一个学习检测自己的过程,面试前大概复习了 一周的时间,把以前的代码看了一下,字节跳动比较注重算法,面试前刷了下leetcode和剑指offer, 也刷了些在牛客网上的面经。大概就说这些了,写代码去了~

祝大家都能收获大厂offer~

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

篇幅有限,仅展示部分内容


:vip1024c 备注前端获取(资料价值较高,非无偿)**
[外链图片转存中…(img-oy65hL3b-1711589681391)]

最后

四轮技术面+一轮hr面结束,学习到了不少,面试也是一个学习检测自己的过程,面试前大概复习了 一周的时间,把以前的代码看了一下,字节跳动比较注重算法,面试前刷了下leetcode和剑指offer, 也刷了些在牛客网上的面经。大概就说这些了,写代码去了~

祝大家都能收获大厂offer~

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

篇幅有限,仅展示部分内容

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值