Class: EventdServer

Inherits:
EventdObject show all
Defined in:
lib/eventd/eventd_server.rb

Instance Attribute Summary collapse

Attributes inherited from EventdObject

#events

Instance Method Summary collapse

Methods inherited from EventdObject

#emit, #off, #on

Constructor Details

#initialize(options = { :host => '127.0.0.1', :port => 8080 }) ⇒ EventdServer

Initialize the EventdServer with configuration options

Attributes

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



38
39
40
41
42
43
44
45
# 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

Instance Attribute Details

#allowed_broadcast_channelsObject

List of allowed broadcast channels



28
29
30
# File 'lib/eventd/eventd_server.rb', line 28

def allowed_broadcast_channels
  @allowed_broadcast_channels
end

#broadcasts_restrictedObject

Broadcast restriction



31
32
33
# File 'lib/eventd/eventd_server.rb', line 31

def broadcasts_restricted
  @broadcasts_restricted
end

#clientsObject

List of active clients on the server



25
26
27
# File 'lib/eventd/eventd_server.rb', line 25

def clients
  @clients
end

#optionsObject

Configuration options for em-websocket and the EventdServer



19
20
21
# File 'lib/eventd/eventd_server.rb', line 19

def options
  @options
end

#run_callbackObject

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



22
23
24
# File 'lib/eventd/eventd_server.rb', line 22

def run_callback
  @run_callback
end

Instance Method Details

#add_allowed_broadcast(channel) ⇒ Object

Add an allowed broadcast channel



98
99
100
# File 'lib/eventd/eventd_server.rb', line 98

def add_allowed_broadcast(channel)
  @allowed_broadcast_channels.push channel
end

#allow_broadcastsObject

Allow any broadcast



86
87
88
# File 'lib/eventd/eventd_server.rb', line 86

def allow_broadcasts
  @broadcasts_restricted = false
end

#broadcast_channel_allowed?(channel) ⇒ Boolean

Check if a specific broadcast channel is allowed

Returns:

  • (Boolean)


122
123
124
# 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) ⇒ Boolean

Check if a specific broadcast channel is restricted

Returns:

  • (Boolean)


128
129
130
# 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?Boolean

Check if broadcasts are allowed

Returns:

  • (Boolean)


110
111
112
# File 'lib/eventd/eventd_server.rb', line 110

def broadcasts_allowed?
  !@broadcasts_restricted
end

#broadcasts_restricted?Boolean

Check if broadcasts are restricted

Returns:

  • (Boolean)


116
117
118
# File 'lib/eventd/eventd_server.rb', line 116

def broadcasts_restricted?
  @broadcasts_restricted
end

#configure(options) ⇒ Object

Add to the EventdServer configuration

Attributes

  • options - Configuration hash to merge with the existing configuration



52
53
54
# File 'lib/eventd/eventd_server.rb', line 52

def configure(options)
	@options.merge! options
end

#plugin(&callback) ⇒ Object

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



80
81
82
# File 'lib/eventd/eventd_server.rb', line 80

def plugin(&callback)
  callback.call self
end

#remove_allowed_broadcast(channel) ⇒ Object

Remove an allowed broadcast channel



104
105
106
# 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_broadcastsObject

Restrict broadcasts



92
93
94
# File 'lib/eventd/eventd_server.rb', line 92

def restrict_broadcasts
  @broadcasts_restricted = true
end

#run(&callback) ⇒ Object

Special event listener which is triggered when the server runs



74
75
76
# File 'lib/eventd/eventd_server.rb', line 74

def run(&callback)
@run_callback = callback
end

#startObject

Start the server



58
59
60
61
62
63
64
65
66
67
68
69
70
# 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