HTML EventSource 未在电子应用程序中关闭

HTML EventSource not closing in electron app

以下代码在浏览器中运行良好。但是在电子中使用时,事件源没有关闭。

 var source = new EventSource("http://localhost/hyperion_datasource/events/powerup_events.php");
    var data=[];
    source.onmessage = function (event) { 
         data = event.data.split("$");
         console.log(data[0]);    
        if (data[0] > 99) {  
            console.log('closing stream'); 
            source.close();
        }

    };

调用close()后,也将值设置为null。然后可以销毁它并进行垃圾收集。

 var source = new EventSource("http://localhost/hyperion_datasource/events/powerup_events.php");
    var data=[];
    source.onmessage = function (event) { 
         data = event.data.split("$");
         console.log(data[0]);    
        if (data[0] > 99) {  
            console.log('closing stream'); 
            source.close();
            source = null;
        }

    };

我怀疑您应该这样做,以便它也能在所有浏览器中可靠地工作。行为的差异也可能是由于 运行 它在 Electron 前端时的不同范围。