/** * Disconnect and stop the event loop */ public function stop() { $this->wsClient->close(); $this->ariClient->onClose(function () { $this->eventLoop->stop(); }); }
public function receive(callable $callback, callable $error) { $should_close = false; $this->client->on('request', function () { $this->logger->notice('Request object created!'); }); $this->client->on('handshake', function () { $this->logger->notice('Handshake received!'); }); $this->client->on('connect', function () { $this->logger->notice('Connected!'); }); $this->client->on('error', function () use($error) { // Call the error callback. $error(); // Reopen the connection. $this->client->close(); $this->client->open(self::TIMEOUT); }); $this->client->on('close', function () use($should_close) { // Reopen the connection. if (!$should_close) { $this->client->open(self::TIMEOUT); } }); $this->client->on('message', function (WebSocketMessageInterface $message) use($callback, &$should_close) { // Wait for the message to be finalized. if (!$message->isFinalised()) { return; } // Get the contents. $contents = json_decode($message->getData(), true); if (!$contents) { return; } // Handle the contents. $value = $this->handleContents($contents); // Call the callback with the value. $result = $callback($value); // If the callback returns true, close the connection. if ($result === true) { $should_close = true; $this->client->close(); } }); }
function test_DoubleEchoResourceHandlerResponse() { $input = str_repeat("a", 1024); $input2 = str_repeat("b", 1024); $msg = WebSocketMessage::create($input); $client = new WebSocket("wss://127.0.0.1:12345/echo/"); $client->setTimeOut(1000); $client->open(); $client->sendMessage($msg); $client->sendMessage(WebSocketMessage::create($input2)); $msg = $client->readMessage(); $msg2 = $client->readMessage(); $client->close(); $this->assertEquals($input, $msg->getData()); $this->assertEquals($input2, $msg2->getData()); }
/** * Disconnect and stop the event loop */ public function stop() { $this->wsClient->close(); $this->eventLoop->stop(); }