diff options
author | Joe Rayhawk <jrayhawk@action.fairlystable.org> | 2017-09-27 15:20:47 -0700 |
---|---|---|
committer | Joe Rayhawk <jrayhawk@action.fairlystable.org> | 2017-09-27 15:20:47 -0700 |
commit | 707d546c05c11bb0a13d404be4e1cee991965612 (patch) | |
tree | 6b3338bf075871df2ecfae0d418bbab7bc49a971 | |
parent | 8d38b2617412e39730bce0b1b99b8d9a0ee79545 (diff) | |
download | twitchtools-707d546c05c11bb0a13d404be4e1cee991965612.tar.gz twitchtools-707d546c05c11bb0a13d404be4e1cee991965612.zip |
ircobsbridge: functionalize websocket output, shuffle websocket creation around to reconnect more robustly
-rw-r--r-- | ircobsbridge.rb | 81 |
1 files changed, 43 insertions, 38 deletions
diff --git a/ircobsbridge.rb b/ircobsbridge.rb index 124f540..103c7a2 100644 --- a/ircobsbridge.rb +++ b/ircobsbridge.rb @@ -166,10 +166,6 @@ def websocket_create() end end -websocket_create() -frame = WebSocket::Frame::Incoming::Client.new -frame_decoded = String.new -status = Hash.new $lastused = Hash.new $globallastused = 0 @@ -219,6 +215,43 @@ def command_dispatch( mode, user, command, arg1 ) end end +def frame_dispatch( frame ) + if frame["update-type"] == "StreamStatus" + $status = frame + elsif frame['current-scene'] && frame['scenes'] # GetSceneList + text = '| Current scene: ' + frame['current-scene'] + ' | Available: ' + frame['scenes'].map{|v| v['name'] }.sort.join(' ') + "\n" + print text + ircsocketwrite( text ) + elsif frame['name'] && frame['sources'] # GetCurrentScene + pp frame + text = '| Sources: ' + frame['sources'].map{|v| v['name'] }.sort.join(' ') + "\n" + print text + if frame['message-id'] == '1' + ircsocketwrite( text ) + else # other message-ids are a source we want to toggle + frame['sources'].each do |v| + if v['name'] == frame['message-id'] + if v['render'] == true + SetSourceRender( v['name'], 'false', '2' ) + ircsocketwrite( '| Source ' + v['name'] + " is now unrendered\n" ) + elsif v['render'] == false + SetSourceRender( v['name'], 'true', '2' ) + ircsocketwrite( '| Source ' + v['name'] + " is now visible\n" ) + end + end + end + end + elsif frame['update-type'] == 'SwitchScenes' + ircsocketwrite( '| Scene is now ' + frame['scene-name'] + "\n" ) + #elsif frame['update-type'] == 'SceneItemVisibilityChanged' + # ircsocketwrite( '| Source ' + frame['item-name'] + ' visibility is now ' + frame['item-visible'].to_s + "\n" ) + elsif frame['authRequired'] == true # GetAuthRequired + websocketwrite( '{ "request-type": "Authenticate", "message-id" : "2", "auth" : "' + Digest::SHA256.base64digest(Digest::SHA256.base64digest($OBS_PASSWORD + frame['salt'])+frame['challenge']) + '" }' ) + else + print frame + end +end + # irclog read loop event thread ircreader = Thread.new { begin @@ -234,6 +267,11 @@ ensure end } +websocket_create() +frame = WebSocket::Frame::Incoming::Client.new +frame_decoded = String.new +$status = Hash.new + # websocket read loop event thread obsreader = Thread.new { begin @@ -241,40 +279,7 @@ obsreader = Thread.new { frame << $tcp_socket.read_nonblock( 4096 ) while ( frame_decoded = frame.next.to_s ) != "" frame_parsed = JSON.parse(frame_decoded) - if frame_parsed["update-type"] == "StreamStatus" - status = frame_parsed - elsif frame_parsed['current-scene'] && frame_parsed['scenes'] # GetSceneList - text = '| Current scene: ' + frame_parsed['current-scene'] + ' | Available: ' + frame_parsed['scenes'].map{|v| v['name'] }.sort.join(' ') + "\n" - print text - ircsocketwrite( text ) - elsif frame_parsed['name'] && frame_parsed['sources'] # GetCurrentScene - pp frame_parsed - text = '| Sources: ' + frame_parsed['sources'].map{|v| v['name'] }.sort.join(' ') + "\n" - print text - if frame_parsed['message-id'] == '1' - ircsocketwrite( text ) - else # other message-ids are a source we want to toggle - frame_parsed['sources'].each do |v| - if v['name'] == frame_parsed['message-id'] - if v['render'] == true - SetSourceRender( v['name'], 'false', '2' ) - ircsocketwrite( '| Source ' + v['name'] + " is now unrendered\n" ) - elsif v['render'] == false - SetSourceRender( v['name'], 'true', '2' ) - ircsocketwrite( '| Source ' + v['name'] + " is now visible\n" ) - end - end - end - end - elsif frame_parsed['update-type'] == 'SwitchScenes' - ircsocketwrite( '| Scene is now ' + frame_parsed['scene-name'] + "\n" ) - #elsif frame_parsed['update-type'] == 'SceneItemVisibilityChanged' - # ircsocketwrite( '| Source ' + frame_parsed['item-name'] + ' visibility is now ' + frame_parsed['item-visible'].to_s + "\n" ) - elsif frame_parsed['authRequired'] == true # GetAuthRequired - websocketwrite( '{ "request-type": "Authenticate", "message-id" : "2", "auth" : "' + Digest::SHA256.base64digest(Digest::SHA256.base64digest($OBS_PASSWORD + frame_parsed['salt'])+frame_parsed['challenge']) + '" }' ) - else - print frame_decoded - end + frame_dispatch( frame_parsed ) end #pp JSON.parse(frame_decoded) end |