class EventdServer

Attributes

allowed_broadcast_channels[RW]

List of allowed broadcast channels

broadcasts_restricted[RW]

Broadcast restriction

clients[RW]

List of active clients on the server

options[RW]

Configuration options for em-websocket and the EventdServer

run_callback[RW]

Callback block which is called when the server starts for threading purposed

Public Class Methods

new(options = { :host => '127.0.0.1', :port => 8080 }) click to toggle source

Initialize the EventdServer with configuration options

Attributes

  • options - Configuration hash which will be used as the initial EventdServer configuration

Calls superclass method EventdObject::new
# File lib/eventd/eventd_server.rb, line 38
    def initialize(options = { :host => '127.0.0.1', :port => 8080 })
            super()
            @options = options

@clients = []
@broadcasts_restricted = false
@allowed_broadcast_channels = []
    end

Public Instance Methods

add_allowed_broadcast(channel) click to toggle source

Add an allowed broadcast channel

# File lib/eventd/eventd_server.rb, line 98
def add_allowed_broadcast(channel)
  @allowed_broadcast_channels.push channel
end
allow_broadcasts() click to toggle source

Allow any broadcast

# File lib/eventd/eventd_server.rb, line 86
def allow_broadcasts
  @broadcasts_restricted = false
end
broadcast_channel_allowed?(channel) click to toggle source

Check if a specific broadcast channel is allowed

# File lib/eventd/eventd_server.rb, line 122
def broadcast_channel_allowed?(channel)
  !@broadcasts_restricted or @allowed_broadcast_channels.include? channel
end
broadcast_channel_restricted?(channel) click to toggle source

Check if a specific broadcast channel is restricted

# File lib/eventd/eventd_server.rb, line 128
def broadcast_channel_restricted?(channel)
  @allowed_broadcast_channels.find_index channel == nil and @broadcasts_restricted
end
broadcasts_allowed?() click to toggle source

Check if broadcasts are allowed

# File lib/eventd/eventd_server.rb, line 110
def broadcasts_allowed?
  !@broadcasts_restricted
end
broadcasts_restricted?() click to toggle source

Check if broadcasts are restricted

# File lib/eventd/eventd_server.rb, line 116
def broadcasts_restricted?
  @broadcasts_restricted
end
configure(options) click to toggle source

Add to the EventdServer configuration

Attributes

  • options - Configuration hash to merge with the existing configuration

# File lib/eventd/eventd_server.rb, line 52
def configure(options)
        @options.merge! options
end
plugin(&callback) click to toggle source

Allows you to extend the functionality of a server, through a plugin

# File lib/eventd/eventd_server.rb, line 80
def plugin(&callback)
  callback.call self
end
remove_allowed_broadcast(channel) click to toggle source

Remove an allowed broadcast channel

# File lib/eventd/eventd_server.rb, line 104
def remove_allowed_broadcast(channel)
  @allowed_broadcast_channels.delete_if do |c| c == channel end
end
restrict_broadcasts() click to toggle source

Restrict broadcasts

# File lib/eventd/eventd_server.rb, line 92
def restrict_broadcasts
  @broadcasts_restricted = true
end
run(&callback) click to toggle source

Special event listener which is triggered when the server runs

# File lib/eventd/eventd_server.rb, line 74
      def run(&callback)
              @run_callback = callback
end
start() click to toggle source

Start the server

# File lib/eventd/eventd_server.rb, line 58
def start
        EM.run do
                @run_callback.call

                EM::WebSocket.run @options do |socket|
                        client = EventdClient.new socket, self
                        self.emit 'connection', client

self.clients.push client
handle_disconnection_for client
                end
        end
end

Private Instance Methods

handle_disconnection_for(client) click to toggle source
# File lib/eventd/eventd_server.rb, line 134
def handle_disconnection_for(client)
  client.on 'disconnect' do
    @clients.delete_if do |c| c == client end
    self.emit 'disconnection', client
  end
end