信令服务器是使用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())