Class: SimplePusher::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_pusher/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(websocket) ⇒ Connection

Returns a new instance of Connection.



43
44
45
# File 'lib/simple_pusher/connection.rb', line 43

def initialize(websocket)
  @websocket = websocket
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/simple_pusher/connection.rb', line 4

def name
  @name
end

#websocketObject

Returns the value of attribute websocket.



3
4
5
# File 'lib/simple_pusher/connection.rb', line 3

def websocket
  @websocket
end

Class Method Details

.add_client(socket) ⇒ Object



24
25
26
27
# File 'lib/simple_pusher/connection.rb', line 24

def self.add_client(socket)
  client = new(socket)
  clients[socket] = client
end

.callbacksObject



10
11
12
# File 'lib/simple_pusher/connection.rb', line 10

def self.callbacks
  @@callbacks ||= {}
end

.clientsObject



6
7
8
# File 'lib/simple_pusher/connection.rb', line 6

def self.clients
  @@clients ||= {}
end

.countObject



39
40
41
# File 'lib/simple_pusher/connection.rb', line 39

def self.count
  clients.size
end

.on(event, &callback) ⇒ Object



14
15
16
17
# File 'lib/simple_pusher/connection.rb', line 14

def self.on(event, &callback)
  callbacks[event] ||= []
  callbacks[event] << callback
end

.publish(channel, message) ⇒ Object



33
34
35
36
37
# File 'lib/simple_pusher/connection.rb', line 33

def self.publish(channel, message)
  clients.each do |websocket, _client|
    websocket.send "#{channel}:#{message}"
  end
end

.remove_client(socket) ⇒ Object



29
30
31
# File 'lib/simple_pusher/connection.rb', line 29

def self.remove_client(socket)
  clients.delete(socket)
end

.trigger(event) ⇒ Object



19
20
21
22
# File 'lib/simple_pusher/connection.rb', line 19

def self.trigger(event)
  callbacks[event] ||= []
  callbacks[event].each { |callback| callback.call }
end

Instance Method Details

#emit(message) ⇒ Object



47
48
49
# File 'lib/simple_pusher/connection.rb', line 47

def emit(message)
  @websocket.send(message)
end