Class: WebSocket::Driver

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/websocket/driver.rb,
lib/websocket/driver/hybi.rb,
lib/websocket/driver/client.rb,
lib/websocket/driver/server.rb,
lib/websocket/driver/draft75.rb,
lib/websocket/driver/draft76.rb,
lib/websocket/driver/headers.rb,
lib/websocket/driver/event_emitter.rb,
lib/websocket/driver/hybi/stream_reader.rb

Direct Known Subclasses

Draft75, Hybi, Server

Defined Under Namespace

Modules: EventEmitter Classes: Client, CloseEvent, ConnectEvent, Draft75, Draft76, Headers, Hybi, MessageEvent, OpenEvent, ProtocolError, Server

Constant Summary collapse

MAX_LENGTH =
0x3ffffff
STATES =
[:connecting, :open, :closing, :closed]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventEmitter

#add_listener, #emit, #listener_count, #listeners, #on, #remove_all_listeners, #remove_listener

Constructor Details

#initialize(socket, options = {}) ⇒ Driver

Returns a new instance of Driver.



59
60
61
62
63
64
65
66
67
68
# File 'lib/websocket/driver.rb', line 59

def initialize(socket, options = {})
  super()

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

Instance Attribute Details

#protocolObject (readonly)

Returns the value of attribute protocol.



57
58
59
# File 'lib/websocket/driver.rb', line 57

def protocol
  @protocol
end

#ready_stateObject (readonly)

Returns the value of attribute ready_state.



57
58
59
# File 'lib/websocket/driver.rb', line 57

def ready_state
  @ready_state
end

Instance Method Details

#binary(message) ⇒ Object



92
93
94
# File 'lib/websocket/driver.rb', line 92

def binary(message)
  false
end

#close(reason = nil, code = nil) ⇒ Object



100
101
102
103
104
105
# File 'lib/websocket/driver.rb', line 100

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) ⇒ Object



96
97
98
# File 'lib/websocket/driver.rb', line 96

def ping(*args)
  false
end

#set_header(name, value) ⇒ Object



75
76
77
78
79
# File 'lib/websocket/driver.rb', line 75

def set_header(name, value)
  return false unless @ready_state <= 0
  @headers[name] = value
  true
end

#startObject



81
82
83
84
85
86
# File 'lib/websocket/driver.rb', line 81

def start
  return false unless @ready_state == 0
  @socket.write(Driver.encode(handshake_response, :binary))
  open unless @stage == -1
  true
end

#stateObject



70
71
72
73
# File 'lib/websocket/driver.rb', line 70

def state
  return nil unless @ready_state >= 0
  STATES[@ready_state]
end

#text(message) ⇒ Object



88
89
90
# File 'lib/websocket/driver.rb', line 88

def text(message)
  frame(message)
end