Class: SmartFox::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/smartfox/packet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, action, body, room = 0, extra = nil) ⇒ Packet

Returns a new instance of Packet.



6
7
8
9
10
11
12
# File 'lib/smartfox/packet.rb', line 6

def initialize(header, action, body, room = 0, extra = nil)
  @header = header
  @action = action
  @room = room.to_i
  @body = body
  @data = (@header == SmartFox::Client::HEADER_EXTENDED ? SmartFox::Packet.parse_extended(extra) : extra)
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



4
5
6
# File 'lib/smartfox/packet.rb', line 4

def action
  @action
end

#bodyObject (readonly)

Returns the value of attribute body.



4
5
6
# File 'lib/smartfox/packet.rb', line 4

def body
  @body
end

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/smartfox/packet.rb', line 4

def data
  @data
end

#headerObject (readonly)

Returns the value of attribute header.



4
5
6
# File 'lib/smartfox/packet.rb', line 4

def header
  @header
end

#roomObject (readonly)

Returns the value of attribute room.



4
5
6
# File 'lib/smartfox/packet.rb', line 4

def room
  @room
end

Class Method Details

.parse(data) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/smartfox/packet.rb', line 14

def self.parse(data)
  case data[0, 1]
  when '<'
    return parse_xml(data)
  when '{'
    return parse_json(data)
  else
    return parse_string(data)
  end
end

.parse_extended(data) ⇒ Object



43
44
45
46
47
# File 'lib/smartfox/packet.rb', line 43

def self.parse_extended(data)
  SmartFox::Logger.debug "SmartFox::Packet.parse_extended('#{data}')"
  document = LibXML::XML::Parser.string(data.first.content).parse
  parse_extended_object document.root
end

.parse_extended_object(node) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/smartfox/packet.rb', line 60

def self.parse_extended_object(node)
  result = {}

  node.children.each do |child|
    case child.name
    when 'var'
      result[child["n"].to_sym] = parse_extended_scaler(child)
    when 'obj'
      result[child["o"].to_sym] = parse_extended_object(child)
    end
  end

  result
end

.parse_extended_scaler(node) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/smartfox/packet.rb', line 49

def self.parse_extended_scaler(node)
  case node['t']
  when 'n'
    node.content.to_i
  when 's'
    node.content
  when 'b'
    node.content == "1"
  end
end

.parse_json(data) ⇒ Object



35
36
37
# File 'lib/smartfox/packet.rb', line 35

def self.parse_json(data)
  SmartFox::Logger.debug "SmartFox::Packet.parse_json('#{data}')"
end

.parse_string(data) ⇒ Object



39
40
41
# File 'lib/smartfox/packet.rb', line 39

def self.parse_string(data)
  SmartFox::Logger.debug "SmartFox::Packet.parse_string('#{data}')"
end

.parse_xml(data) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/smartfox/packet.rb', line 25

def self.parse_xml(data)
  SmartFox::Logger.debug "SmartFox::Packet.parse_xml('#{data}')"
  document = LibXML::XML::Parser.string(data).parse
  header = document.root['t']
  action = document.root.child['action']
  room = document.root.child['r']
  extra = document.root.child.children? ? document.root.child.children : nil
  new(header, action, document.root.child, room, extra)
end