protoojs的学习整理server,client

春节后第一篇文章,肺炎疫情期间,大家都应该安排在家办公或者休假吧,我也和大家一样,小区门卫都只让每两天才能出去一次,出入都得测温消毒,实属不便。闲话就撤这么多了,这几天任务不忙的时候,接触了一下mediasoup, 今天先说下里面的依赖 protoojs部分

如果说以前接触过socketio或做过websocket相关的项目的话可能会比较好理解,
整体来看 protoo 两部分 客户端 服务端

客户端

可以简单理解成建立websocket连接实例

const protooTransport = new protooClient.WebSocketTransport('wss://xxxxxxxxxxxxx:xxx?xxx');
const user = new protooClient.Peer(protooTransport);
事件
  • open
  • failed
  • disconnected
  • request 这个有应答
  • notification 这个没有应答

简单例子

user.on("request",  (request, accept, reject) => {
        switch(request.method) {
            case 'user':
                this.setState({
                    peers:request.data.peers
                })
                break;
			default:
			    console.log('default')
        }
        accept({ foo: 'bar' });
    }let result = await user.request('method',  jsondata);

user.on('notification', async(notification){
   if(notification.method === 'xxxxx'){
		console.log(notification);
	 }
})
user.notify('method', { foo: 'bar' });

服务端

干净统一

 protooWebSocketServer = new protooServer.WebSocketServer(httpsServer, options);
 protooWebSocketServer.on('connectionrequest', async (info, accept, reject) => {
  const parseUrl = url.parse(info.request.url, true);
  if(success){
   accept(something)
  }else{
   reject(errinfo)
   }
 })

最小单位是人,房间的逻辑需要自己动手实现

rooms = new Map();

//如果没有房间
room = new protooServer.Room();
rooms.set(roomId, room);

//房间里面创建人
const transport = accept();
user = rooms.createPeer(userId,transport)

// rooms.peers:Map   
user.request(method, () => {})
user.notify(method, ()=> {})
user.on('request', () => {})
user.on('notification', ()=> {})

到此大概结构介绍完了, 具体业务具体设计吧~~~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章