当前位置: 首页>>代码示例>>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;未经允许,请勿转载。