1

我启动了简单的 kafka 服务器。请参阅快速入门部分

我无法连接到上述服务器。这是我的代码:

Deno.listen({ port: 9092, transport: 'tcp' });
// or
Deno.listen({ port: 9092, hostname: 'localhost', transport: 'tcp' });
//or
Deno.listen({ port: 9092, hostname: '127.0.0.1' });

这是错误:

error: Uncaught PermissionDenied: An attempt was made to access a socket in a way forbidden by its access permissions. (os error 10013)
    at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)
    at Object.sendSync ($deno$/ops/dispatch_json.ts:72:10)
    at Object.listen ($deno$/ops/net.ts:51:10)
    at Object.listen ($deno$/net.ts:152:22)
    at Object.listen ($deno$/net_unstable.ts:36:12)
    at file:///D:/rust/deno/index.ts:10:6

我试过的命令

deno run --allow-net --allow-write ./index.ts
deno run --allow-net --unstable ./index.ts
deno run --allow-net  ./index.ts

我可以在 Rust 帮助下毫无问题地连接到上面的服务器。这是我的锈代码:

fn connect() -> Result<(), std::io::Error> {
    let stream = TcpStream::connect("localhost:9092").expect("Could not connect");
}

我是 Deno 的新手。我做错了什么?

4

1 回答 1

2

随着Deno.listen您在 port 上创建服务器9002,而不是连接到 Kafka 服务器。你应该Deno.connect改用。

const conn = await Deno.connect({ hostname: "localhost", port: 9092 });
console.log("Connected", conn);

const decoder = new TextDecoder();
const encoder = new TextEncoder();
await conn.write(encoder.encode("message"));

// Read response
const buf = new Uint8Array(1024);
await conn.read(buf);
console.log('Client - Response:', decoder.decode(buf))


conn.close();
于 2020-05-22T15:28:27.833 回答