Delen via


Connection Class

  • java.lang.Object
    • com.microsoft.cognitiveservices.speech.Connection

Implements

java.lang.AutoCloseable

public final class Connection
implements java.lang.AutoCloseable

Connection is a proxy class for managing connection to the speech service of the specified Recognizer. By default, a Recognizer autonomously manages connection to service when needed. The Connection class provides additional methods for users to explicitly open or close a connection and to subscribe to connection status changes. The use of Connection is optional. It is intended for scenarios where fine tuning of application behavior based on connection status is needed. Users can optionally call openConnection() to manually initiate a service connection before starting recognition on the Recognizer associated with this Connection. After starting a recognition, calling openConnection() or closeConnection() might fail. This will not impact the Recognizer or the ongoing recognition. Connection might drop for various reasons, the Recognizer will always try to reinstitute the connection as required to guarantee ongoing operations. In all these cases Connected/Disconnected events will indicate the change of the connection status. Note: close() must be called in order to release underlying resources held by the object. Updated in version 1.17.0.

Field Summary

Modifier and Type Field and Description
final EventHandlerImpl<ConnectionEventArgs> connected

The Connected event to indicate that the recognizer is connected to service.

final EventHandlerImpl<ConnectionEventArgs> disconnected

The Disconnected event to indicate that the recognizer is disconnected from service.

final EventHandlerImpl<ConnectionMessageEventArgs> messageReceived

The MessageReceived event indicates that the service has sent a network message to the client.

Method Summary

Modifier and Type Method and Description
void close()

Dispose of associated resources.

void closeConnection()

Closes the connection the service.

static Connection fromConversationTranslator(ConversationTranslator conversationTranslator)

Gets the Connection instance from the conversation translator.

static Connection fromDialogServiceConnector(DialogServiceConnector dialogServiceConnector)

Gets the Connection instance from the dialog service connector.

static Connection fromRecognizer(Recognizer recognizer)

Gets the Connection instance from the specified recognizer.

static Connection fromSpeechSynthesizer(SpeechSynthesizer speechSynthesizer)

Gets the Connection instance from the speech synthesizer.

void openConnection(boolean forContinuousRecognition)

Starts to set up connection to the service.

java.util.concurrent.Future<java.lang.Void> sendMessageAsync(String path, String payload)

Sends a message to service.

void setMessageProperty(String path, String propertyName, String propertyValue)

Appends a parameter in a message to service.

Methods inherited from java.lang.Object

java.lang.Object.clone java.lang.Object.equals java.lang.Object.finalize java.lang.Object.getClass java.lang.Object.hashCode java.lang.Object.notify java.lang.Object.notifyAll java.lang.Object.toString java.lang.Object.wait java.lang.Object.wait java.lang.Object.wait

Field Details

connected

public final EventHandlerImpl connected

The Connected event to indicate that the recognizer is connected to service. In order to receive the connected event after subscribing to it, the Connection object itself needs to be alive. If the Connection object owning this event is out of its life time, all subscribed events won't be delivered.

disconnected

public final EventHandlerImpl disconnected

The Disconnected event to indicate that the recognizer is disconnected from service. In order to receive the disconnected event after subscribing to it, the Connection object itself needs to be alive. If the Connection object owning this event is out of its life time, all subscribed events won't be delivered.

messageReceived

public final EventHandlerImpl messageReceived

The MessageReceived event indicates that the service has sent a network message to the client. Added in version 1.15.0.

Method Details

close

public void close()

Dispose of associated resources. close() must be called to relinquish underlying resources correctly.

closeConnection

public void closeConnection()

Closes the connection the service. Users can optionally call closeConnection() to manually shutdown the connection of the associated Recognizer. The call might fail, depending on the process state of the Recognizer. But the failure does not affect the state of the associated Recognizer.

fromConversationTranslator

public static Connection fromConversationTranslator(ConversationTranslator conversationTranslator)

Gets the Connection instance from the conversation translator.

Parameters:

conversationTranslator - The conversationTranslator associated with the connection.

Returns:

The Connection instance of the recognizer.

fromDialogServiceConnector

public static Connection fromDialogServiceConnector(DialogServiceConnector dialogServiceConnector)

Gets the Connection instance from the dialog service connector.

Parameters:

dialogServiceConnector - The dialog service connector associated with the connection, used for observing and managing connection state with the speech service.

Returns:

The Connection instance of the dialog service connector.

fromRecognizer

public static Connection fromRecognizer(Recognizer recognizer)

Gets the Connection instance from the specified recognizer.

Parameters:

recognizer - The recognizer associated with the connection.

Returns:

The Connection instance of the recognizer.

fromSpeechSynthesizer

public static Connection fromSpeechSynthesizer(SpeechSynthesizer speechSynthesizer)

Gets the Connection instance from the speech synthesizer. Added in version 1.17.0

Parameters:

speechSynthesizer - The speech synthesizer associated with the connection, used for observing and managing connection state with the speech service.

Returns:

The Connection instance of the speech synthesizer.

openConnection

public void openConnection(boolean forContinuousRecognition)

Starts to set up connection to the service. Users can optionally call openConnection() to manually set up a connection in advance before starting recognition on the Recognizer associated with this Connection. After starting recognition, calling OpenConnection() might fail, depending on the process state of the Recognizer. But the failure does not affect the state of the associated Recognizer. Note: On return, the connection might not be ready yet. Please subscribe to the Connected event to be notified when the connection is established.

Parameters:

forContinuousRecognition - indicates whether the connection is used for continuous recognition or single-shot recognition. It takes no effect if the connection is from SpeechSynthsizer.

sendMessageAsync

public Future sendMessageAsync(String path, String payload)

Sends a message to service. Added in version 1.7.0

Parameters:

path - The message path.
payload - The message payload.

Returns:

a future representing the asynchronous operation that sends the message.

setMessageProperty

public void setMessageProperty(String path, String propertyName, String propertyValue)

Appends a parameter in a message to service. Added in version 1.7.0

Parameters:

path - The message path.
propertyName - The property name that you want to set.
propertyValue - The value of the property that you want to set.

Applies to