Class: OBS::WebSocket::Client

Inherits:
Object
  • Object
show all
Includes:
Protocol::Event::Mixin, Protocol::Request::Mixin
Defined in:
lib/obs/websocket.rb

Overview

OBS-websocket client.

Instance Method Summary collapse

Methods included from Protocol::Request::Mixin

#add_filter_to_source, #add_scene_item, #authenticate, #broadcast_custom_message, #create_scene, #create_source, #delete_scene_item, #disable_studio_mode, #duplicate_scene_item, #enable_studio_mode, #execute_batch, #get_audio_active, #get_audio_monitor_type, #get_auth_required, #get_browser_source_properties, #get_current_profile, #get_current_scene, #get_current_scene_collection, #get_current_transition, #get_filename_formatting, #get_media_duration, #get_media_sources_list, #get_media_state, #get_media_time, #get_mute, #get_output_info, #get_preview_scene, #get_recording_folder, #get_recording_status, #get_replay_buffer_status, #get_scene_item_list, #get_scene_item_properties, #get_scene_list, #get_scene_transition_override, #get_source_active, #get_source_default_settings, #get_source_filter_info, #get_source_filters, #get_source_settings, #get_source_types_list, #get_sources_list, #get_special_sources, #get_stats, #get_stream_settings, #get_streaming_status, #get_studio_mode_status, #get_sync_offset, #get_text_freetype2_properties, #get_text_gdi_plus_properties, #get_tracks, #get_transition_duration, #get_transition_list, #get_transition_position, #get_transition_settings, #get_version, #get_video_info, #get_virtual_cam_status, #get_volume, #list_outputs, #list_profiles, #list_scene_collections, #move_source_filter, #next_media, #open_projector, #pause_recording, #play_pause_media, #previous_media, #refresh_browser_source, #release_t_bar, #remove_filter_from_source, #remove_scene_transition_override, #reorder_scene_items, #reorder_source_filter, #reset_scene_item, #restart_media, #resume_recording, #save_replay_buffer, #save_stream_settings, #scrub_media, #send_captions, #set_audio_monitor_type, #set_browser_source_properties, #set_current_profile, #set_current_scene, #set_current_scene_collection, #set_current_transition, #set_filename_formatting, #set_heartbeat, #set_media_time, #set_mute, #set_preview_scene, #set_recording_folder, #set_scene_item_crop, #set_scene_item_position, #set_scene_item_properties, #set_scene_item_render, #set_scene_item_transform, #set_scene_transition_override, #set_source_filter_settings, #set_source_filter_visibility, #set_source_name, #set_source_settings, #set_stream_settings, #set_sync_offset, #set_t_bar_position, #set_text_freetype2_properties, #set_text_gdi_plus_properties, #set_tracks, #set_transition_duration, #set_transition_settings, #set_volume, #sleep, #start_output, #start_recording, #start_replay_buffer, #start_stop_recording, #start_stop_replay_buffer, #start_stop_streaming, #start_stop_virtual_cam, #start_streaming, #start_virtual_cam, #stop_media, #stop_output, #stop_recording, #stop_replay_buffer, #stop_streaming, #stop_virtual_cam, #take_source_screenshot, #toggle_mute, #toggle_studio_mode, #transition_to_program, #trigger_hotkey_by_name, #trigger_hotkey_by_sequence

Methods included from Protocol::Event::Mixin

#on_broadcast_custom_message, #on_exiting, #on_heartbeat, #on_media_ended, #on_media_next, #on_media_paused, #on_media_playing, #on_media_previous, #on_media_restarted, #on_media_started, #on_media_stopped, #on_preview_scene_changed, #on_profile_changed, #on_profile_list_changed, #on_recording_paused, #on_recording_resumed, #on_recording_started, #on_recording_starting, #on_recording_stopped, #on_recording_stopping, #on_replay_started, #on_replay_starting, #on_replay_stopped, #on_replay_stopping, #on_scene_collection_changed, #on_scene_collection_list_changed, #on_scene_item_added, #on_scene_item_deselected, #on_scene_item_lock_changed, #on_scene_item_removed, #on_scene_item_selected, #on_scene_item_transform_changed, #on_scene_item_visibility_changed, #on_scenes_changed, #on_source_audio_activated, #on_source_audio_deactivated, #on_source_audio_mixers_changed, #on_source_audio_sync_offset_changed, #on_source_created, #on_source_destroyed, #on_source_filter_added, #on_source_filter_removed, #on_source_filter_visibility_changed, #on_source_filters_reordered, #on_source_mute_state_changed, #on_source_order_changed, #on_source_renamed, #on_source_volume_changed, #on_stream_started, #on_stream_starting, #on_stream_status, #on_stream_stopped, #on_stream_stopping, #on_studio_mode_switched, #on_switch_scenes, #on_switch_transition, #on_transition_begin, #on_transition_duration_changed, #on_transition_end, #on_transition_list_changed, #on_transition_video_end, #on_virtual_cam_started, #on_virtual_cam_stopped

Constructor Details

#initialize(websocket, executor: :io) ⇒ Client

Creates an OBS-websocket client.

websocket object must respond to the following methods:

  • text(str): send a text frame

  • on(event, &block): add an event handler

  • close(): close the connection

Parameters:

  • websocket (Object)

    the websocket object

  • executor (defaults to: :io)

    the executor on which the callbacks are invoked



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/obs/websocket.rb', line 34

def initialize(websocket, executor: :io)
  @websocket = websocket
  @response_dispatcher = ResponseDispatcher.new
  @event_dispatcher = EventDispatcher.new
  @executor = executor
  @on_open = Concurrent::Promises.resolvable_event
  @on_close = Concurrent::Promises.resolvable_future

  websocket.on(:open) do
    @on_open.resolve
  end

  websocket.on(:close) do |event|
    @on_close.resolve(true, [event.code, event.reason])
  end

  websocket.on(:message) do |event|
    handle_message(JSON.parse(event.data))
  end

  websocket.on(:error) do |event|
    $stderr.puts "Error: #{event.code} #{event.reason}"
  end
end

Instance Method Details

#authenticate!(password) ⇒ Future<:ok>

Authenticates the client to the server using the password.

Parameters:

  • password (String)

    the password

Returns:

  • (Future<:ok>)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/obs/websocket.rb', line 63

def authenticate!(password)
  get_auth_required.then do |h|
    if h.auth_required
      token = auth_token(
        password: password,
        salt: h.salt,
        challenge: h.challenge,
      )
      authenticate(auth: token).then { :ok }
    else
      :ok
    end
  end.flat
end

#closevoid

This method returns an undefined value.

Close the connection.



119
120
121
# File 'lib/obs/websocket.rb', line 119

def close
  @websocket.close
end

#on(type, executor: @executor) {|event| ... } ⇒ void

This method returns an undefined value.

Adds an event handler for obs-websocket event.

Parameters:

  • type (String)

    type of obs-websocket event to listen for

  • executor (defaults to: @executor)

    the executor on which the callback is invoked

Yields:

  • Called when the specified type of obs-websocket event is received.

Yield Parameters:

  • event (Event)

    the event object



111
112
113
114
# File 'lib/obs/websocket.rb', line 111

def on(type, executor: @executor, &listener)
  @event_dispatcher.register(executor, type, listener)
  nil
end

#on_close(executor: @executor) { ... } ⇒ Future

Adds an event handler for connection termination.

Parameters:

  • executor (defaults to: @executor)

    the executor on which the callback is invoked

Yields:

  • Called when obs-websocket connection is terminated.

Returns:

  • (Future)


96
97
98
99
100
101
102
# File 'lib/obs/websocket.rb', line 96

def on_close(executor: @executor, &listener)
  if listener
    @on_close.then_on(executor, &listener)
  else
    @on_close.with_default_executor(executor)
  end
end

#on_open(executor: @executor) { ... } ⇒ Event

Adds an event handler for connection establishment.

Parameters:

  • executor (defaults to: @executor)

    the executor on which the callback is invoked

Yields:

  • Called when obs-websocket connection is established.

Returns:

  • (Event)


83
84
85
86
87
88
89
# File 'lib/obs/websocket.rb', line 83

def on_open(executor: @executor, &listener)
  if listener
    @on_open.chain_on(executor, &listener)
  else
    @on_open.with_default_executor(executor)
  end
end