Class: SocketIo::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/socket.io/packet.rb,
lib/socket.io/packet/ack_packet.rb,
lib/socket.io/packet/json_packet.rb,
lib/socket.io/packet/noop_packet.rb,
lib/socket.io/packet/error_packet.rb,
lib/socket.io/packet/event_packet.rb,
lib/socket.io/packet/connect_packet.rb,
lib/socket.io/packet/message_packet.rb,
lib/socket.io/packet/heartbeat_packet.rb,
lib/socket.io/packet/disconnect_packet.rb

Defined Under Namespace

Classes: AckPacket, ConnectPacket, DisconnectPacket, ErrorPacket, EventPacket, HeartbeatPacket, JsonPacket, MessagePacket, NoopPacket

Constant Summary collapse

PACKET_REGEX =
/([^:]+):([0-9]+)?(\+)?:([^:]+)?:?([\s\S]*)?/
PACKET_TYPES =
[:disconnect, :connect, :heartbeat, :message, :json, :event, :ack, :error, :noop]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Packet

Returns a new instance of Packet.



36
37
38
39
40
# File 'lib/socket.io/packet.rb', line 36

def initialize(options = {})
  options.each do |name, value|
    self.send "#{name}=", value
  end
end

Instance Attribute Details

#ackObject

Returns the value of attribute ack.



5
6
7
# File 'lib/socket.io/packet.rb', line 5

def ack
  @ack
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/socket.io/packet.rb', line 5

def data
  @data
end

#endpointObject

Returns the value of attribute endpoint.



5
6
7
# File 'lib/socket.io/packet.rb', line 5

def endpoint
  @endpoint
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/socket.io/packet.rb', line 5

def id
  @id
end

#type_idObject

Returns the value of attribute type_id.



5
6
7
# File 'lib/socket.io/packet.rb', line 5

def type_id
  @type_id
end

Class Method Details

.parse(string) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/socket.io/packet.rb', line 14

def self.parse(string)
  pieces = string.match(PACKET_REGEX);
  return nil if pieces.nil?

  type = PACKET_TYPES[pieces[1].to_i]
  packet_class = self.const_get "#{type.capitalize}Packet"

  packet = packet_class.new :id => pieces[2] || '',
    :ack => pieces[3] ? 'data' : true,
    :data => pieces[5] || '',
    :endpoint => pieces[4] || ''

  packet.parse_pieces pieces if packet.respond_to?(:parse_pieces)
  packet
end

.parse_json(str) ⇒ Object



30
31
32
33
34
# File 'lib/socket.io/packet.rb', line 30

def self.parse_json(str)
  JSON.parse str
rescue
  false
end

Instance Method Details

#ackdata?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/socket.io/packet.rb', line 46

def ackdata?
  ack == 'data'
end

#jsonObject



42
43
44
# File 'lib/socket.io/packet.rb', line 42

def json
  @json ||= self.class.parse_json(data)
end

#to_s(payload = nil) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/socket.io/packet.rb', line 50

def to_s(payload = nil)
  parts = []
  parts << type_id
  parts << (id || '') + (ack == 'data' ? '+' : '') 
  parts << endpoint
  parts << payload if payload
  parts.join ':'
end