 
 import Foundation
 
+/** Abstracts the WebSockets connection to the Watson Speech to Text service. */
 class SpeechToTextWebSocket {
 
     private let socket: WatsonWebSocket


    // MARK: WatsonWebSocket API Functions

    func writeData(data: NSData) {
        socket.writeData(data)
    }

    func writeString(str: String) {
        socket.writeString(str)
    }

    func writePing(data: NSData) {
        socket.writePing(data)
    }

    func disconnect(forceTimeout: NSTimeInterval? = nil) {
        socket.disconnect(forceTimeout)
    }

    // MARK: WatsonWebSocket Delegate Functions

    private func onText(text: String) {
        guard let response = SpeechToTextGenericResponse.parseResponse(text) else {
            if let failure = failure {
                let description = "Could not serialize a generic text response to an object."
                let error = createError(SpeechToTextConstants.domain, description: description)
                failure(error)
            }
            return
        }


    private let failure: (NSError -> Void)?
    private let success: [SpeechToTextResult] -> Void

    /**
     Create a `SpeechToTextWebSocket` object to communicate with Speech to Text.

     - parameter authStrategy: An `AuthenticationStrategy` that defines how to authenticate
        with the Watson Developer Cloud's Speech to Text service. The `AuthenticationStrategy`
        is used internally to obtain tokens, refresh expired tokens, and maintain information
        about authentication state.
     - parameter settings: The configuration for this transcription request.
     - parameter failure: A function executed whenever an error occurs.
     - parameter success: A function executed with all transcription results whenever
        a final or interim transcription is received.

     - returns: A `SpeechToTextWebSocket` object that can communicate with Speech to Text.
     */
    init?(
        authStrategy: AuthenticationStrategy,
        settings: SpeechToTextSettings,
