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

MAX_LENGTH, STATES

Instance Attribute Summary

Attributes inherited from WebSocket::Driver

#protocol, #ready_state

Instance Method Summary collapse

Methods inherited from WebSocket::Driver

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

Returns a new instance of Draft75.



5
6
7
8
# File 'lib/websocket/driver/draft75.rb', line 5

def initialize(socket, options = {})
  super
  @stage = 0
end

Instance Method Details

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



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

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

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



66
67
68
69
70
71
# File 'lib/websocket/driver/draft75.rb', line 66

def frame(data, type = nil, error_type = nil)
  return queue([data, type, error_type]) if @ready_state == 0
  frame = ["\x00", data, "\xFF"].map { |s| Driver.encode(s, :binary) } * ''
  @socket.write(Driver.encode(frame, :binary))
  true
end

#parse(buffer) ⇒ Object



21
22
23
24
25
26
27
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
# File 'lib/websocket/driver/draft75.rb', line 21

def parse(buffer)
  return if @ready_state > 1
  buffer = buffer.bytes if buffer.respond_to?(:bytes)

  buffer.each do |data|
    case @stage
      when -1 then
        @body << data
        send_handshake_body

      when 0 then
        parse_leading_byte(data)

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

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

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

#versionObject



10
11
12
# File 'lib/websocket/driver/draft75.rb', line 10

def version
  'hixie-75'
end