class WebSocket::Driver - websocket-driver-0.6.5 Documentation

# class WebSocket::Driver

### Constants

BINARY
CloseEvent
ConfigurationError
ConnectEvent
MAX_LENGTH
MessageEvent
OpenEvent
ProtocolError
STATES
UNICODE
URIError

### Attributes

protocol[R]
ready_state[R]

### Public Class Methods

new(socket, options = {}) click to toggle source
Calls superclass method WebSocket::Driver::EventEmitter.new

    # File lib/websocket/driver.rb, line 72
    def initialize(socket, options = {})
      super()
      Driver.validate_options(options, [:max_length, :masking, :require_masking, :protocols])

      @socket      = socket
      @reader      = StreamReader.new
      @options     = options
      @max_length  = options[:max_length] || MAX_LENGTH
      @headers     = Headers.new
      @queue       = []
      @ready_state = 0
    end

### Private Class Methods

client(socket, options = {}) click to toggle source

    # File lib/websocket/driver.rb, line 147
    def self.client(socket, options = {})
      Client.new(socket, options.merge(:masking => true))
    end

encode(string, encoding = nil) click to toggle source

    # File lib/websocket/driver.rb, line 166
    def self.encode(string, encoding = nil)
      case string
        when Array then
          string = string.pack('C*')
          encoding ||= BINARY
        when String then
          encoding ||= UNICODE
      end
      unless string.encoding.name == encoding
        string = string.dup if string.frozen?
        string.force_encoding(encoding)
      end
      string.valid_encoding? ? string : nil
    end

rack(socket, options = {}) click to toggle source

    # File lib/websocket/driver.rb, line 155
    def self.rack(socket, options = {})
      env = socket.env
      if env['HTTP_SEC_WEBSOCKET_VERSION']
        Hybi.new(socket, options.merge(:require_masking => true))
      elsif env['HTTP_SEC_WEBSOCKET_KEY1']
        Draft76.new(socket, options)
      else
        Draft75.new(socket, options)
      end
    end

server(socket, options = {}) click to toggle source

    # File lib/websocket/driver.rb, line 151
    def self.server(socket, options = {})
      Server.new(socket, options.merge(:require_masking => true))
    end

validate_options(options, valid_keys) click to toggle source

    # File lib/websocket/driver.rb, line 181
    def self.validate_options(options, valid_keys)
      options.keys.each do |key|
        unless valid_keys.include?(key)
          raise ConfigurationError, "Unrecognized option: #{key.inspect}"
        end
      end
    end

websocket?(env) click to toggle source

    # File lib/websocket/driver.rb, line 189
    def self.websocket?(env)
      connection = env['HTTP_CONNECTION'] || ''
      upgrade    = env['HTTP_UPGRADE']    || ''

      env['REQUEST_METHOD'] == 'GET' and
      connection.downcase.split(/ *, */).include?('upgrade') and
      upgrade.downcase == 'websocket'
    end

### Public Instance Methods

add_extension(extension) click to toggle source

    # File lib/websocket/driver.rb, line 90
    def add_extension(extension)
      false
    end

binary(message) click to toggle source

    # File lib/websocket/driver.rb, line 114
    def binary(message)
      false
    end

close(reason = nil, code = nil) click to toggle source

    # File lib/websocket/driver.rb, line 126
    def close(reason = nil, code = nil)
      return false unless @ready_state == 1
      @ready_state = 3
      emit(:close, CloseEvent.new(nil, nil))
      true
    end

ping(*args) click to toggle source

    # File lib/websocket/driver.rb, line 118
    def ping(*args)
      false
    end

pong(*args) click to toggle source

    # File lib/websocket/driver.rb, line 122
    def pong(*args)
      false
    end

set_header(name, value) click to toggle source

    # File lib/websocket/driver.rb, line 94
    def set_header(name, value)
      return false unless @ready_state <= 0
      @headers[name] = value
      true
    end

start() click to toggle source

    # File lib/websocket/driver.rb, line 100
    def start
      return false unless @ready_state == 0
      response = handshake_response
      return false unless response
      @socket.write(response)
      open unless @stage == -1
      true
    end

state() click to toggle source

    # File lib/websocket/driver.rb, line 85
    def state
      return nil unless @ready_state >= 0
      STATES[@ready_state]
    end

text(message) click to toggle source

    # File lib/websocket/driver.rb, line 109
    def text(message)
      message = message.encode(UNICODE) unless message.encoding.name == UNICODE
      frame(message, :text)
    end

### Private Instance Methods

open() click to toggle source

    # File lib/websocket/driver.rb, line 135
    def open
      @ready_state = 1
      @queue.each { |message| frame(*message) }
      @queue = []
      emit(:open, OpenEvent.new)
    end

queue(message) click to toggle source

    # File lib/websocket/driver.rb, line 142
    def queue(message)
      @queue << message
      true
    end