您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

重新连接到直接连线 API 3.0 中的会话Reconnect to a conversation in Direct Line API 3.0

如果客户端使用 WebSocket 接口来接收消息但丢失了连接,则可能需要重新连接。If a client is using the WebSocket interface to receive messages but loses its connection, it may need to reconnect. 在这种情况下,客户端必须生成一个新的 WebSocket 流 URL,它可以用来重新连接到聊天。In this scenario, the client must generate a new WebSocket stream URL that it can use to reconnect to the conversation.

生成新的 WebSocket 流 URLGenerate a new WebSocket stream URL

若要生成可用于重新连接到现有聊天的新 WebSocket 流 URL,请发出此请求:To generate a new WebSocket stream URL that can be used to reconnect to an existing conversation, issue this request:

GET https://directline.botframework.com/v3/directline/conversations/{conversationId}?watermark={watermark_value}
Authorization: Bearer SECRET_OR_TOKEN

在此请求 URI 中,请将 {conversationId} 替换为聊天 ID,并将 {watermark_value} 替换为水印值(如果 watermark 参数 可用)。In this request URI, replace {conversationId} with the conversation ID and replace {watermark_value} with the watermark value (if the watermark parameter is available). watermark 参数是可选的。The watermark parameter is optional. 如果在请求 URI 中指定了 watermark 参数,则聊天会从水印处重播,从而保证不会丢失任何消息。If the watermark parameter is specified in the request URI, the conversation replays from the watermark, guaranteeing that no messages are lost. 如果请求 URI 中省略了 watermark 参数,则仅重播重新连接请求后收到的消息。If the watermark parameter is omitted from the request URI, only messages received after the reconnection request are replayed.

以下代码片段提供了重新连接请求和响应的示例。The following snippets provide an example of the Reconnect request and response.

请求Request

GET https://directline.botframework.com/v3/directline/conversations/abc123?watermark=0000a-42
Authorization: Bearer RCurR_XV9ZA.cwA.BKA.iaJrC8xpy8qbOF5xnR2vtCX7CZj0LdjAPGfiCpg4Fv0y8qbOF5xPGfiCpg4Fv0y8qqbOF5x8qbOF5xn

响应Response

如果请求成功,则响应将包含聊天 ID、令牌和新的 WebSocket 流 URL。If the request is successful, the response will contain an ID for the conversation, a token, and a new WebSocket stream URL.

HTTP/1.1 200 OK
[other headers]
{
  "conversationId": "abc123",
  "token": "RCurR_XV9ZA.cwA.BKA.iaJrC8xpy8qbOF5xnR2vtCX7CZj0LdjAPGfiCpg4Fv0y8qbOF5xPGfiCpg4Fv0y8qqbOF5x8qbOF5xn",
  "streamUrl": "https://directline.botframework.com/v3/directline/conversations/abc123/stream?watermark=000a-4&t=RCurR_XV9ZA.cwA..."
}

重新连接到聊天Reconnect to the conversation

客户端必须使用新的 WebSocket 流 URL 在 60 秒内重新连接到聊天The client must use the new WebSocket stream URL to reconnect to the conversation within 60 seconds. 如果在此期间无法建立连接,则客户端必须发出另一个重新连接请求以生成新的流 URL。If the connection cannot be established during this time, the client must issue another Reconnect request to generate a new stream URL.

如果 Direct Line 设置中启用了“增强身份验证选项”,则在未将正确配置的令牌附加到请求的情况下,可能会收到 400“MissingProperty”错误。If you have "Enhanced authentication option" enabled in the Direct Line settings, you might get a 400 "MissingProperty" error if you don't have a correctly configured token attached to the request.

其他资源Additional resources