Sha256: d8fab36e9a7260ac849bb85f3fd348b144283724ffb9a47e1a464cbd98b0cfce

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

class Ircmad
  class WebSocket
    include Configurable

    def initialize(&block)
      instance_eval(&block) if block_given?
    end

    def subscribers
      @subscribers ||= {}
    end

    def host
      '127.0.0.1'
    end

    # http://qiita.com/items/bf47e254d662af1294d8#
    def port
      unless @port || config[:port]
        s = TCPServer.open(0)
        @port = s.addr[1]
        s.close
      end
      @port || config[:port]
    end

    def run!
      puts "Stating WebSocket server on #{host}:#{port}"
      EM::WebSocket.start(:host => host, :port => port) do |socket|
        socket.onopen do |sock|
          subscribers[socket.object_id] = Ircmad.get_channel.subscribe { |msg| socket.send msg.to_json }
        end

        socket.onclose do |sock|
          Ircmad.get_channel.unsubscribe(subscribers[socket.object_id])
        end

        socket.onmessage do |msg|
          Ircmad.post_channel << msg
        end

        socket.onerror do |error|
          puts error
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ircmad-0.0.2 lib/ircmad/web_socket.rb