Class: WEBrick::Websocket::Frame

Inherits:
Object
  • Object
show all
Defined in:
lib/webrick/websocket/frame.rb

Overview

:nodoc:

Constant Summary collapse

@@ops_rev =
{
    cont: 0,
    text: 1,
    binary: 2,
    close: 8,
    ping: 9,
    pong: 10
}
@@ops =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, data = '') ⇒ Frame

Returns a new instance of Frame.



22
23
24
25
# File 'lib/webrick/websocket/frame.rb', line 22

def initialize(op, data = '')
  @op = op
  @payload = data
end

Instance Attribute Details

#opObject (readonly)

Returns the value of attribute op.



17
18
19
# File 'lib/webrick/websocket/frame.rb', line 17

def op
  @op
end

#payloadObject (readonly)

Returns the value of attribute payload.



17
18
19
# File 'lib/webrick/websocket/frame.rb', line 17

def payload
  @payload
end

Class Method Details

.parse(socket, prev = nil) ⇒ Object



18
19
20
# File 'lib/webrick/websocket/frame.rb', line 18

def self.parse(socket, prev = nil)
  Frame.new(nil).parse(socket, prev)
end

Instance Method Details

#binary?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/webrick/websocket/frame.rb', line 52

def binary?
  is(:binary)
end

#close?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/webrick/websocket/frame.rb', line 49

def close?
  is(:close)
end

#control?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/webrick/websocket/frame.rb', line 46

def control?
  @@ops_rev[@op] > 7
end

#fin?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/webrick/websocket/frame.rb', line 64

def fin?
  @fin
end

#parse(socket, prev) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/webrick/websocket/frame.rb', line 27

def parse(socket, prev)
  head = socket.read(1).unpack('C')[0]
  @fin = head & 0b10000000 > 0
  op = head & 0b00001111
  @op = @@ops[op]
  @op = prev if @op == :cont
  head = socket.read(1).unpack('C')[0]
  @masked = head & 0b10000000 > 0
  len = head & 0b01111111
  if len > 125
    long = len == 127
    len = socket.read(long ? 8 : 2).unpack(long ? 'Q' : 'S')[0]
  end
  @mask = socket.read(4).unpack('C4') if @masked
  @payload = socket.read(len)
  @payload = mask(@payload)
  self
end

#ping?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/webrick/websocket/frame.rb', line 58

def ping?
  is(:ping)
end

#pong?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/webrick/websocket/frame.rb', line 61

def pong?
  is(:pong)
end

#text?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/webrick/websocket/frame.rb', line 55

def text?
  is(:text)
end

#write(sock) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/webrick/websocket/frame.rb', line 68

def write(sock)
  head = 0b10000000 + @@ops_rev[@op]
  puts head.to_s(2)
  sock.write([head].pack('C'))
  @len = @payload.length
  lendata = if @len > 125
    if @len > 65535
      [127, @len].pack('CQ')
    else
      [126, @len].pack('CS')
    end
  else
    [@len].pack('C')
  end
  sock.write(lendata)
  sock.write(@payload)
  sock.flush
end