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, room = 0, extra = nil) ⇒ Packet

Returns a new instance of Packet.



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

def initialize(header, action, room = 0, extra = nil)
  @header = header
  @action = action
  @room = room
  @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

#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



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

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_json(data) ⇒ Object



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

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

.parse_string(data) ⇒ Object



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

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

.parse_xml(data) ⇒ Object



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

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.child
  new(header, action, room, extra)
end