Sha256: cad8643c58ff129e1c221dcd21a3975288eeda94887a4d83443e40db4f433d1e

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require File.expand_path '../../sinatra-rocketio/version', File.dirname(__FILE__)
require 'sinatra/cometio/client'
require 'sinatra/websocketio/client'
require 'httparty'
require 'json'
require 'event_emitter'

module Sinatra
  module RocketIO
    class Client
      class Error < StandardError
      end

      include EventEmitter
      attr_reader :settings, :type, :io

      def initialize(url, opt={:type => :websocket})
        @settings = JSON.parse HTTParty.get("#{url}/rocketio/settings").body
        type = opt[:type].to_sym

        if type == :websocket and @settings['websocket']
          @type = :websocket
          @io = Sinatra::WebSocketIO::Client.new @settings['websocket']
        elsif type == :comet and @settings['comet']
          @type = :comet
          @io = Sinatra::CometIO::Client.new @settings['comet']
        else
          raise Error, "cannot find #{type} IO #{url}"
        end
        this = self
        if @io
          @io.on :* do |event_name, *args|
            if args.size > 1
              this.emit event_name, args[0], args[1]
            else
              this.emit event_name, args[0]
            end
          end
        end
        self
      end

      def connect
        @io.connect
        self
      end

      def close
        @io.close
      end

      def push(type, data)
        @io.push type, data
      end

      def method_missing(name, *args)
        @io.__send__ name, *args
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sinatra-rocketio-0.1.0 lib/sinatra/rocketio/client.rb