Node配合WebSocket做多文件下載以及進度回傳
起因
為什么做這個東西,是突然間聽一后端同事說起Annie這個東西,發(fā)現(xiàn)這個東西下載視頻挺方便的,會自動爬取網(wǎng)頁中的視頻,然后整理成列表。發(fā)現(xiàn)用命令執(zhí)行之后是下面的樣子:
心里琢磨了下,整一個界面玩一下吧。然后就做成下面這個樣子了。
列表
下載列表
本文地址倉庫:https://github.com/Rynxiao/yh-tools,如果喜歡,歡迎star.
涉及技術
- Express 后端服務
- Webpack 模塊化編譯工具
- Nginx 主要做文件gzip壓縮(發(fā)現(xiàn)Express添加gzip有點問題,才棄坑nginx)
- Ant-design 前端UI庫
- React + React Router
- WebSocket 進度回傳服務
其中還有點小插曲,最開始是使用docker起了一個nginx服務,但是發(fā)現(xiàn)內部轉發(fā)一直有問題,同時獲取宿主主機IP也出現(xiàn)了點問題,然后折磨了好久放棄了。(docker研究不深,敬請諒解^_^)
下載部分細節(jié)
首先瀏覽器會連接WebSocket服務器,同時在WebSocket服務器上存在一個所有客戶端的Map,瀏覽器端生成一個uuid作為瀏覽器客戶端id,然后將這個鏈接作為值存進Map中。
客戶端:
// list.jsx await WebSocketClient.connect((event) => { const data = JSON.parse(event.data); if (data.event === 'close') { this.updateCloseStatusOfProgressBar(list, data); } else { this.generateProgressBarList(list, data); } }); // src/utils/websocket.client.js async connect(onmessage, onerror) { const socket = this.getSocket(); return new Promise((resolve) => { // ... }); } getSocket() { if (!this.socket) { this.socket = new WebSocket( `ws://localhost:${CONFIG.PORT}?from=client&id=${clientId}`, 'echo-protocol', ); } return this.socket; }
服務端:
// public/javascript/websocket/websocket.server.js connectToServer(httpServer) { initWsServer(httpServer); wsServer.on('request', (request) => { // uri: ws://localhost:8888?from=client&id=xxxx-xxxx-xxxx-xxxx logger.info('[ws server] request'); const connection = request.accept('echo-protocol', request.origin); const queryStrings = querystring.parse(request.resource.replace(/(^\/|\?)/g, '')); // 每有連接連到websocket服務器,就將當前連接保存到map中 setConnectionToMap(connection, queryStrings); connection.on('message', onMessage); connection.on('close', (reasonCode, description) => { logger.info(`[ws server] connection closed ${reasonCode} ${description}`); }); }); wsServer.on('close', (connection, reason, description) => { logger.info('[ws server] some connection disconnect.'); logger.info(reason, description); }); }
然后在瀏覽器端點擊下載的時候,會傳遞兩個主要的字段resourceId(在代碼中由parentId和childId組成)和客戶端生成的bClientId。這兩個id有什么用呢?
每次點擊下載,都會在Web服務器中生成一個WebSocket的客戶端,那么這個resouceId就是作為在服務器中生成的WebSocket服務器的key值。
bClientId主要是為了區(qū)分瀏覽器的客戶端,因為考慮到同時可能會有多個瀏覽器接入,這樣在WebSocket服務器中產生消息的時候,就可以用這個id來區(qū)分應該發(fā)送給哪個瀏覽器客戶端
客戶端:
// list.jsx http.get( 'download', { code, filename, parent_id: row.id, child_id: childId, download_url: url, client_id: clientId, }, ); // routes/api.js router.get('/download', async (req, res) => { const { code, filename } = req.query; const url = req.query.download_url; const clientId = req.query.client_id; const parentId = req.query.parent_id; const childId = req.query.child_id; const connectionId = `${parentId}-${childId}`; const params = { code, url, filename, parent_id: parentId, child_id: childId, client_id: clientId, }; const flag = await AnnieDownloader.download(connectionId, params); if (flag) { await res.json({ code: 200 }); } else { await res.json({ code: 500, msg: 'download error' }); } }); // public/javascript/annie.js async download(connectionId, params) { //... // 當annie下載時,會進行數(shù)據(jù)監(jiān)聽,這里會用到節(jié)流,防止進度回傳太快,websocket服務器無法反應 downloadProcess.stdout.on('data', throttle((chunk) => { try { if (!chunk) { isDownloading = false; } // 這里主要做的是解析數(shù)據(jù),然后發(fā)送進度和速度等信息給websocket服務器 getDownloadInfo(chunk, ws, params); } catch (e) { downloadSuccess = false; WsClient.close(params.client_id, connectionId, 'download error'); this.stop(connectionId); logger.error(`[server annie download] error: ${e}`); } }, 500, 300)); }
服務端收到進度以及速度的消息后,回傳給客戶端,如果進度達到了100%,那么就刪除掉存在server中的服務器中起的websocket的客戶端,并且發(fā)送一個客戶端被關閉的通知,通知瀏覽器已經(jīng)下載完成。
// public/javascript/websocket/websocket.server.js function onMessage(message) { const data = JSON.parse(message.utf8Data); const id = data.client_id; if (data.event === 'close') { logger.info('[ws server] close event'); closeConnection(id, data); } else { getConnectionAndSendProgressToClient(data, id); } } function getConnectionAndSendProgressToClient(data, clientId) { const browserClient = clientsMap.get(clientId); // logger.info(`[ws server] send ${JSON.stringify(data)} to client ${clientId}`); if (browserClient) { const serverClientId = `${data.parent_id}-${data.child_id}`; const serverClient = clientsMap.get(serverClientId); // 發(fā)送從web服務器中傳過來的進度、速度給瀏覽器 browserClient.send(JSON.stringify(data)); // 如果進度已經(jīng)達到了100% if (data.progress >= 100) { logger.info(`[ws server] file has been download successfully, progress is ${data.progress}`); logger.info(`[ws server] server client ${serverClientId} ready to disconnect`); // 從clientsMap將當前的這個由web服務器創(chuàng)建的websocket客戶端移除 // 然后關閉當前連接 // 同時發(fā)送下載完成的消息給瀏覽器 clientsMap.delete(serverClientId); serverClient.send(JSON.stringify({ connectionId: serverClientId, event: 'complete' })); serverClient.close('download completed'); } } }
整體來說就這么多,有一點需要指出,annie在解析的時候有時候可能消息處理不是很穩(wěn)定,導致我數(shù)據(jù)解析的時候出現(xiàn)了一些問題,但是我用mock的數(shù)據(jù)以及mock的進度條回傳是不會出現(xiàn)問題的。
相關文章
Node.js websocket使用socket.io庫實現(xiàn)實時聊天室
這篇文章主要為大家詳細介紹了Node.js websocket使用socket.io庫實現(xiàn)實時聊天室,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Node.js中Bootstrap-table的兩種分頁的實現(xiàn)方法
這篇文章主要介紹了Node.js中Bootstrap-table的兩種分頁的使用方法,需要的朋友可以參考下2017-09-09深入淺析Node.js 事件循環(huán)、定時器和process.nextTick()
這篇文章主要介紹了Node.js 事件循環(huán)、定時器和process.nextTick()的相關知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10使用PM2實現(xiàn)高效的應用監(jiān)控與管理功能
PM2?是一個流行的進程管理器,用于?Node.js?應用程序,它支持應用程序的負載均衡、自動重啟、日志管理、監(jiān)控以及多環(huán)境管理等功能,本文給大家介紹了如何使用PM2實現(xiàn)高效的應用監(jiān)控與管理功能,需要的朋友可以參考下2024-02-02