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]

Constants inherited from WebSocket::Driver

MAX_LENGTH, STATES

Instance Attribute Summary

Attributes inherited from WebSocket::Driver

#protocol, #ready_state

Instance Method Summary collapse

Methods inherited from WebSocket::Driver

#binary, #close, #ping, #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
# File 'lib/websocket/driver/server.rb', line 7

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

Instance Method Details

#envObject



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

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

#parse(buffer) ⇒ Object



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

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

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

  @delegate = Driver.rack(self, @options)
  @delegate.on(:open) { open }
  EVENTS.each do |event|
    @delegate.on(event) { |e| emit(event, e) }
  end

  emit(:connect, ConnectEvent.new)
end

#urlObject



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

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



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

def write(data)
  @socket.write(Driver.encode(data, :binary))
end