electron 发送http请求,参考官网api:
https://www.electronjs.org/zh/docs/latest/api/net#netrequestoptions
https://www.electronjs.org/zh/docs/latest/api/client-request#new-clientrequestoptions
很重要的一点:当接收的数据比较大,数据会多次返回,等数据全部接收完毕时调finish事件,所以判断数据有没有接收完成,需在finish里面处理。
示例代码:
let tempReceiveGetFrame = ""; function getFrame(){ try{ const request = net.request(api+"getFrame"); request.on("response",response=>{ response.on("data",chunk=>{ let result = chunk.toString(); tempReceiveGetFrame += result; //数据大时,这边会多次返回 }); response.on("end",()=>{ }); }); request.on("finish",()=>{ //数据返回结束时调用此方法 try{ //console.log(tempReceiveGetFrame); let data = eval('('+tempReceiveGetFrame+')'); if(data.returnCode == 0){ //alert('data:image/jpg;base64,' + data.data); document.getElementById("video").src ='data:image/jpg;base64,' + data.data.img; } } catch(e){ } tempReceiveGetFrame=""; }) request.end(); } catch(e) { } }
标签:http,数据,request,28,electron,api,tempReceiveGetFrame,data,response From: https://www.cnblogs.com/handsomeziff/p/17049047.html