當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Server::getPort方法代碼示例

本文整理匯總了PHP中React\Socket\Server::getPort方法的典型用法代碼示例。如果您正苦於以下問題:PHP Server::getPort方法的具體用法?PHP Server::getPort怎麽用?PHP Server::getPort使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在React\Socket\Server的用法示例。


在下文中一共展示了Server::getPort方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: testGetRemoteAddress

 /**
  * @covers React\Socket\Connection::getRemoteAddress
  */
 public function testGetRemoteAddress()
 {
     $loop = new StreamSelectLoop();
     $server = new Server($loop);
     $server->listen(0);
     $class = new \ReflectionClass('React\\Socket\\Server');
     $master = $class->getProperty('master');
     $master->setAccessible(true);
     $client = stream_socket_client('tcp://localhost:' . $server->getPort());
     $class = new \ReflectionClass('React\\Socket\\Connection');
     $method = $class->getMethod('parseAddress');
     $method->setAccessible(true);
     $servConn = new Connection($server->master, $loop);
     $mock = $this->expectCallableOnceWith($method->invokeArgs($servConn, array(stream_socket_get_name($master->getValue($server), false))));
     $server->on('connection', function ($conn) use($mock) {
         $mock($conn->getRemoteAddress());
     });
     $loop->tick();
 }
開發者ID:reactphp,項目名稱:socket,代碼行數:22,代碼來源:ConnectionTest.php

示例2: Server

// sent for each connection and will print the average throughput once the
// connection closes.
//
// $ php examples/03-benchmark.php 8000
// $ telnet localhost 8000
// $ echo hello world | nc -v localhost 8000
// $ dd if=/dev/zero bs=1M count=1000 | nc -v localhost 8000
use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\ConnectionInterface;
require __DIR__ . '/../vendor/autoload.php';
$loop = Factory::create();
$server = new Server($loop);
$server->listen(isset($argv[1]) ? $argv[1] : 0);
$server->on('connection', function (ConnectionInterface $conn) use($loop) {
    echo '[connected]' . PHP_EOL;
    // count the number of bytes received from this connection
    $bytes = 0;
    $conn->on('data', function ($chunk) use(&$bytes) {
        $bytes += strlen($chunk);
    });
    // report average throughput once client disconnects
    $t = microtime(true);
    $conn->on('close', function () use($conn, $t, &$bytes) {
        $t = microtime(true) - $t;
        echo '[disconnected after receiving ' . $bytes . ' bytes in ' . round($t, 3) . 's => ' . round($bytes / $t / 1024 / 1024, 1) . ' MiB/s]' . PHP_EOL;
    });
});
$server->on('error', 'printf');
echo 'bound to ' . $server->getPort() . PHP_EOL;
$loop->run();
開發者ID:reactphp,項目名稱:socket,代碼行數:31,代碼來源:03-benchmark.php

示例3: Server

use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\ConnectionInterface;
require __DIR__ . '/../vendor/autoload.php';
$loop = Factory::create();
$server = new Server($loop);
$server->listen(isset($argv[1]) ? $argv[1] : 0, '0.0.0.0');
$clients = array();
$server->on('connection', function (ConnectionInterface $client) use(&$clients) {
    // keep a list of all connected clients
    $clients[] = $client;
    $client->on('close', function () use($client, &$clients) {
        unset($clients[array_search($client, $clients)]);
    });
    // whenever a new message comes in
    $client->on('data', function ($data) use($client, &$clients) {
        // remove any non-word characters (just for the demo)
        $data = trim(preg_replace('/[^\\w\\d \\.\\,\\-\\!\\?]/u', '', $data));
        // ignore empty messages
        if ($data === '') {
            return;
        }
        // prefix with client IP and broadcast to all connected clients
        $data = $client->getRemoteAddress() . ': ' . $data . PHP_EOL;
        foreach ($clients as $client) {
            $client->write($data);
        }
    });
});
echo 'Listening on ' . $server->getPort() . PHP_EOL;
$loop->run();
開發者ID:reactphp,項目名稱:socket,代碼行數:31,代碼來源:02-chat-server.php

示例4: Server

<?php

use React\EventLoop\Factory;
use React\Socket\Server;
use React\Http\Request;
use React\Http\Response;
require __DIR__ . '/../vendor/autoload.php';
$loop = Factory::create();
$socket = new Server($loop);
$server = new \React\Http\Server($socket);
$server->on('request', function (Request $request, Response $response) {
    $response->writeHead(200, array('Content-Type' => 'text/plain'));
    $response->end("Hello world!\n");
});
$socket->listen(isset($argv[1]) ? $argv[1] : 0, '0.0.0.0');
echo 'Listening on ' . $socket->getPort() . PHP_EOL;
$loop->run();
開發者ID:reactphp,項目名稱:http,代碼行數:17,代碼來源:01-hello-world.php


注:本文中的React\Socket\Server::getPort方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。