Encode the packet for transmission. If the packet contains binary elements, this function returns a list of packets where the first is the original packet with placeholders for the binary components and the remaining ones the binary attachments.
(self)
| 43 | self.attachment_count = self.decode(encoded_packet) or 0 |
| 44 | |
| 45 | def encode(self): |
| 46 | """Encode the packet for transmission. |
| 47 | |
| 48 | If the packet contains binary elements, this function returns a list |
| 49 | of packets where the first is the original packet with placeholders for |
| 50 | the binary components and the remaining ones the binary attachments. |
| 51 | """ |
| 52 | encoded_packet = str(self.packet_type) |
| 53 | if self.packet_type == BINARY_EVENT or self.packet_type == BINARY_ACK: |
| 54 | data, attachments = self.deconstruct_binary(self.data) |
| 55 | encoded_packet += str(len(attachments)) + '-' |
| 56 | else: |
| 57 | data = self.data |
| 58 | attachments = None |
| 59 | if self.namespace is not None and self.namespace != '/': |
| 60 | encoded_packet += self.namespace + ',' |
| 61 | if self.id is not None: |
| 62 | encoded_packet += str(self.id) |
| 63 | if data is not None: |
| 64 | encoded_packet += self.json.dumps(data, separators=(',', ':')) |
| 65 | if attachments is not None: |
| 66 | encoded_packet = [encoded_packet] + attachments |
| 67 | return encoded_packet |
| 68 | |
| 69 | def decode(self, encoded_packet): |
| 70 | """Decode a transmitted package. |