private function initClient() { $this->client->on("connect", function () { $this->logger->notice("Connected to websocket."); $this->client->send('{"op":"unconfirmed_sub"}'); }); $this->client->on("message", function (WebSocketMessage $message) { $data = json_decode($message->getData(), true); $output = $data['x']['out']; foreach ($output as $receiver) { if (!isset($receiver['addr'])) { //some outputs does not have address, dafuq? continue; } $newAddresses = file_get_contents($this->newAddressesFile); if ($newAddresses) { $this->loadAddresses(); } file_put_contents($this->lastReadFile, \Carbon\Carbon::now()->toAtomString()); $address = $receiver['addr']; $amount = $receiver['value'] / 10000000; // $this->logger->notice("$address, $amount"); if (isset($this->addresses[$address])) { //todo: zjistit si, jestli není lepší mít to spíše jako hashset, aby to bylo rychlejší $this->transactionReceived($address, $amount); } } }); }
private function setupConnection() { $this->client = new WebSocket($this->context->url, $this->loop, $this->logger); $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!"); }); }
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(); } }); }
/** * @param callable|callable $callback */ public function onStasisEnd(callable $callback) { $this->wsClient->on(Event::STASIS_END, $callback); }
/** * @param callable|callable $callback */ public function onConnect(callable $callback) { $this->wsClient->on("connect", $callback); }
/** * @param callable|callable $callback */ public function onClose(callable $callback) { $this->wsClient->on("close", $callback); }