<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## 創建房間 ### 創建token 服務端從**basicServer.js**開始, 在此以之前的**Basic Example**的客戶端發送的**CreteToken**為例子, 服務端的**http**框架監聽到該請求后開始處理, 對發送過來的消息進行解析之后,調用**getOrCreateRoom**創建**room**和**token**. ``` //licode\extras\basic_example\basicServer.js app.post('/createToken/', (req, res) => { console.log('Creating token. Request body: ', req.body); const username = req.body.username; const role = req.body.role; let room = defaultRoomName; let type; let roomId; let mediaConfiguration; if (req.body.room) room = req.body.room; if (req.body.type) type = req.body.type; if (req.body.roomId) roomId = req.body.roomId; if (req.body.mediaConfiguration) mediaConfiguration = req.body.mediaConfiguration; const createToken = (tokenRoomId) => { N.API.createToken(tokenRoomId, username, role, (token) => { console.log('Token created', token); res.send(token); }, (error) => { console.log('Error creating token', error); res.status(401).send('No Erizo Controller found'); }); }; if (roomId) { createToken(roomId); } else { getOrCreateRoom(room, type, mediaConfiguration, createToken); //調用到此處創建房間,創建token } }); ``` **getOrCreateRoom**中使用了**N.API.getRooms()**去向**nuve**獲取房間列表,校驗當前的房間是否已經創建,如果創建了則使用該**room**去創建**token**, 如果沒有則使用**N.API.createRoom()**創建房間之后再去創建**token**. ``` const getOrCreateRoom = (name, type = 'erizo', mediaConfiguration = 'default', callback = () => {}) => { if (name === defaultRoomName && defaultRoom) { callback(defaultRoom); return; } //向NUVE發請求獲取房間列表,如果該room存在,使用這個room創建token, //不存在,創建room之后再創建token N.API.getRooms((roomlist) => { let theRoom = ''; const rooms = JSON.parse(roomlist); for (let i = 0; i < rooms.length; i += 1) { const room = rooms[i]; if (room.name === name && room.data && room.data.basicExampleRoom) { theRoom = room._id; callback(theRoom);//create token return; } } const extra = { data: { basicExampleRoom: true }, mediaConfiguration }; if (type === 'p2p') extra.p2p = true; N.API.createRoom(name, (roomID) => { theRoom = roomID._id; callback(theRoom);//create token }, () => {}, extra); }); }; ``` 此處的**N.API**是一個用來發送請求到后端**nuve**的工具類,提供了用戶的業務后臺到**nuve**的通訊接口,licode中的設計中,對于一些基礎的操作,createroken, createroom,deleteroom都通過nuve進行處理, 開發者的業務后臺只需要調用并同步信息即可,如以下兩個函數, 主要是用send發送請求到nuve端 ``` N.API = (function (N) { getRooms = function (callback, callbackError, params) { send(callback, callbackError, 'GET', undefined, 'rooms', params); }; createRoom = function (name, callback, callbackError, options, params) { if (!options) { options = {}; } send(function (roomRtn) { var room = JSON.parse(roomRtn); callback(room); }, callbackError, 'POST', {name: name, options: options}, 'rooms', params); }; } ``` nuve的起點在文件**nuve.js**中,其上面也是用express作為監聽的http框架,去處理發送過來的請求,以**createroom**為例,如下所示,一旦有請求,首先觸發對應請求的鑒權,該鑒權就不細看了,其會通過請求頭的service\_id獲取一個service對象, 通過service進行簽名校驗之后鑒權結束,這個service相當于在nuve的一個session, 可通過post,get向nuve請求創建后返回id,后續請求需要帶上該id(注:但該Basic Example中的是預創建的) ``` // licode\nuve\nuveAPI\nuve.js //鑒權 app.post('*', nuveAuthenticator.authenticate); ``` - createroom 鑒權通過后觸發**roomsResource.createRoom**進行處理 ``` // licode\nuve\nuveAPI\nuve.js //監聽創建房間的請求 app.post('/rooms', roomsResource.createRoom); ``` **roomsResource.createRoom**中使用**roomRegistry.addRoom()**往monogo中寫房間,寫庫成功后將roomid加入service.rooms數組中進行管理 ``` ~~~tsx exports.createRoom = (req, res) => { let room; const currentService = req.service; //...省略... req.body.options = req.body.options || {}; if (req.body.options.test) { //...省略... } else { room = { name: req.body.name }; if (req.body.options.p2p) { room.p2p = true; } if (req.body.options.data) { room.data = req.body.options.data; } if (typeof req.body.options.mediaConfiguration === 'string') { room.mediaConfiguration = req.body.options.mediaConfiguration; } //addroom之后進行 roomRegistry.addRoom(room, (result) => { currentService.rooms.push(result); //將房間id(save返回的主鍵)放入數組 serviceRegistry.addRoomToService(currentService, result);//將房間與service關聯起來 log.info(`message: createRoom success, roomName:${req.body.name}, ` + `serviceId: ${currentService.name}, p2p: ${room.p2p}`); res.send(result); }); } }; ``` ##### \- createtoken 創建完房間之后,basicServer發送請求創建token的請求,nuve監聽到之后執行,先通過doInit找到房間,然后去創建token ``` exports.create = (req, res) => { //獲取room后執行創建token的回調 doInit(req, (currentService, currentRoom) => { if (currentService === undefined) { log.warn('message: createToken - service not found'); res.status(404).send('Service not found'); return; } else if (currentRoom === undefined) { log.warn(`message: createToken - room not found, roomId: ${req.params.room}`); res.status(404).send('Room does not exist'); return; } //創建token generateToken(req, (tokenS) => { if (tokenS === undefined) { res.status(401).send('Name and role?'); return; } if (tokenS === 'error') { log.error('message: createToken error, errorMgs: No Erizo Controller available'); res.status(404).send('No Erizo Controller found'); return; } log.info(`message: createToken success, roomId: ${currentRoom._id}, ` + `serviceId: ${currentService._id}`); res.send(tokenS); }); }); }; ``` 在generateToken()中,會將roomid,username等寫入到token中,還會分配ErizoController, 將erizoController的IP和port寫入到token中 ``` const generateToken = (req, callback) => { //....省略..... token = {}; token.userName = user; token.room = currentRoom._id; token.role = role; token.service = currentService._id; token.creationDate = new Date(); token.mediaConfiguration = 'default'; //....省略..... //分配ErizoController, 將erizoController的IP和port寫入到token中 cloudHandler.getErizoControllerForRoom(currentRoom, (ec) => { if (ec === 'timeout' || !ec) { callback('error'); return; } token.secure = ec.ssl; if (ec.hostname !== '') { token.host = ec.hostname; } else { token.host = ec.ip; } token.host += `:${ec.port}`; tokenRegistry.addToken(token, (id, err) => { if (err) { return callback('error'); } const tokenAdded = getTokenString(id, token); return callback(tokenAdded); }); }; ``` 獲取tErizoController的過程如下,首先通過room->erizoControllerId去獲取erizoController, 如果沒有的話,通過策略從隊列中獲取erizoController, 如果沒有策略,則遍歷隊列,根據狀態返回一個可用的erizoController隊列,獲取其首元素進行使用。 ``` const getErizoControllerForRoom = (room, callback) => { const roomId = room._id; //通過room->erizoControllerId去獲取erizoController roomRegistry.getRoom(roomId, (roomResult) => { const id = roomResult.erizoControllerId; if (id) { erizoControllerRegistry.getErizoController(id, (erizoController) => { if (erizoController) { callback(erizoController); } else { roomResult.erizoControllerId = undefined; roomRegistry.updateRoom(roomResult._id, roomResult); getErizoControllerForRoom(roomResult, callback); } }); return; } let attempts = 0; let intervalId; // 如果room->erizoControllerId是空的,從erizoController獲取 getEcQueue((ecQueue) => { intervalId = setInterval(() => { let erizoController; if (getErizoController) { //通過策略獲取 erizoController = getErizoController(room, ecQueue); } else { //從隊列獲取 erizoController = ecQueue[0]; } const erizoControllerId = erizoController ? erizoController._id : undefined; if (erizoControllerId !== undefined) { assignErizoController(erizoControllerId, room, (assignedEc) => { callback(assignedEc); clearInterval(intervalId); }); } if (attempts > TOTAL_ATTEMPTS_EC_READY) { clearInterval(intervalId); callback('timeout'); } attempts += 1; }, INTERVAL_TIME_EC_READY); }); }); }; ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看