Class: SocketIo::Packet
- Inherits:
-
Object
- Object
- SocketIo::Packet
- 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
Direct Known Subclasses
AckPacket, ConnectPacket, DisconnectPacket, ErrorPacket, EventPacket, HeartbeatPacket, JsonPacket, MessagePacket, NoopPacket
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
-
#ack ⇒ Object
Returns the value of attribute ack.
-
#data ⇒ Object
Returns the value of attribute data.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#id ⇒ Object
Returns the value of attribute id.
-
#type_id ⇒ Object
Returns the value of attribute type_id.
Class Method Summary collapse
Instance Method Summary collapse
- #ackdata? ⇒ Boolean
-
#initialize(options = {}) ⇒ Packet
constructor
A new instance of Packet.
- #json ⇒ Object
- #to_s(payload = nil) ⇒ Object
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( = {}) .each do |name, value| self.send "#{name}=", value end end |
Instance Attribute Details
#ack ⇒ Object
Returns the value of attribute ack.
5 6 7 |
# File 'lib/socket.io/packet.rb', line 5 def ack @ack end |
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/socket.io/packet.rb', line 5 def data @data end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
5 6 7 |
# File 'lib/socket.io/packet.rb', line 5 def endpoint @endpoint end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/socket.io/packet.rb', line 5 def id @id end |
#type_id ⇒ Object
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
46 47 48 |
# File 'lib/socket.io/packet.rb', line 46 def ackdata? ack == 'data' end |
#json ⇒ Object
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 |