Class: WebSocket::Driver::Server

Inherits:
WebSocket::Driver show all
Defined in:
lib/websocket/driver/server.rb

Constant Summary collapse

EVENTS =
%w[open message error close ping pong]

Constants inherited from WebSocket::Driver

ConfigurationError, MAX_LENGTH, ProtocolError, STATES, URIError

Instance Attribute Summary

Attributes inherited from WebSocket::Driver

#protocol, #ready_state

Instance Method Summary collapse

Methods inherited from WebSocket::Driver

#add_extension, #binary, #close, #ping, #pong, #set_header, #start, #state, #text

Methods included from EventEmitter

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

Constructor Details

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

Returns a new instance of Server.


7
8
9
10
11
# File 'lib/websocket/driver/server.rb', line 7

def initialize(socket, options = {})
  super
  @http = HTTP::Request.new
  @delegate = nil
end

Instance Method Details

#envObject


13
14
15
# File 'lib/websocket/driver/server.rb', line 13

def env
  @http.complete? ? @http.env : nil
end

#parse(chunk) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/websocket/driver/server.rb', line 43

def parse(chunk)
  return @delegate.parse(chunk) if @delegate

  @http.parse(chunk)
  return fail_request('Invalid HTTP request') if @http.error?
  return unless @http.complete?

  @delegate = Driver.rack(self, @options)
  open

  EVENTS.each do |event|
    @delegate.on(event) { |e| emit(event, e) }
  end

  emit(:connect, ConnectEvent.new)
end

#urlObject


17
18
19
20
21
22
23
24
# File 'lib/websocket/driver/server.rb', line 17

def url
  return nil unless e = env

  url  = "ws://#{ e['HTTP_HOST'] }"
  url << e['PATH_INFO']
  url << "?#{ e['QUERY_STRING'] }" unless e['QUERY_STRING'] == ''
  url
end

#write(buffer) ⇒ Object


60
61
62
# File 'lib/websocket/driver/server.rb', line 60

def write(buffer)
  @socket.write(buffer)
end