【electron】网络net接口请求,返回数据流

1.net请求

const { net } = require('electron');
const data =  '';  // 请求参数 string

// 创建连接
request = net.request({
   url: '', // 请求接口
     method: 'POST',
     headers: {
         "Content-Type": "application/json"
     }
 });
 
// 请求回应
request.on('response', (response) => {
    response.on('data', (chunk) => {
       console.log('chunk', chunk) // 数据流
       event.reply('chat-response');
     });
    response.on('end', () => {
           event.reply('chat-end');
       });
});

// 请求中断
request.on('abort', () => {
        event.reply('chat-end');
    }
});

// 请求报错
request.on('error', (error) => {
    console.error(`error Problem with request: ${error.message}`);
});
   
// 写入数据到请求体中
request.write(data);
request.end(); // 结束请求

2.数据流转字符串

function Utf8ArrayToStr(array) {
   var out, i, len, c;
   var char2, char3;
   
   out = "";
   len = array.length;
   i = 0;
   while(i < len)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值