Class: WebSocket::Driver::Draft75

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

Direct Known Subclasses

Draft76

Constant Summary

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, #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 = {}) ⇒ Draft75

Returns a new instance of Draft75.


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/websocket/driver/draft75.rb', line 5

def initialize(socket, options = {})
  super

  @stage   = 0
  @closing = false

  @headers['Upgrade']            = 'WebSocket'
  @headers['Connection']         = 'Upgrade'
  @headers['WebSocket-Origin']   = @socket.env['HTTP_ORIGIN']
  @headers['WebSocket-Location'] = @socket.url
end

Instance Method Details

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


21
22
23
24
25
26
# File 'lib/websocket/driver/draft75.rb', line 21

def close(reason = nil, code = nil)
  return false if @ready_state == 3
  @ready_state = 3
  emit(:close, CloseEvent.new(nil, nil))
  true
end

#frame(buffer, type = nil, error_type = nil) ⇒ Object


73
74
75
76
77
78
# File 'lib/websocket/driver/draft75.rb', line 73

def frame(buffer, type = nil, error_type = nil)
  return queue([buffer, type, error_type]) if @ready_state == 0
  frame = [0x00, buffer, 0xFF].pack('CA*C')
  @socket.write(frame)
  true
end

#parse(chunk) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/websocket/driver/draft75.rb', line 28

def parse(chunk)
  return if @ready_state > 1

  @reader.put(chunk)

  @reader.each_byte do |octet|
    case @stage
      when -1 then
        @body << octet
        send_handshake_body

      when 0 then
        parse_leading_byte(octet)

      when 1 then
        @length = (octet & 0x7F) + 128 * @length

        if @closing and @length.zero?
          return close
        elsif (octet & 0x80) != 0x80
          if @length.zero?
            @stage = 0
          else
            @skipped = 0
            @stage   = 2
          end
        end

      when 2 then
        if octet == 0xFF
          @stage = 0
          emit(:message, MessageEvent.new(Driver.encode(@buffer, Encoding::UTF_8)))
        else
          if @length
            @skipped += 1
            @stage = 0 if @skipped == @length
          else
            @buffer << octet
            return close if @buffer.size > @max_length
          end
        end
    end
  end
end

#versionObject


17
18
19
# File 'lib/websocket/driver/draft75.rb', line 17

def version
  'hixie-75'
end