当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript electron.net类代码示例

本文整理汇总了TypeScript中electron.net的典型用法代码示例。如果您正苦于以下问题:TypeScript net类的具体用法?TypeScript net怎么用?TypeScript net使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了net类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: resolve

    await new Promise<void>((resolve, reject) => {
      const request = net.request(requestOptions)

      request.on('response', response => {
        if (response.statusCode === 200) {
          resolve()
        } else {
          reject(
            `Got ${response.statusCode} - ${response.statusMessage} from central`
          )
        }
      })

      request.on('error', reject)

      request.end(body)
    })
开发者ID:Aj-ajaam,项目名称:desktop,代码行数:17,代码来源:exception-reporting.ts

示例2: request

export async function request(
  method: HTTPMethod,
  uri: string,
  data: any = {},
  opts: IRequestOpts = {}
): Promise<IResponse> {
  let url = uri;
  if (env.unitTests) {
    throw new Error(`refusing to do API request in unit test`);
  }

  if (method === "get") {
    const query = querystring.stringify(data);
    if (query !== "") {
      url = `${url}?${query}`;
    }
  }

  const req = net.request({
    method,
    url,
    partition: NET_PARTITION_NAME,
  });
  req.setHeader("user-agent", userAgent());

  const p = new ItchPromise<IResponse>((resolve, reject) => {
    req.on("response", (inputRes: any) => {
      const res = inputRes as ActualElectronResponse;
      const response = {
        statusCode: res.statusCode,
        status: res.statusMessage,
        body: null,
        headers: res.headers,
      } as IResponse;

      if (opts.cb) {
        try {
          opts.cb(response);
        } catch (e) {
          reject(e);
          return;
        }
      }

      let text: any = "";

      if (opts.sink) {
        res.pipe(opts.sink());
      } else {
        res.setEncoding("utf8");
        res.on("data", function(chunk) {
          text += chunk;
        });
      }

      const contentTypeHeader = (res.headers["content-type"] || [
        "text/plain",
      ])[0];
      const contentType = /[^;]*/.exec(contentTypeHeader)![0];

      res.on("end", async () => {
        response.size = text.length;

        if (opts.sink) {
          // all good, it's up to caller to wait on promised sink
        } else if (contentType === "application/json") {
          try {
            response.body = JSON.parse(text);
          } catch (e) {
            reject(new RequestParsingFailure(e.message));
            return;
          }
        } else {
          response.body = text;
        }

        resolve(response);
      });
    });

    req.on("error", error => {
      reject(new RequestError(error.message));
    });

    req.on("abort", (error: Error) => {
      reject(new RequestAborted());
    });

    req.on("login", (authInfo, callback) => {
      // cf. https://github.com/electron/electron/blob/master/docs/api/client-request.md
      // "Providing empty credentials will cancel the request and report
      // an authentication error on the response object"
      callback(undefined as any, undefined as any);
    });

    req.on("close", () => {
      // no-op
    });

    if (!opts.sink) {
//.........这里部分代码省略.........
开发者ID:HorrerGames,项目名称:itch,代码行数:101,代码来源:metal-request.ts

示例3: doRequest

 public doRequest(options: any, callback: (response: any) => void): any {
   const request = net.request(options)
   request.on("response", callback)
   this.addProxyLoginHandler(request)
   return request
 }
开发者ID:ledinhphuong,项目名称:electron-builder,代码行数:6,代码来源:electronHttpExecutor.ts


注:本文中的electron.net类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。