Web套接字挂起获取多请求Node.js

时间:2019-02-19 12:38:16

标签: node.js web websocket

我已经创建了后端(使用node.js)和现场服务器(使用node.js)。 具有rtsp,摄像机视频的现场服务保存在现场服务器中。后端使用Web套接字与现场服务器连接,并向现场服务器发送许多请求以获取视频流。

但是,如果我向现场服务器Web套接字发送了许多请求,则它会挂断并且视频流非常慢。

**Back-end code -**

/**
         * @desc - Listen the get video request
         * @param {object} req - hold request object
         * @param {object} res - hold response object
         */
        loadVideo: function (req, res) {
            let { storeId, video, size, camId, dir } = req.params;
            let range = req.headers.range;
            let parts = range ? range.replace(/bytes=/, "").split("-") : [0];
            let start = parseInt(parts[0], 10);
            let end = start + consts.defaultChunkSize < size - 1 ? start + consts.defaultChunkSize : size - 1;
            //"5c3847754e9e0f38f4e6607c"
          //  console.log({ action: 'loadTimeLineVideo', data: { storeId: storeId, dir: dir, camId: camId, start: start, end: end, video: video, size: size } })
            ws.Send({ action: 'loadTimeLineVideo', data: { storeId: storeId, dir: dir, camId: camId, start: start, end: end, video: video, size: size } });
            let videoResponseHandler = (response) => {
                //streamifier.createReadStream(_Response).pipe(res);
                let file = streamifier.createReadStream(response);
                let chunksize = (end - start) + 1
                let head = {
                    'Content-Range': `bytes ${start}-${end}/${size}`,
                    'Accept-Ranges': 'bytes',
                    'Content-Length': chunksize,
                    'Content-Type': 'video/mp4',
                }
                res.writeHead(206, head)
                file.pipe(res);
            };
            eventEmitter.once('videoResponse', videoResponseHandler);
        },

**Onsite-server code -**

 let { start, end, video, size } = data,
          path = `${videoPath}\\${data.camId}\\${data.dir}\\${video}`;
        if (!fs.existsSync(path)) {
          path = `${videoPath}/NoVideoAvailable.mp4`;
        }

        var currentDate = moment();
        console.log("Read Start " + currentDate);
        var fileStream = fs.createReadStream(path, { start, end, highWaterMark: 512 * 1024 });

        fileStream.on('end', function () {
          console.log("Read End Stream: Current Time: " + moment().format("YYYY-MM-DD hh:mm:ss") + " Diff: " + moment().diff(currentDate, 'seconds'))
        })
        fileStream.pipe(request.post(util.getServerUrl + "/api/reponseTimelineVideo")).on('end', function () {
          console.log("Read End: Current Time: " + moment().format("YYYY-MM-DD hh:mm:ss") + " Diff: " + moment().diff(currentDate, 'seconds'))
        });

0 个答案:

没有答案