Class: Hyperloop::IncomingBroadcast

Inherits:
Object
  • Object
show all
Defined in:
lib/hyper-operation/transport/client_drivers.rb

Class Method Summary collapse

Class Method Details

.add_connection(channel_name, id = nil) ⇒ Object


62
63
64
65
66
# File 'lib/hyper-operation/transport/client_drivers.rb', line 62

def self.add_connection(channel_name, id = nil)
  channel_string = "#{channel_name}#{'-'+id.to_s if id}"
  open_channels << channel_string
  channel_string
end

.connect_to(channel_name, id = nil) ⇒ Object


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
97
98
99
100
101
102
103
104
105
106
# File 'lib/hyper-operation/transport/client_drivers.rb', line 68

def self.connect_to(channel_name, id = nil)
  channel_string = add_connection(channel_name, id)
  if ClientDrivers.opts[:transport] == :pusher
    channel = "#{ClientDrivers.opts[:channel]}-#{channel_string}"
    %x{
      var channel = #{ClientDrivers.opts[:pusher_api]}.subscribe(#{channel.gsub('::', '==')});
      channel.bind('dispatch', #{ClientDrivers.opts[:dispatch]})
      channel.bind('pusher:subscription_succeeded', #{lambda {ClientDrivers.get_queued_data("connect-to-transport", channel_string)}})
    }
  elsif ClientDrivers.opts[:transport] == :action_cable
    channel = "#{ClientDrivers.opts[:channel]}-#{channel_string}"
    Hyperloop::HTTP.post(ClientDrivers.polling_path('action-cable-auth', channel), headers: { 'X-CSRF-Token' => ClientDrivers.opts[:form_authenticity_token] }).then do |response|
      %x{
        var fix_opal_0110 = 'return';
        #{Hyperloop.action_cable_consumer}.subscriptions.create(
          {
            channel: "Hyperloop::ActionCableChannel",
            client_id: #{ClientDrivers.opts[:id]},
            hyperloop_channel: #{channel_string},
            authorization: #{response.json[:authorization]},
            salt: #{response.json[:salt]}
          },
          {
            connected: function() {
              if (#{ClientDrivers.env == 'development'}) { console.log("ActionCable connected to: ", channel_string); }
              #{ClientDrivers.complete_connection(channel_string)}
            },
            received: function(data) {
              if (#{ClientDrivers.env == 'development'}) { console.log("ActionCable received: ", data); }
              #{ClientDrivers.sync_dispatch(JSON.parse(`JSON.stringify(data)`)['data'])}
            }
          }
        )
      }
    end
  else
    Hyperloop::HTTP.get(ClientDrivers.polling_path(:subscribe, channel_string))
  end
end

.open_channelsObject


58
59
60
# File 'lib/hyper-operation/transport/client_drivers.rb', line 58

def self.open_channels
  @open_channels ||= Set.new
end