使用websocket调用我的Azure机器人服务失败

时间:2018-07-03 09:23:14

标签: rest azure websocket botframework

我有一个Azure机器人服务,当在网络聊天中进行测试时,该服务可以完美运行。 现在,我试图打开一个websocket并测试该机器人。

  1. 我首先将POST请求(带有所有必需的标头)发送到https://directline.botframework.com/v3/directline/conversations

  2. 我收到

    的回复

    {

    “ conversationId”:“ 7FY18sO6FCT9OVi0pW7WBY”,

    “令牌”:“ my_token_here”,

    “ expires_in”:1800,

    “ streamUrl”: “ wss://directline.botframework.com/v3/directline/conversations/7FY18sO6FCT9OVi0pW7WBY/stream?watermark =-&t = token_value_here”,

    “ referenceGrammarId”:“ c1c290dd-f896-5857-b328-cdc10298e440”

    }

现在,我尝试使用streamUrl通过Web套接字发送/接收数据。 我收到一个错误:未定义

我试图在其他在线websocket测试工具上测试streamUrl。我仍然收到未定义的错误。

如何测试streamUrl中是否存在问题? 如何测试Web套接字?

1 个答案:

答案 0 :(得分:0)

首先,正如Eric Dahlvang所述,它使我们能够receive activities via WebSocket stream,但不发送活动。

此外,我通过以下步骤和示例进行测试,可以通过WebSocket流按预期接收活动,您可以参考它。

步骤1: start a conversation

发出请求

enter image description here

第二步: 启动客户端应用(控制台应用)以等待接收活动

class Program
{
    private static string botId = "fehanbasicbot";

    static void Main(string[] args)
    {
        var url = Console.ReadLine();
        StartReceivingActivities(url).Wait();
        Console.ReadLine();
    }

    private static async Task StartReceivingActivities(string url)
    {
        var webSocketClient = new WebSocket(url);
        webSocketClient.OnMessage += WebSocketClient_OnMessage;
        webSocketClient.Connect();
    }

    private static void WebSocketClient_OnMessage(object sender, MessageEventArgs e)
    {
        // Occasionally, the Direct Line service sends an empty message as a liveness ping. Ignore these messages.
        if (string.IsNullOrWhiteSpace(e.Data))
        {
            return;
        }

        var activitySet = JsonConvert.DeserializeObject<ActivitySet>(e.Data);
        var activities = from x in activitySet.Activities
                            where x.From.Id == botId
                            select x;

        foreach (Activity activity in activities)
        {
            Console.WriteLine(activity.Text);
        }
    }
}

packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Bot.Connector.DirectLine" version="3.0.2" targetFramework="net461" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.2" targetFramework="net461" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net461" />
  <package id="WebSocketSharp" version="1.0.3-rc11" targetFramework="net461" />
</packages>

第3步: 向机器人发出send an activity的请求

enter image description here

第4步: 检查控制台应用程序的输出,我可以找到收到的活动

enter image description here