* Called upon transport packet. * * @param {Object} packet * @private
(packet: Packet)
| 151 | * @private |
| 152 | */ |
| 153 | private onPacket(packet: Packet) { |
| 154 | if (class="st">"open" !== this.readyState) { |
| 155 | return debug(class="st">"packet received with closed socket"); |
| 156 | } |
| 157 | class="cm">// export packet event |
| 158 | debug(`received packet ${packet.type}`); |
| 159 | this.emit(class="st">"packet", packet); |
| 160 | |
| 161 | switch (packet.type) { |
| 162 | case class="st">"ping": |
| 163 | if (this.transport.protocol !== 3) { |
| 164 | this.onError(new Error(class="st">"invalid heartbeat direction")); |
| 165 | return; |
| 166 | } |
| 167 | debug(class="st">"got ping"); |
| 168 | this.pingTimeoutTimer.refresh(); |
| 169 | this.sendPacket(class="st">"pong"); |
| 170 | this.emit(class="st">"heartbeat"); |
| 171 | break; |
| 172 | |
| 173 | case class="st">"pong": |
| 174 | if (this.transport.protocol === 3) { |
| 175 | this.onError(new Error(class="st">"invalid heartbeat direction")); |
| 176 | return; |
| 177 | } |
| 178 | debug(class="st">"got pong"); |
| 179 | clearTimeout(this.pingTimeoutTimer); |
| 180 | this.pingIntervalTimer.refresh(); |
| 181 | this.emit(class="st">"heartbeat"); |
| 182 | break; |
| 183 | |
| 184 | case class="st">"error": |
| 185 | this.onClose(class="st">"parse error"); |
| 186 | break; |
| 187 | |
| 188 | case class="st">"message": |
| 189 | this.emit(class="st">"data", packet.data); |
| 190 | this.emit(class="st">"message", packet.data); |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Called upon transport error. |
no test coverage detected