Class: Faye::WebSocket::Draft75Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/faye/websocket/draft75_parser.rb

Direct Known Subclasses

Draft76Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(web_socket, options = {}) ⇒ Draft75Parser

Returns a new instance of Draft75Parser.



7
8
9
10
# File 'lib/faye/websocket/draft75_parser.rb', line 7

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

Instance Attribute Details

#protocolObject (readonly)

Returns the value of attribute protocol.



5
6
7
# File 'lib/faye/websocket/draft75_parser.rb', line 5

def protocol
  @protocol
end

Instance Method Details

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



79
80
81
82
# File 'lib/faye/websocket/draft75_parser.rb', line 79

def frame(data, type = nil, error_type = nil)
  return WebSocket.encode(data) if Array === data
  ["\x00", data, "\xFF"].map(&WebSocket.method(:encode)) * ''
end

#handshake_responseObject



16
17
18
19
20
21
22
23
24
# File 'lib/faye/websocket/draft75_parser.rb', line 16

def handshake_response
  upgrade =  "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"
  upgrade << "Upgrade: WebSocket\r\n"
  upgrade << "Connection: Upgrade\r\n"
  upgrade << "WebSocket-Origin: #{@socket.env['HTTP_ORIGIN']}\r\n"
  upgrade << "WebSocket-Location: #{@socket.url}\r\n"
  upgrade << "\r\n"
  upgrade
end

#open?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/faye/websocket/draft75_parser.rb', line 26

def open?
  true
end

#parse(buffer) ⇒ Object



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
# File 'lib/faye/websocket/draft75_parser.rb', line 30

def parse(buffer)
  buffer.each_byte do |data|
    case @stage
      when 0 then
        parse_leading_byte(data)
        
      when 1 then
        value = (data & 0x7F)
        @length = value + 128 * @length
        
        if @closing and @length.zero?
          @socket.close(nil, nil, false)
        elsif (0x80 & data) != 0x80
          if @length.zero?
            @socket.receive('')
            @stage = 0
          else
            @buffer = []
            @stage = 2
          end
        end
        
      when 2 then
        if data == 0xFF
          @socket.receive(WebSocket.encode(@buffer))
          @stage = 0
        else
          @buffer << data
          if @length and @buffer.size == @length
            @stage = 0
          end
        end
    end
  end
  
  nil
end

#parse_leading_byte(data) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/faye/websocket/draft75_parser.rb', line 68

def parse_leading_byte(data)
  if (0x80 & data) == 0x80
    @length = 0
    @stage = 1
  else
    @length = nil
    @buffer = []
    @stage = 2
  end
end

#versionObject



12
13
14
# File 'lib/faye/websocket/draft75_parser.rb', line 12

def version
  'hixie-75'
end