Skip to content

Commit 6f9810d

Browse files
authored
Merge pull request #154160 from trrwilson/user/travisw/add_connection_page
[Cog Svcs] Speech: add a page for using 'Connection'
2 parents 0e56831 + 471d219 commit 6f9810d

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
---
2+
title: Service connectivity how-to - Speech SDK
3+
titleSuffix: Azure Cognitive Services
4+
description: Learn how to monitor for connection status and manually pre-connect or disconnect from the Speech Service.
5+
services: cognitive-services
6+
author: trrwilson
7+
manager: nitinme
8+
ms.service: cognitive-services
9+
ms.subservice: speech-service
10+
ms.topic: conceptual
11+
ms.date: 04/12/2021
12+
ms.author: travisw
13+
zone_pivot_groups: programming-languages-set-thirteen
14+
ms.custom: "devx-track-js, devx-track-csharp"
15+
---
16+
17+
# How to: Monitor and control service connections with the Speech SDK
18+
19+
`SpeechRecognizer` and other objects in the Speech SDK automatically connect to the Speech Service when it's appropriate. Sometimes, you may either want additional control over when connections begin and end or want more information about when the Speech SDK establishes or loses its connection. The supporting `Connection` class provides this capability.
20+
21+
## Retrieve a Connection object
22+
23+
A `Connection` can be obtained from most top-level Speech SDK objects via a static `From...` factory method, e.g. `Connection::FromRecognizer(recognizer)` for `SpeechRecognizer`.
24+
25+
::: zone pivot="programming-language-csharp"
26+
27+
```csharp
28+
var connection = Connection.FromRecognizer(recognizer);
29+
```
30+
31+
::: zone-end
32+
33+
::: zone pivot="programming-language-cpp"
34+
35+
```cpp
36+
auto connection = Connection::FromRecognizer(recognizer);
37+
```
38+
39+
::: zone-end
40+
41+
::: zone pivot="programming-language-java"
42+
43+
```java
44+
Connection connection = Connection.fromRecognizer(recognizer);
45+
```
46+
47+
::: zone-end
48+
49+
## Monitor for connections and disconnections
50+
51+
A `Connection` raises `Connected` and `Disconnected` events when the corresponding status change happens in the Speech SDK's connection to the Speech Service. You can listen to these events to know the latest connection state.
52+
53+
::: zone pivot="programming-language-csharp"
54+
55+
```csharp
56+
connection.Connected += (sender, connectedEventArgs) =>
57+
{
58+
Console.WriteLine($"Successfully connected, sessionId: {connectedEventArgs.SessionId}");
59+
};
60+
connection.Disconnected += (sender, disconnectedEventArgs) =>
61+
{
62+
Console.WriteLine($"Disconnected, sessionId: {disconnectedEventArgs.SessionId}");
63+
};
64+
```
65+
66+
::: zone-end
67+
68+
::: zone pivot="programming-language-cpp"
69+
70+
```cpp
71+
connection->Connected += [&](ConnectionEventArgs& args)
72+
{
73+
std::cout << "Successfully connected, sessionId: " << args.SessionId << std::endl;
74+
};
75+
connection->Disconnected += [&](ConnectionEventArgs& args)
76+
{
77+
std::cout << "Disconnected, sessionId: " << args.SessionId << std::endl;
78+
};
79+
```
80+
81+
::: zone-end
82+
83+
::: zone pivot="programming-language-java"
84+
85+
```java
86+
connection.connected.addEventListener((s, connectionEventArgs) -> {
87+
System.out.println("Successfully connected, sessionId: " + connectionEventArgs.getSessionId());
88+
});
89+
connection.disconnected.addEventListener((s, connectionEventArgs) -> {
90+
System.out.println("Disconnected, sessionId: " + connectionEventArgs.getSessionId());
91+
});
92+
```
93+
94+
::: zone-end
95+
96+
## Connect and disconnect
97+
98+
`Connection` has explicit methods to start or end a connection to the Speech Service. Reasons you may want to use these include:
99+
100+
- "Pre-connecting" to the Speech Service to allow the first interaction to start as quickly as possible
101+
- Establishing connection at a specific time in your application's logic to gracefully and predictably handle initial connection failures
102+
- Disconnecting to clear an idle connection when you don't expect immediate reconnection but also don't want to destroy the object
103+
104+
Some important notes on the behavior when manually modifying connection state:
105+
106+
- Trying to connect when already connected will do nothing. It will not generate an error. Monitor the `Connected` and `Disconnected` events if you want to know the current state of the connection.
107+
- A failure to connect that originates from a problem that has no involvement with the Speech Service -- such as attempting to do so from an invalid state -- will throw or return an error as appropriate to the programming language. Failures that require network resolution -- such as authentication failures -- will not throw or return an error but instead generate a `Canceled` event on the top-level object the `Connection` was created from.
108+
- Manually disconnecting from the Speech Service during an ongoing interaction will result in a connection error and loss of data for that interaction. Connection errors are surfaced on the appropriate top-level object's `Canceled` event.
109+
110+
::: zone pivot="programming-language-csharp"
111+
112+
```csharp
113+
try
114+
{
115+
connection.Open(forContinuousRecognition: false);
116+
}
117+
catch (ApplicationException ex)
118+
{
119+
Console.WriteLine($"Couldn't pre-connect. Details: {ex.Message}");
120+
}
121+
// ... Use the SpeechRecognizer
122+
connection.Close();
123+
```
124+
125+
::: zone-end
126+
127+
::: zone pivot="programming-language-cpp"
128+
129+
```cpp
130+
try
131+
{
132+
connection->Open(false);
133+
}
134+
catch (std::runtime_error&)
135+
{
136+
std::cout << "Unable to pre-connect." << std::endl;
137+
}
138+
// ... Use the SpeechRecognizer
139+
connection->Close();
140+
```
141+
142+
::: zone-end
143+
144+
::: zone pivot="programming-language-java"
145+
146+
```java
147+
try {
148+
connection.openConnection(false);
149+
} catch {
150+
System.out.println("Unable to pre-connect.");
151+
}
152+
// ... Use the SpeechRecognizer
153+
connection.closeConnection();
154+
```
155+
156+
::: zone-end
157+
158+
## Next steps
159+
160+
> [!div class="nextstepaction"]
161+
> [Explore our samples on GitHub](https://aka.ms/csspeech/samples)

articles/cognitive-services/Speech-Service/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ items:
4242
href: migrate-v2-to-v3.md
4343
- name: Use Pronunciation Assessment
4444
href: how-to-pronunciation-assessment.md
45+
- name: Control and monitor connection status
46+
href: how-to-control-connections.md
4547
- name: Customization
4648
items:
4749
- name: What is Custom Speech?

0 commit comments

Comments
 (0)