Class: SimplePusher::Server

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/simple_pusher/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



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

def initialize
  @host = '0.0.0.0'
  @port = SimplePusher.configuration.port
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/simple_pusher/server.rb', line 8

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/simple_pusher/server.rb', line 8

def port
  @port
end

Class Method Details

.startObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/simple_pusher/server.rb', line 15

def self.start
  $stderr.puts 'Start SimplePusher ...' if SimplePusher.configuration.debug
  EM::WebSocket.run(host: instance.host, port: instance.port) do |socket|
    socket.onopen do |handshake|
      $stderr.puts 'on open:' + handshake.inspect if SimplePusher.configuration.debug
      Connection.add_client(socket)
    end

    socket.onclose do
      $stderr.puts 'on close'
      Connection.remove_client(socket)
    end

    socket.onmessage do |raw_message|
      $stderr.puts 'on message: ' + raw_message if SimplePusher.configuration.debug
      action, *message = raw_message.split(':')

      case action
       when 'broadcast'
         message = message.join(':')
         Connection.broadcast(message)
       when 'emit'
         event = message[0]
         Connection.trigger(event)
       else
        # TODO
      end
    end

  end
end