WebRTC 从零学习笔记⑤-信令服务器map思想

信令服务器是使用map管理房间,MAP负责以下过程中GSM各功能实体间的信息传递:

  • 位置登记、删除
  • 位置寄存器故障后的复原
  • 用户管理
  • 鉴权加密
  • IMEI的管理
  • 路由功能
  • 接入处理及寻呼
  • 补充业务的处理
  • 切换
  • 短消息业务
  • 操作和维护

 按照有关的协议规范,操作可分为四类:

         1类操作:操作成功与否都需要返回确认,成功返回结果,失败返回错误;

         2类操作:只有操作失败时才需要返回确认;

         3类操作:只有操作成功时才需要返回确认;

         4类操作:操作不需要返回确认。

map实战操作主要涉及put/get/removelsize等操作。

map实现:

/*** -------  ZeroRTCMap  ------ ***/
const ZeroRTCMap = function(){//map类
    this._entrys = new Array();
    // 插入
    this.put=function(key,value){
        if(key==null || key==undefined){
            return
        }
        let index = this._getIndex(key)
        if(index == -1){
            let entry = new Object()
            entry.key=key
            entry.value=value
            this._entrys[this._entrys.length] = entry
        } else{
            this._entrys[index].value = value
        }
    };

    // 根据key获取value
    this.get=function(key){
        const index = this._getIndex(key)
        return (index != 1)?this._entrys[index]:null
    }

    // 移除key-value
    this.remove=function(key){
        const index=this._getIndex(key)
        if(index!=-1){
            this._entrys.splice(index,1)
        }
    }

    // 清空map
    this.clear=function(){
        this._entrys.length=0
    }

    // 判断是否包含key
    this.contains=function(key){
        const index=this._getIndex(key)
        return (index != -1)?true:false
    }

    // map内key-value的数量
    this.size=function(){
        return this._entrys.length
    }

    // 获取所有key
    this.getEntrys = function (){
        return this._entrys
    }

    // 内部函数
    this._getIndex=function(key){
        if(key==null || key==undefined){
            return -1
        }
        let _length=this._entrys.length
        for(let i=0;i<_length;i++){
            const entry=this._entrys[i]
            if(entry==null||entry==undefined){
                continue
            }
            if(entry.key==key){
                return i
            }
        }
        return -1
    }
}

function Client(uid,conn,roomId){
    this.uid=uid    // 用户所属的id
    this.conn=conn  // uid对应的websocket连接 
    this.roomId=roomId
    console.log('uid:'+uid+',conn:'+conn+',roomId:'+roomId)
}

// 创建客户端加入map
const roomMap=new ZeroRTCMap()//房间管理

测试代码:

// Math.random()返回介于0(包含)~1(不包含)之间的一个随机数:
// toString(36)代表36进制,其他一些也可以,比如toString(2)、toString(8),代表输出为二进制和八进制。最高支持36进制(0~9,a~z)
// 36进制 = 10进制数字+26个字母(小写)
// substr(2)舍去0/1位置的字符
console.log('\n\n--------------------Math.random------------------')
let randmo=Math.random();
console.log('Math.random()='+randmo)//默认十进制的0——1的小数
console.log('Math.random().toString(10)=' + randmo.toString(10))//10进制
console.log('Math.random().toString(36)=' + randmo.toString(36))//36进制
console.log('Math.random().toString(36).substring(0)=' + randmo.toString(36).substring(0))//从第零位开始      0.1231231567
console.log('Math.random().toString(36).substring(1)=' + randmo.toString(36).substring(1))//从第一位开始      .1231231567
console.log('Math.random().toString(36).substring(2)=' + randmo.toString(36).substring(2))//从第二位开始      1231231567

console.log('\n\n----------------create client-----------------')
let roomId=100
let uid1=Math.random().toString(36).substring(2)//uid由随机数产生
let conn1=100
let client1=new Client(uid1,conn1,roomId);

let uid2=Math.random().toString(36).substring(2)
let conn2 = 101
let client2=new Client(uid2,conn2,roomId)

// 插入put
console.log('\n\n-----------------------put------------------------')
console.log('roomMap pu client1')
roomMap.put(uid1,client1)//key:客户端uid

console.log('roomMap pu client1')
roomMap.put(uid2,client2)
console.log('roomMap size:'+roomMap.size())

// 获取get
console.log('\n\n--------------------------get----------------------')
let client = null
let uid=uid1
client = roomMap.get(uid)
if(client!=null){
    console.log('get client=>'+'uid:'+client.uid+'.conn:'+client.conn+',rommId'+client.roomId);
}else{
    console.log("can't find the client of " + uid);
}

uid = '123345';
client = roomMap.get(uid)
if(client!=null){
    console.log('get client=>'+'uid:'+client.uid+'.conn:'+client.conn+',rommId'+client.roomId);
}else{
    console.log("can't find the client of " + uid);
}

console.log('\n\n----------------traverse遍历map---------------')
// 便利map
const clients = roomMap.getEntrys();
for(let i in clients){
    let uid=clients[i].key
    let client=roomMap.get(uid)
    console.log('get client->'+'uid:'+client?.uid+',conn:'+client?.conn+',roomId:'+client?.roomId)
}

console.log('\n\n-----------------------remove-------------------')
console.log('roomMap remove uid1')
roomMap.remove(uid)
console.log('roomMap size:'+roomMap.size())

console.log('\n\n----------------------clear---------------------')
console.log('roomMap clear all')
roomMap.clear()
console.log('roomMap size:'+roomMap.size())

效果:

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WebRTC信令服务器是实现WebRTC通信的关键组件,它负责处理WebRTC会话的建立、维护和终止。WebRTC信令服务器起着桥梁的作用,连接了不同的对等端,并协调它们之间的通信。在WebRTC中,信令服务器用于传递信令消息,包括会话描述协议(Session Description Protocol,SDP)和候选者(candidates)信息,以便对等端能够互相发现、建立和交换媒体流。 关于WebRTC信令服务器的选择,有几种常见的方案。一种是使用专门设计的信令服务器,如开源的Janus、Kurento和Jitsi等。这些服务器提供了丰富的API和功能,可以满足各种复杂的通信需求。 另一种选择是利用现有的Web服务器来实现信令功能。常见的选择包括Apache、Nginx和NodeJS等。这些服务器在处理HTTP/HTTPS、WS/WSS等基于TCP的传输协议方面有天然的优势。对于实时通信的信令服务器,负载一般不会很高,一台服务器就可以满足需求。使用Nginx和NodeJS来实现信令服务器相对容易,并且具有较高的稳定性。 在WebRTC中,STUN服务器和TURN服务器也是必不可少的组件。STUN服务器用于获取对等端的公网IP地址和端口信息,而TURN服务器则用于中继媒体流,以便在对等端之间进行通信。在创建RTCPeerConnection对象时,需要提供STUN/TURN服务器的相关信息。 总而言之,WebRTC信令服务器是实现WebRTC通信的关键组件,它起着连接对等端并协调通信的作用。选择合适的信令服务器取决于具体的需求和技术要求,可以使用专门设计的信令服务器,如Janus、Kurento和Jitsi等,也可以利用现有的Web服务器来实现信令功能,如Apache、Nginx和NodeJS等。同时,STUN服务器和TURN服务器也是不可或缺的组件,用于获取对等端的网络信息和中继媒体流。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值