0

我正在尝试使用我的机器人中的 Web 套接字与服务器上的 echo 服务进行通信。我正在使用WebSocketSharp程序集来创建 Web 套接字连接。我想回显机器人中的任何用户类型,但它从不触发“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 类

[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);
    }
}
4

1 回答 1

0

“MessageReceivedAsync”不是创建 websocket 的正确位置。bot 框架中的 WebSockets 用于在 Direct Line 连接场景中接收消息。StreamUrl从调用中获得的AStartConversationAsync用于创建 Web 套接字:

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();

等等

请在此处查看:https ://github.com/Microsoft/BotBuilder-Samples/blob/master/CSharp/core-DirectLineWebSockets/DirectLineClient/Program.cs

于 2017-04-19T17:13:24.733 回答