gpt4 book ai didi

node.js - 如何知道客户端已连接Eventhub

转载 作者:太空宇宙 更新时间:2023-11-04 01:29:56 26 4
gpt4 key购买 nike

不知道如何判断客户端是否已连接。如果 EVENTHUB_CONNECTION_STRING 完全错误,则会引发异常。但万一 key 错了怎么办?如果不会抛出异常。如何判断客户端连接成功。

我的代码如下。

var EventHubClient = require('azure-event-hubs').EventHubClient; 

const connectionString = "EVENTHUB_CONNECTION_STRING";
const entityPath = "EVENTHUB_NAME";
const connStr = process.env[connectionString] || "";
const path = process.env[entityPath] || "";

var client = EventHubClient.createFromConnectionString(connStr, path);

const eventData = {
body: {
test1: aaa,
test2: bbb
}
};
await client.send(eventData);

最佳答案

createFromConnectionString(connStr, path) 返回一个 Promise,您应该等待它或在它解决连接或错误时注册一个回调。此外,为了发送数据,您还需要一个分区 ID,可以使用 eventHubClient.getPartitionIds()

检索该分区 ID
var EventHubClient = require('azure-event-hubs').EventHubClient; 

const connectionString = "EVENTHUB_CONNECTION_STRING";
const entityPath = "EVENTHUB_NAME";
const connStr = process.env[connectionString] || "";
const path = process.env[entityPath] || "";

var client;
EventHubClient.createFromConnectionString(connStr, path).then((connection) => {
client = connection;
( async function () {
const pids = client.getPartitionIds();
const eventData = {
body: {
test1: aaa,
test2: bbb
}
};
pids.forEach((id) => {
client.send(eventData, id).then((success) => {
console.log(success);
}, (error) => {
console.log(error);

});

});
}())
}, (connectionError) => {
console.log(connectionError);
});

关于node.js - 如何知道客户端已连接Eventhub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56437689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com