在 MS Bot Framework 中使用网络套接字和事件
Using web sockets and events in MS Bot Framework
我正在尝试使用我的机器人中的 Web 套接字与服务器上的 echo 服务进行通信。我正在使用 WebSocketSharp 程序集来创建网络套接字连接。我想回显任何用户在机器人中键入的内容,但是它从不触发 "ws.OnMessage" 事件,而且我没有收到任何回复。我在控制台应用程序上测试了连接,一切正常。请建议我在这里做错了什么。
以下是我的 MessageController
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new HumanCollaboratorDialog());
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
以下是我的 HumanCollaboratorDialog class
[Serializable]
public class HumanCollaboratorDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
context.Wait(this.MessageReceivedAsync);
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
using (var ws = new WebSocket("ws://Some IP addrress:8080/human-collaborator/echo"))
{
ws.OnMessage += async (sender, e) =>
{
try
{
await context.PostAsync(e.Data);
}
catch (Exception ex)
{
await context.PostAsync($"Exception: {ex.Message}");
}
};
ws.ConnectAsync();
var msg = message.Text;
ws.Send(msg);
}
context.Wait(this.MessageReceivedAsync);
}
}
"MessageReceivedAsync" 不是创建 websocket 的正确位置。 bot 框架中的 WebSockets 用于在 Direct Line 连接场景中接收消息。通过调用 StartConversationAsync
获得的 StreamUrl
用于创建网络套接字:
var token = await new DirectLineClient(dlSecret).Tokens.GenerateTokenForNewConversationAsync();
// Use token to create conversation
var directLineClient = new DirectLineClient(tokenResponse.Token);
var conversation = await directLineClient.Conversations.StartConversationAsync();
using (var webSocketClient = new WebSocket(conversation.StreamUrl))
{
webSocketClient.OnMessage += WebSocketClient_OnMessage;
webSocketClient.Connect();
等等
我正在尝试使用我的机器人中的 Web 套接字与服务器上的 echo 服务进行通信。我正在使用 WebSocketSharp 程序集来创建网络套接字连接。我想回显任何用户在机器人中键入的内容,但是它从不触发 "ws.OnMessage" 事件,而且我没有收到任何回复。我在控制台应用程序上测试了连接,一切正常。请建议我在这里做错了什么。
以下是我的 MessageController
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new HumanCollaboratorDialog());
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
以下是我的 HumanCollaboratorDialog class
[Serializable]
public class HumanCollaboratorDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
context.Wait(this.MessageReceivedAsync);
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
using (var ws = new WebSocket("ws://Some IP addrress:8080/human-collaborator/echo"))
{
ws.OnMessage += async (sender, e) =>
{
try
{
await context.PostAsync(e.Data);
}
catch (Exception ex)
{
await context.PostAsync($"Exception: {ex.Message}");
}
};
ws.ConnectAsync();
var msg = message.Text;
ws.Send(msg);
}
context.Wait(this.MessageReceivedAsync);
}
}
"MessageReceivedAsync" 不是创建 websocket 的正确位置。 bot 框架中的 WebSockets 用于在 Direct Line 连接场景中接收消息。通过调用 StartConversationAsync
获得的 StreamUrl
用于创建网络套接字:
var token = await new DirectLineClient(dlSecret).Tokens.GenerateTokenForNewConversationAsync();
// Use token to create conversation
var directLineClient = new DirectLineClient(tokenResponse.Token);
var conversation = await directLineClient.Conversations.StartConversationAsync();
using (var webSocketClient = new WebSocket(conversation.StreamUrl))
{
webSocketClient.OnMessage += WebSocketClient_OnMessage;
webSocketClient.Connect();
等等