deno.com

TCP/TLS connector: Ping

在 Github 上编辑

An example of connecting to a TCP server using TLS on localhost and writing a 'ping' message to the server.

Read a CA Certificate from the file system
const caCert = await Deno.readTextFile("./root.pem");
Establish a connection to our TCP server using TLS that is currently being run on localhost port 443. We use a custom CA root certificate here. If we remove this option, Deno defaults to using Mozilla's root certificates.
const conn = await Deno.connectTls({
  hostname: "127.0.0.1",
  port: 443,
  caCerts: [caCert],
});
Instantiate an instance of text encoder to write to the TCP stream.
const encoder = new TextEncoder();
Encode the 'ping' message and write to the TCP connection for the server to receive.
await conn.write(encoder.encode("ping"));

使用 Deno CLI 在本地运行 此示例

deno run --allow-net --allow-read https://docs.denohub.com/examples/scripts/tls_connector.ts

你找到需要的内容了吗?

隐私政策