/** * Tests sending to a socket * @dataProvider getValidEncodePayloads */ public function testSendToSocket($type, $payload) { $successfulSocket = $this->getMock('Wrench\\Socket\\ClientSocket', array(), array('wss://localhost:8000')); $failedSocket = clone $successfulSocket; $successfulSocket->expects($this->any())->method('send')->will($this->returnValue(true)); $failedSocket->expects($this->any())->method('send')->will($this->returnValue(false)); $this->payload->encode($payload, $type); $this->assertTrue($this->payload->sendToSocket($successfulSocket)); $this->assertFalse($this->payload->sendToSocket($failedSocket)); }
/** * Handle a complete payload received from the client * * Public because called from our PayloadHandler * * @param string $payload */ public function handlePayload(Payload $payload) { $app = $this->getClientApplication(); $this->log('Handling payload: ' . $payload->getPayload(), 'debug'); switch ($type = $payload->getType()) { case Protocol::TYPE_TEXT: if (method_exists($app, 'onData')) { $app->onData($payload, $this); } return; case Protocol::TYPE_BINARY: if (method_exists($app, 'onBinaryData')) { $app->onBinaryData($payload, $this); } else { $this->close(1003); } break; case Protocol::TYPE_PING: $this->log('Ping received', 'notice'); $this->send($payload->getPayload(), Protocol::TYPE_PONG); $this->log('Pong!', 'debug'); break; /** * A Pong frame MAY be sent unsolicited. This serves as a * unidirectional heartbeat. A response to an unsolicited Pong * frame is not expected. */ /** * A Pong frame MAY be sent unsolicited. This serves as a * unidirectional heartbeat. A response to an unsolicited Pong * frame is not expected. */ case Protocol::TYPE_PONG: $this->log('Received unsolicited pong', 'info'); break; case Protocol::TYPE_CLOSE: $this->log('Close frame received', 'notice'); $this->close(); $this->log('Disconnected', 'info'); break; default: throw new ConnectionException('Unhandled payload type'); } }