你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Connection class
Connection 是一个代理类,用于管理与指定识别器语音服务的连接。 默认情况下,识别器会在需要时自主管理与服务的连接。 Connection 类为用户提供其他方法,用于显式打开或关闭连接以及订阅连接状态更改。 Connection 的使用是可选的,主要用于需要根据连接状态微调应用程序行为的情况。 用户可以选择调用 Open () 以在与此连接关联的识别器上开始识别之前提前手动设置连接。 如果识别器需要连接到服务或断开连接,它将独立设置或关闭连接。 在这种情况下,将通过连接/断开连接事件更改连接状态来通知连接。 在版本 1.2.1 中添加。
属性
connected | 用于指示识别器已连接到服务的 Connected 事件。 |
disconnected | 用于指示识别器已与服务断开连接的 Disconnected 事件。 |
message |
从语音服务接收的任何消息。 |
message |
发送到语音服务的任何消息。 |
received |
未由任何其他顶级识别器处理的任何来自服务的消息。 将在 2.0 中删除。 |
方法
close() | 释放关联的资源。 |
close |
关闭服务的连接。 用户可以选择调用 closeConnection () 来手动关闭关联识别器的连接。 如果在识别过程中调用 closeConnection () ,则识别将失败并取消并显示错误。 |
from |
从指定的识别器获取 Connection 实例。 |
from |
从指定的合成器获取 Connection 实例。 |
open |
开始设置与服务的连接。 用户可以选择调用 openConnection () 来手动提前设置连接,然后再开始识别与此连接关联的识别器。 开始识别后,调用 Open () 将不起作用 注意:返回时,连接可能尚未准备就绪。 请订阅 Connected 事件,以在建立连接时收到通知。 |
send |
向语音服务发送消息。 在版本 1.13.0 中添加。 |
set |
将消息中的参数追加到服务。 在版本 1.12.1 中添加。 |
属性详细信息
connected
用于指示识别器已连接到服务的 Connected 事件。
connected: (args: ConnectionEventArgs) => void
属性值
(args: ConnectionEventArgs) => void
disconnected
用于指示识别器已与服务断开连接的 Disconnected 事件。
disconnected: (args: ConnectionEventArgs) => void
属性值
(args: ConnectionEventArgs) => void
messageReceived
从语音服务接收的任何消息。
messageReceived: (args: ConnectionMessageEventArgs) => void
属性值
(args: ConnectionMessageEventArgs) => void
messageSent
发送到语音服务的任何消息。
messageSent: (args: ConnectionMessageEventArgs) => void
属性值
(args: ConnectionMessageEventArgs) => void
receivedServiceMessage
未由任何其他顶级识别器处理的任何来自服务的消息。
将在 2.0 中删除。
receivedServiceMessage: (args: ServiceEventArgs) => void
属性值
(args: ServiceEventArgs) => void
方法详细信息
close()
释放关联的资源。
function close()
closeConnection(() => void, (error: string) => void)
关闭服务的连接。 用户可以选择调用 closeConnection () 来手动关闭关联识别器的连接。
如果在识别过程中调用 closeConnection () ,则识别将失败并取消并显示错误。
function closeConnection(cb?: () => void, err?: (error: string) => void)
参数
- cb
-
() => void
- err
-
(error: string) => void
fromRecognizer(Recognizer | ConversationTranscriber)
从指定的识别器获取 Connection 实例。
static function fromRecognizer(recognizer: Recognizer | ConversationTranscriber): Connection
参数
- recognizer
与连接关联的识别器。
返回
识别器的连接实例。
fromSynthesizer(SpeechSynthesizer)
从指定的合成器获取 Connection 实例。
static function fromSynthesizer(synthesizer: SpeechSynthesizer): Connection
参数
- synthesizer
- SpeechSynthesizer
与连接关联的合成器。
返回
合成器的连接实例。
openConnection(() => void, (error: string) => void)
开始设置与服务的连接。 用户可以选择调用 openConnection () 来手动提前设置连接,然后再开始识别与此连接关联的识别器。 开始识别后,调用 Open () 将不起作用
注意:返回时,连接可能尚未准备就绪。 请订阅 Connected 事件,以在建立连接时收到通知。
function openConnection(cb?: () => void, err?: (error: string) => void)
参数
- cb
-
() => void
- err
-
(error: string) => void
sendMessageAsync(string, string | ArrayBuffer, () => void, (error: string) => void)
向语音服务发送消息。 在版本 1.13.0 中添加。
function sendMessageAsync(path: string, payload: string | ArrayBuffer, success?: () => void, error?: (error: string) => void)
参数
- path
-
string
消息的 WebSocket 路径
- payload
-
string | ArrayBuffer
消息的负载。 这是 json 字符串或 ArrayBuffer。
- success
-
() => void
用于指示成功的回调。
- error
-
(error: string) => void
用于指示错误的回调。
setMessageProperty(string, string, string | object)
将消息中的参数追加到服务。 在版本 1.12.1 中添加。
function setMessageProperty(path: string, propertyName: string, propertyValue: string | object)
参数
- path
-
string
网络消息的路径。
- propertyName
-
string
属性的名称
- propertyValue
-
string | object
属性的名称。 这是一个 json 字符串。