你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Connection class

连接是一个代理类,用于管理与指定识别器语音服务的连接。 默认情况下,识别器会根据需要自主管理与服务的连接。 Connection 类为用户提供其他方法来显式打开或关闭连接并订阅连接状态更改。 使用连接是可选的,主要适用于需要根据连接状态微调应用程序行为的情况。 用户可以选择先调用 Open(),在开始识别与此连接关联的识别器上开始识别之前,提前手动设置连接。 如果识别器需要连接或断开连接到服务,它将独立设置或关闭连接。 在这种情况下,连接将通过连接/已断开连接事件更改连接状态来通知连接。 在版本 1.2.1 中添加。

属性

connected

“已连接”事件,指示识别器已连接到服务。

disconnected

“已断开连接”事件,指示识别器已与服务断开连接。

messageReceived

从语音服务接收的任何消息。

messageSent

发送到语音服务的任何消息。

receivedServiceMessage

任何其他顶级识别器未处理的服务中的任何消息。

将在 2.0 中删除。

方法

close()

释放关联的资源。

closeConnection(() => void, (error: string) => void)

关闭服务的连接。 用户可以选择调用 closeConnection()以手动关闭关联的识别器的连接。

如果在识别过程中调用 closeConnection(),则识别将失败并取消并显示错误。

fromRecognizer(Recognizer | ConversationTranscriber)

从指定的识别器获取 Connection 实例。

fromSynthesizer(SpeechSynthesizer)

从指定的合成器获取 Connection 实例。

openConnection(() => void, (error: string) => void)

开始设置与服务的连接。 用户可以选择调用 openConnection(),在开始识别与此连接关联的识别器之前,提前手动设置连接。 开始识别后,调用 Open() 将不起作用

注意:返回时,连接可能尚未准备就绪。 请订阅 Connected 事件,以在建立连接时收到通知。

sendMessageAsync(string, string | ArrayBuffer, () => void, (error: string) => void)

将消息发送到语音服务。 在版本 1.13.0 中添加。

setMessageProperty(string, string, string | object)

将消息中的参数追加到服务。 在版本 1.12.1 中添加。

属性详细信息

connected

“已连接”事件,指示识别器已连接到服务。

connected: (args: ConnectionEventArgs) => void

属性值

(args: ConnectionEventArgs) => void

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

Recognizer | ConversationTranscriber

与连接关联的识别器。

返回

识别器的连接实例。

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 字符串。