Sha256: e12d653e05a28938ed9ee0cb1876e543880fd8fe17104e4f97a7e5e6ed7ac7c8

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

module Faye
  class WebSocket
    
    module API
      module ReadyStates
        CONNECTING = 0
        OPEN       = 1
        CLOSING    = 2
        CLOSED     = 3
      end
      
      require File.expand_path('../api/event_target', __FILE__)
      require File.expand_path('../api/event', __FILE__)
      include EventTarget
      include ReadyStates
      
      attr_reader :url, :ready_state, :buffered_amount
      
      def receive(data)
        return false unless ready_state == OPEN
        event = Event.new('message')
        event.init_event('message', false, false)
        event.data = data
        dispatch_event(event)
      end
      
      def send(data, type = nil, error_type = nil)
        return false if ready_state == CLOSED
        data = WebSocket.encode(data) if String === data
        frame = @parser.frame(data, type, error_type)
        @stream.write(frame) if frame
      end
      
      def close(code = nil, reason = nil, ack = true)
        return if [CLOSING, CLOSED].include?(ready_state)
        
        @ready_state = CLOSING
        
        close = lambda do
          @ready_state = CLOSED
          @stream.close_connection_after_writing
          event = Event.new('close', :code => code || 1000, :reason => reason || '')
          event.init_event('close', false, false)
          dispatch_event(event)
        end
        
        if ack
          if @parser.respond_to?(:close)
            @parser.close(code, reason, &close)
          else
            close.call
          end
        else
          @parser.close(code, reason) if @parser.respond_to?(:close)
          close.call
        end
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
faye-websocket-0.3.0 lib/faye/websocket/api.rb