问题描述
我正在尝试使用Microsoft.Bot.Connector.DirectLine .NET客户端连接到我的直线通道.我的客户端应用程序将同时打开许多对话(例如1000多个).
I'm trying to use the Microsoft.Bot.Connector.DirectLine .NET client to connect to my Direct Line Channel. My client application will have many conversations open at once (like 1000+).
我想做的是有效地创建一个Direct Line客户端对象,该对象可以接收我所有对话的消息,而每个对话都没有一个客户端.
What I'm trying to do is efficiently create a single Direct Line client object which can receive messages for all my conversations and NOT have a single client per conversation.
问题在于,要创建一个新的对话,我需要创建一个新的客户端,我认为这最终将耗尽许多套接字.有谁知道我是否可以创建一个连接然后听多个对话?
The problem is that to create a new conversation I need to create a new client which I think would eventually exhaust use up a lot of sockets. Does anyone know if I can create a single connection and then listen for multiple conversations?
谢谢
static async Task Main(string[] args)
{
Console.WriteLine("What is your name:");
var UserName = Console.ReadLine();
var tokenClient = new DirectLineClient(
new Uri(endpoint),
new DirectLineClientCredentials(secret));
var conversation = await tokenClient.Tokens.GenerateTokenForNewConversationAsync();
var client = new DirectLineClient(
new Uri(endpoint),
new DirectLineClientCredentials(conversation.Token));
await client.StreamingConversations.ConnectAsync(
conversation.ConversationId,
ReceiveActivities);
var startConversation = await client.StreamingConversations.StartConversationAsync();
var from = new ChannelAccount() { Id = startConversation.ConversationId, Name = UserName };
var message = Console.ReadLine();
while (message != "end")
{
try
{
var response = await client.StreamingConversations.PostActivityAsync(
startConversation.ConversationId,
new Activity()
{
Type = "message",
Text = message,
From = from,
ChannelData = new Common.ChannelData() { FromNumber = "+17081234567"}
});
}
catch (OperationException ex)
{
Console.WriteLine(
$"OperationException when calling PostActivityAsync: ({ex.StatusCode})");
}
message = Console.ReadLine();
}
Console.ReadLine();
}
public static void ReceiveActivities(ActivitySet activitySet)
{
if (activitySet != null)
{
foreach (var a in activitySet.Activities)
{
if (a.Type == ActivityTypes.Message && a.From.Id == "MyBotName")
{
Console.WriteLine($"<Bot>: {a.Text}");
}
}
}
}
推荐答案
对于您的目的,我认为使用Direct Line流扩展会很麻烦.我猜您的自定义SMS频道本身将是一项应用程序服务.由于可以缩放应用程序服务(在您的情况下可能应该缩放),以使多个实例同时运行,因此,假设来自同一对话的两条SMS消息进入了通道的两个实例.除了使频道的每个实例都使用许多Web套接字与许多漫游器对话之外,您的频道的多个实例还可以使用重复的Web套接字与同一个漫游器对话.还有每个机器人本身都需要支持流扩展的问题.
I think using the Direct Line streaming extensions would be problematic for your purposes. I'm guessing your custom SMS channel would itself be an app service. Since an app service can (and probably should, in your case) be scaled so that multiple instances are running simultaneously, suppose two SMS messages from the same conversation go to two instances of your channel. In addition to having each instance of your channel using many web sockets to talk to many bots, multiple instances of your channel may use duplicated web sockets to talk to the same bot. There's also the problem of each bot itself needing to support streaming extensions.
您可以考虑使用传统直线.这将涉及通过轮询Direct Line端点从机器人接收活动.
Rather than using using Direct Line streaming extensions, you might consider using traditional Direct Line. This would involve receiving activities from the bots by polling a Direct Line endpoint.
由于Direct Line本身就是您要在自己的频道之上使用的频道,因此您也可以考虑完全删除Direct Line.这样,您将在用户和漫游器之间没有两个通道.您可以直接向每个漫游器的端点发送HTTP请求,并且漫游器将接收的活动将包含您的频道的服务URL,从而使您的频道可以接收来自漫游器的消息.
Since Direct Line is a channel itself that you'd be using on top of your own channel, you might also consider cutting out Direct Line altogether. That way you wouldn't have two channels between the user and the bot. You could send HTTP requests to each bot's endpoint directly, and the activities the bots would receive would contain the service URL for your channel, allowing your channel to receive messages from the bots.
这篇关于直线客户的多种对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!