* Encode a packet as a single string if non-binary, or as a * buffer sequence, depending on packet type. * * @param {Object} obj - packet object
(obj: Packet)
| 61 | * @param {Object} obj - packet object |
| 62 | */ |
| 63 | public encode(obj: Packet) { |
| 64 | debug("encoding packet %j", obj); |
| 65 | |
| 66 | if (obj.type === PacketType.EVENT || obj.type === PacketType.ACK) { |
| 67 | if (hasBinary(obj)) { |
| 68 | return this.encodeAsBinary({ |
| 69 | type: |
| 70 | obj.type === PacketType.EVENT |
| 71 | ? PacketType.BINARY_EVENT |
| 72 | : PacketType.BINARY_ACK, |
| 73 | nsp: obj.nsp, |
| 74 | data: obj.data, |
| 75 | id: obj.id, |
| 76 | }); |
| 77 | } |
| 78 | } |
| 79 | return [this.encodeAsString(obj)]; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Encode packet as string. |