春節後第一篇文章,肺炎疫情期間,大家都應該安排在家辦公或者休假吧,我也和大家一樣,小區門衛都只讓每兩天才能出去一次,出入都得測溫消毒,實屬不便。閒話就撤這麼多了,這幾天任務不忙的時候,接觸了一下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', ()=> {})
到此大概結構介紹完了, 具體業務具體設計吧~~~~