Class: WebSocket::Driver

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/websocket/driver.rb,
lib/websocket/driver/hybi.rb,
lib/websocket/driver/proxy.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/hybi/frame.rb,
lib/websocket/driver/hybi/message.rb,
lib/websocket/driver/event_emitter.rb,
lib/websocket/driver/stream_reader.rb

Direct Known Subclasses

Draft75, Hybi, Server

Defined Under Namespace

Modules: EventEmitter Classes: Client, CloseEvent, ConnectEvent, Draft75, Draft76, Headers, Hybi, MessageEvent, OpenEvent, PingEvent, PongEvent, Proxy, Server, StreamReader

Constant Summary collapse

MAX_LENGTH =
0x3ffffff
STATES =
[:connecting, :open, :closing, :closed]
ProtocolError =
Class.new(StandardError)
URIError =
Class.new(ArgumentError)
ConfigurationError =
Class.new(ArgumentError)

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.


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/websocket/driver.rb', line 71

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

Instance Attribute Details

#protocolObject (readonly)

Returns the value of attribute protocol


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

def protocol
  @protocol
end

#ready_stateObject (readonly)

Returns the value of attribute ready_state


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

def ready_state
  @ready_state
end

Instance Method Details

#add_extension(extension) ⇒ Object


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

def add_extension(extension)
  false
end

#binary(message) ⇒ Object


122
123
124
# File 'lib/websocket/driver.rb', line 122

def binary(message)
  false
end

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


134
135
136
137
138
139
# File 'lib/websocket/driver.rb', line 134

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


126
127
128
# File 'lib/websocket/driver.rb', line 126

def ping(*args)
  false
end

#pong(*args) ⇒ Object


130
131
132
# File 'lib/websocket/driver.rb', line 130

def pong(*args)
  false
end

#set_header(name, value) ⇒ Object


93
94
95
96
97
# File 'lib/websocket/driver.rb', line 93

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

#startObject


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/websocket/driver.rb', line 99

def start
  return false unless @ready_state == 0

  unless Driver.websocket?(@socket.env)
    return fail_handshake(ProtocolError.new('Not a WebSocket request'))
  end

  begin
    response = handshake_response
  rescue => error
    return fail_handshake(error)
  end

  @socket.write(response)
  open unless @stage == -1
  true
end

#stateObject


84
85
86
87
# File 'lib/websocket/driver.rb', line 84

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

#text(message) ⇒ Object


117
118
119
120
# File 'lib/websocket/driver.rb', line 117

def text(message)
  message = Driver.encode(message, Encoding::UTF_8)
  frame(message, :text)
end