Class: WebSocketClient

Inherits:
Object
  • Object
show all
Defined in:
lib/ibm_watson/websocket/speech_to_text_websocket_listener.rb

Overview

Class for interacting with the WebSocket API

Instance Method Summary collapse

Constructor Details

#initialize(audio: nil, chunk_data:, options:, recognize_callback:, service_url:, headers:, disable_ssl_verification: false) ⇒ WebSocketClient

Returns a new instance of WebSocketClient.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ibm_watson/websocket/speech_to_text_websocket_listener.rb', line 14

def initialize(audio: nil, chunk_data:, options:, recognize_callback:, service_url:, headers:, disable_ssl_verification: false)
  @audio = audio
  @options = options
  @callback = recognize_callback
  @bytes_sent = 0
  @headers = headers
  @is_listening = false
  @service_url = service_url
  @timer = nil
  @chunk_data = chunk_data
  @mic_running = false
  @data_size = audio.nil? ? 0 : @audio.size
  @queue = Queue.new
  @disable_ssl_verification = disable_ssl_verification
end

Instance Method Details

#add_audio_chunk(chunk:) ⇒ Object



98
99
100
101
# File 'lib/ibm_watson/websocket/speech_to_text_websocket_listener.rb', line 98

def add_audio_chunk(chunk:)
  @data_size += chunk.size
  @queue << chunk
end

#startObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ibm_watson/websocket/speech_to_text_websocket_listener.rb', line 30

def start
  on_open = lambda do |event|
    on_connect(event)
    @client.send(build_start_message(options: @options))
    @mic_running = true if @chunk_data
    send_audio(data: @audio)
  end

  on_message = lambda do |event|
    json_object = JSON.parse(event.data)
    if json_object.key?("error")
      error = json_object["error"]
      if error.start_with?(TIMEOUT_PREFIX)
        @callback.on_inactivity_timeout(error: error)
      else
        @callback.on_error(error: error)
      end
    elsif json_object.key?("state")
      if !@is_listening
        @is_listening = true
      else
        @client.send(build_close_message)
        @callback.on_transcription_complete
        @client.close(CLOSE_SIGNAL)
      end
    elsif json_object.key?("results") || json_object.key?("speaker_labels")
      hypothesis = ""
      unless json_object["results"].nil? && json_object["speaker_labels"].nil?
        hypothesis = json_object.dig("results", 0, "alternatives", 0, "transcript")
        b_final = json_object.dig("results", 0, "final")
        transcripts = extract_transcripts(alternatives: json_object.dig("results", 0, "alternatives"))

        @callback.on_hypothesis(hypothesis: hypothesis) if b_final

        @callback.on_transcription(transcript: transcripts)
        @callback.on_data(data: json_object)
      end
    end
  end

  on_close = lambda do |_event|
    @client = nil
    EM.stop_event_loop
  end

  on_error = lambda do |event|
    @callback.on_error(error: event)
  end

  EM&.reactor_thread&.join
  EM.run do
    if @disable_ssl_verification
      @service_url = @service_url.sub("wss:", "ws:")
      @client = Faye::WebSocket::Client.new(@service_url, nil, tls: { verify_peer: false, fail_if_no_peer_cert: false }, headers: @headers)
    else
      @client = Faye::WebSocket::Client.new(@service_url, nil, headers: @headers)
    end
    @client.onclose = on_close
    @client.onerror = on_error
    @client.onmessage = on_message
    @client.onopen = on_open
    @client.add_listener(Faye::WebSocket::API::Event.create("open"))
    @client.add_listener(Faye::WebSocket::API::Event.create("message"))
    @client.add_listener(Faye::WebSocket::API::Event.create("close"))
    @client.add_listener(Faye::WebSocket::API::Event.create("error"))
  end
end

#stop_audioObject



103
104
105
# File 'lib/ibm_watson/websocket/speech_to_text_websocket_listener.rb', line 103

def stop_audio
  @mic_running = false
end