本文整理汇总了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)
})
示例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) {
//.........这里部分代码省略.........
示例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
}