module Faye::WebSocket::API

Constants

CLOSED
CLOSE_TIMEOUT
CLOSING
CONNECTING
OPEN
TYPES

Attributes

buffered_amount[R]
ready_state[R]
url[R]

Public Instance Methods

close(code = nil, reason = nil) click to toggle source
# File lib/faye/websocket/api.rb, line 79
def close(code = nil, reason = nil)
  code   ||= 1000
  reason ||= ''

  unless code == 1000 or (code >= 3000 and code <= 4999)
    raise ArgumentError, "Failed to execute 'close' on WebSocket: " +
                         "The code must be either 1000, or between 3000 and 4999. " +
                         "#{code} is neither."
  end

  @ready_state = CLOSING unless @ready_state == CLOSED
  @driver.close(reason, code)

  @close_timer = EventMachine.add_timer(CLOSE_TIMEOUT) { begin_close('', 1006) }
end
ping(message = '', &callback) click to toggle source
# File lib/faye/websocket/api.rb, line 74
def ping(message = '', &callback)
  return false if @ready_state > OPEN
  @driver.ping(message, &callback)
end
protocol() click to toggle source
# File lib/faye/websocket/api.rb, line 95
def protocol
  @driver.protocol || ''
end
send(message) click to toggle source
# File lib/faye/websocket/api.rb, line 64
def send(message)
  return false if @ready_state > OPEN
  case message
    when Numeric then @driver.text(message.to_s)
    when String  then @driver.text(message)
    when Array   then @driver.binary(message)
    else false
  end
end
write(data) click to toggle source
# File lib/faye/websocket/api.rb, line 60
def write(data)
  @stream.write(data)
end

Public Class Methods

new(options = {}) { || ... } click to toggle source
# File lib/faye/websocket/api.rb, line 22
def initialize(options = {})
  @ready_state = CONNECTING
  super()
  ::WebSocket::Driver.validate_options(options, [:headers, :extensions, :max_length, :ping, :proxy, :tls])

  @driver = yield

  if headers = options[:headers]
    headers.each { |name, value| @driver.set_header(name, value) }
  end

  [*options[:extensions]].each do |extension|
    @driver.add_extension(extension)
  end

  @ping            = options[:ping]
  @ping_id         = 0
  @buffered_amount = 0

  @close_params = @close_timer = @ping_timer = @proxy = @stream = nil
  @onopen = @onmessage = @onclose = @onerror = nil

  @driver.on(:open)    { |e| open }
  @driver.on(:message) { |e| receive_message(e.data) }
  @driver.on(:close)   { |e| begin_close(e.reason, e.code) }

  @driver.on(:error) do |error|
    emit_error(error.message)
  end

  if @ping
    @ping_timer = EventMachine.add_periodic_timer(@ping) do
      @ping_id += 1
      ping(@ping_id.to_s)
    end
  end
end