簡體   English   中英

客戶端重新加載/離開頁面后Socket.io服務器崩潰

[英]Socket.io server crash after client reload/leave page

我有一個奇怪的情況。 我正在編寫一個用於從IP攝像機流式傳輸rtsp的Web應用程序。 一切正常,除非監視流的客戶端重新加載或離開頁面時服務器崩潰(並非總是如此)。

這是崩潰的輸出:

zlib.js:499
      var newReq = self._handle.write(flushFlag,
                                ^

TypeError: Cannot read property 'write' of null
    at Zlib.callback (zlib.js:499:33)  

服務器端:

const app = require('express')();
const child_process = require('child_process');
const server = require('http').Server(app);
const io = require('socket.io')(server);
io.of('/stream').on('connection', function(socket) {
    let room;
    socket.on('startStream', function(camuid) {
        room = camuid;
        let stream;
        socket.join(room);

        if (processes.indexOf(camuid) == -1) {
            processes.push(camuid);
            logic.Onvif.getStreamLink(camuid).then(rtsp_link => {
                stream = child_process.spawn("ffmpeg", [
                    '-y',
                    '-re',
                    "-i", rtsp_link,
                    '-f', 'mjpeg',
                    '-'
                ], {
                    detached: false
                });

                stream.stdout.on('data', (data) => {
                    const frame = Object.values(new Uint8Array(data));
                    socket.nsp.to(room).emit('camfeed', frame);
                });

            }).catch((e) => {
                socket.nsp.to(room).emit('streamError', true);
            });
        }
    });

在客戶端:

const io = require('socket.io-client');
socket = io.connect(`http://${location.hostname}:8088/stream`);
socket.on('connect', function() {
   socket.emit('startStream', $this.$props.id);
});
socket.on('camfeed', function (data) {
   //Here the data is displayed on canvas
});

我試圖找出到目前為止服務器中沒有代碼的那部分會導致這種行為。

我嘗試在try{}catchconsole.log每一步中放置函數,偵聽器和發射器,以查看停止位置,但輸出並不總是相同。

因此,我開始尋找解決方案,發現了一個github問題,說zlib負責在發送之前將數據壓縮到gzip,並且錯誤是由於嘗試處理不存在的數據引起的。 另一方面,我沒有安裝zlib作為依賴項,據我所知,因為node具有內置的此功能,因此不再使用zlib軟件包。 我搜索了node_modules並發現minizlib包,但不知道該依賴項是誰。

我相信你需要使用

  socket.on("disconnect",function(){
  //delete all user data here
  })

因此它停止使用您創建的對象

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM