private function getData($message) { $loop = React\EventLoop\Factory::create(); $tcpConnector = new TcpConnector($loop); $i = 0; $status_text = ""; $services = $this->services; //$this->services->log('Starting connector'); $tcpConnector->create($this->host, $this->port)->then(function (React\Stream\Stream $stream) use(&$status_text, $message, $services) { $command_count = 0; $stream->on('data', function ($data) use(&$command_count, $stream, &$status_text, $message) { if ($command_count == 0) { //$services->log($message); $hash = $this->generateHash($data, $command_count, $message); $_message = sprintf("%s %s\n", $hash, $message); $stream->write($_message); $command_count++; } else { $status_text .= $data; if (substr($status_text, strlen($status_text) - 1, 1) == "\n") { $stream->end(); } } }); }); $loop->run(); $status_text = str_replace(['{', '}', '(', ')', '%', 'undef', '*::FH'], ['[', ']', '[', ']', '$', 'null', 'null'], $status_text); return $status_text; }
public function sendAsReact($data) { $loop = Factory::create(); $tcpConnector = new TcpConnector($loop); $tcpConnector->create('127.0.0.1', 4001)->then(function (Stream $stream) use($data) { $stream->write($data); $stream->end(); }); $loop->run(); }
/** * @return PromiseInterface */ protected function getConnection() { if (!$this->connection) { if (!empty($this->options['socket'])) { $connector = new UnixConnector($this->loop); $connection = $connector->create($this->options['socket']); } else { $connector = new TcpConnector($this->loop); $connection = $connector->create($this->options['host'], $this->options['port']); } $this->connection = $connection->then([$this, 'handleInit']); } return $this->connection; }
/** * Handles incoming connections from $this->port. Basically redirects to a slave. * * @param Connection $incoming incoming connection from react */ public function onWeb(Connection $incoming) { // preload sent data from $incoming to $buffer, otherwise it would be lost, // since getNextSlave is async. $redirect = null; $buffer = ''; $incoming->on('data', function ($data) use(&$redirect, &$buffer) { if (!$redirect) { $buffer .= $data; } }); $this->getNextSlave(function ($id) use($incoming, &$buffer, &$redirect) { $slave =& $this->slaves[$id]; $slave['busy'] = true; $slave['connections']++; $this->tcpConnector->create('127.0.0.1', $slave['port'])->then(function (\React\Stream\Stream $stream) use(&$buffer, $redirect, $incoming, &$slave) { $stream->write($buffer); $stream->on('close', function () use($incoming, &$slave) { $slave['busy'] = false; $slave['connections']--; $slave['requests']++; $incoming->end(); if ($slave['requests'] > $this->maxRequests) { $info['ready'] = false; $slave['connection']->close(); } if ($slave['closeWhenFree']) { $slave['connection']->close(); } }); $stream->on('data', function ($data) use($incoming) { $incoming->write($data); }); $incoming->on('data', function ($data) use($stream) { $stream->write($data); }); $incoming->on('close', function () use($stream) { $stream->close(); }); }); }); }
if ($request->getPath() === '/') { $response->writeHead('200', array('Content-Type' => 'text/html')); $response->end(file_get_contents(__DIR__ . '/../01-simple-periodic/eventsource.html')); return; } echo 'connected' . PHP_EOL; $headers = $request->getHeaders(); $id = isset($headers['Last-Event-ID']) ? $headers['Last-Event-ID'] : null; $response->writeHead(200, array('Content-Type' => 'text/event-stream')); $channel->connect($response, $id); $response->on('close', function () use($response, $channel) { echo 'disconnected' . PHP_EOL; $channel->disconnect($response); }); }); $port = isset($argv[2]) ? $argv[2] : 8000; $connector = new TcpConnector($loop); $connector->create('127.0.0.1', $port)->then(function (Stream $stream) use($channel) { $buffer = ''; $stream->on('data', function ($data) use(&$buffer, $channel) { $buffer .= $data; while (($pos = strpos($buffer, "\n")) !== false) { $channel->writeMessage(substr($buffer, 0, $pos)); $buffer = substr($buffer, $pos + 1); } }); }, 'printf'); $socket->listen(isset($argv[1]) ? $argv[1] : 0, '0.0.0.0'); echo 'Server now listening on http://localhost:' . $socket->getPort() . ' (port is first parameter)' . PHP_EOL; echo 'Connecting to plain text chat on port ' . $port . ' (port is second parameter)' . PHP_EOL; $loop->run();
/** @test */ public function connectionToHostnameShouldFailImmediately() { $loop = $this->getMock('React\\EventLoop\\LoopInterface'); $connector = new TcpConnector($loop); $connector->create('www.google.com', 80)->then($this->expectCallableNever(), $this->expectCallableOnce()); }