class Staticme::WebSocket

Attributes

params[RW]

Public Class Methods

new(params) click to toggle source
# File lib/staticme/web_socket.rb, line 11
def initialize(params)
  self.params = params
  @pool = []
end

Public Instance Methods

emit(event) click to toggle source
# File lib/staticme/web_socket.rb, line 29
def emit(event)
  Staticme.logger.debug("Broadcasting event: #{event.to_json}")
  @pool.each do |ws|
    next if ws.nil?
    ws.send event.to_json
  end
end
run!(&block) click to toggle source
# File lib/staticme/web_socket.rb, line 16
def run!(&block)
  EM.next_tick do
    Staticme.logger.debug "Starting WebSocket server on ws://#{params[:host]}:#{params[:ws_port]}"
    EM::WebSocket.run(:host => params[:host], :port => params[:ws_port]) do |ws|
      @pool.push ws
      ws.onclose do
        @pool.delete ws
      end
    end
    block.call if block_given?
  end
end
stop!() click to toggle source
# File lib/staticme/web_socket.rb, line 37
def stop!
  Staticme.logger.debug('Stopping WebSocket server')
  EM.stop!
  @pool = []
end