Sha256: ac8f9397ba8b5b9765ebf079a9ea42fd4efd169aeb026f64cb3e977e929d96cf

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

require 'thin'
require 'rack/sockjs'

module RhetButler
  class FollowerSession < SockJS::Session
    def initialize(connection)
      super
      @queue = connection.options[:queue]
    end

    def opened
      @queue.subscribe(self)
    end

    def close(*args)
      @queue.unsubscribe(self)
      super
    end
  end

  class LeaderSession < SockJS::Session
    def initialize(connection)
      super
      @queue = connection.options[:queue]
    end

    def process_message(message)
      @queue.current_slide = message
      @queue.enqueue(message)
    end
  end

  class SlideMessageQueue
    attr_accessor :current_slide
    def initialize
      @listeners = {}
    end

    def inspect
      "<<#{self.class.name} Listeners: #{@listeners.keys.length}>>"
    end

    def subscribe(session)
      @listeners[session] = true
      session.send(current_slide) unless current_slide.nil?
    end

    def unsubscribe(session)
      @listeners.delete_key(session)
    end

    def enqueue(message)
      @listeners.keys.each do |session|
        session.send(message)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rhet-butler-0.11.1 lib/rhet-butler/messaging.rb
rhet-butler-0.11.0 lib/rhet-butler/messaging.rb
rhet-butler-0.10.0 lib/rhet-butler/messaging.rb
rhet-butler-0.9.0 lib/rhet-butler/messaging.rb
rhet-butler-0.5.0 lib/rhet-butler/messaging.rb