You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
17
vendor/swoole/examples/async/dns_lookup.php
vendored
Executable file
17
vendor/swoole/examples/async/dns_lookup.php
vendored
Executable file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
swoole_async_set(array(
|
||||
//使用纯异步IO
|
||||
'use_async_resolver' => true,
|
||||
'disable_dns_cache' => true,
|
||||
'dns_lookup_random' => true,
|
||||
'dns_server' => '114.114.114.114',
|
||||
));
|
||||
swoole_async_dns_lookup("www.sina.com.cn", function ($host, $ip)
|
||||
{
|
||||
echo "{$host} reslove to {$ip}\n";
|
||||
});
|
||||
|
||||
swoole_async_dns_lookup("www.baidu.com", function ($host, $ip)
|
||||
{
|
||||
echo "{$host} reslove to {$ip}\n";
|
||||
});
|
6
vendor/swoole/examples/async/exec.php
vendored
Executable file
6
vendor/swoole/examples/async/exec.php
vendored
Executable file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
$pid = Swoole\Async::exec("ps aux", function ($result, $status) {
|
||||
var_dump(strlen($result), $status);
|
||||
});
|
||||
|
||||
var_dump($pid);
|
19
vendor/swoole/examples/async/read.php
vendored
Executable file
19
vendor/swoole/examples/async/read.php
vendored
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
//ini_set("swoole.aio_mode", 1);
|
||||
swoole_async_read(
|
||||
__DIR__ . '/data.txt',
|
||||
function ($filename, $content)
|
||||
{
|
||||
echo "file: $filename\ncontent-length: " . strlen($content) . "\nContent: $content\n";
|
||||
if (empty($content))
|
||||
{
|
||||
echo "file is end.\n";
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
},
|
||||
8192
|
||||
);
|
10
vendor/swoole/examples/async/readfile.php
vendored
Executable file
10
vendor/swoole/examples/async/readfile.php
vendored
Executable file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
swoole_async::set(['aio_mode' => SWOOLE_AIO_LINUX]);
|
||||
|
||||
swoole_async_readfile(__DIR__.'/../test.jpg', function($filename, $content){
|
||||
echo "file: $filename\ncontent-length: ".strlen($content)."\nContent:\n";
|
||||
swoole_async_writefile(__DIR__.'/test.copy', $content, function($write_file) {
|
||||
echo "file: $write_file\n";
|
||||
swoole_event_exit();
|
||||
});
|
||||
});
|
12
vendor/swoole/examples/async/write.php
vendored
Executable file
12
vendor/swoole/examples/async/write.php
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
function write_callback($file, $writen)
|
||||
{
|
||||
echo "write $file [$writen]\n";
|
||||
//return true: write contine. return false: close the file.
|
||||
return true;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < 10; $i++)
|
||||
{
|
||||
swoole_async_write("data.txt", str_repeat('A', 10) . "\n", -1, "write_callback");
|
||||
}
|
5
vendor/swoole/examples/async/writefile.php
vendored
Executable file
5
vendor/swoole/examples/async/writefile.php
vendored
Executable file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
use Swoole\Async;
|
||||
|
||||
//Async::set(array('aio_mode' => SWOOLE_AIO_LINUX));
|
||||
Async::writeFile(__DIR__.'/data2.txt', str_repeat('C', 1023)."\n", null, FILE_APPEND);
|
8
vendor/swoole/examples/atomic/long.php
vendored
Executable file
8
vendor/swoole/examples/atomic/long.php
vendored
Executable file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
$l = new Swoole\Atomic\Long( -2 ** 36);
|
||||
echo $l->get()."\n";
|
||||
echo $l->add(20)."\n";
|
||||
echo $l->sub(20)."\n";
|
||||
echo $l->sub(-20)."\n";
|
||||
echo $l->cmpset(-2 ** 36, 0)."\n";
|
||||
echo $l->cmpset(-2 ** 36 + 20, 0)."\n";
|
7
vendor/swoole/examples/atomic/test.php
vendored
Executable file
7
vendor/swoole/examples/atomic/test.php
vendored
Executable file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
$atomic = new swoole_atomic(123);
|
||||
echo $atomic->add(12)."\n";
|
||||
echo $atomic->sub(11)."\n";
|
||||
echo $atomic->cmpset(122, 999)."\n";
|
||||
echo $atomic->cmpset(124, 999)."\n";
|
||||
echo $atomic->get()."\n";
|
16
vendor/swoole/examples/atomic/wait.php
vendored
Executable file
16
vendor/swoole/examples/atomic/wait.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
$n = new swoole_atomic(0);
|
||||
|
||||
if (pcntl_fork() > 0)
|
||||
{
|
||||
echo "master start\n";
|
||||
$n->wait(1.5);
|
||||
echo "master end\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "child start\n";
|
||||
sleep(1);
|
||||
$n->wakeup();
|
||||
echo "child end\n";
|
||||
}
|
15
vendor/swoole/examples/buffer.php
vendored
Executable file
15
vendor/swoole/examples/buffer.php
vendored
Executable file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
$buffer = new swoole_buffer;
|
||||
$buffer->append(str_repeat("A", 10));
|
||||
$buffer->append(str_repeat("B", 20));
|
||||
$buffer->append(str_repeat("C", 30));
|
||||
|
||||
var_dump($buffer);
|
||||
echo $buffer->substr(0, 10, true)."\n";
|
||||
echo $buffer->substr(0, 20, true)."\n";
|
||||
echo $buffer->substr(0, 30)."\n";
|
||||
$buffer->clear();
|
||||
|
||||
echo $buffer->substr(0, 10, true)."\n";
|
||||
var_dump($buffer);
|
||||
sleep(1);
|
29
vendor/swoole/examples/c10k.php
vendored
Executable file
29
vendor/swoole/examples/c10k.php
vendored
Executable file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
$clients = array();
|
||||
for($j = 0; $j < 2; $j++)
|
||||
{
|
||||
$pid = pcntl_fork();
|
||||
if($pid > 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
for($i = 0; $i < 9999; $i++){
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); //同步阻塞
|
||||
$ret = $client->connect('127.0.0.1', 9501, 0.5);
|
||||
if(!$ret)
|
||||
{
|
||||
echo "#$i\tConnect fail. errno=".$client->errCode;
|
||||
die("\n");
|
||||
}
|
||||
$clients[] = $client;
|
||||
usleep(10);
|
||||
}
|
||||
echo "Worker #".posix_getpid()." connect $i finish\n";
|
||||
sleep(1000);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
sleep(1000);
|
||||
|
45
vendor/swoole/examples/channel.php
vendored
Executable file
45
vendor/swoole/examples/channel.php
vendored
Executable file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
$chan = new Swoole\Channel(1024 * 256);
|
||||
$n = 100000;
|
||||
$bytes = 0;
|
||||
|
||||
if (pcntl_fork() > 0)
|
||||
{
|
||||
echo "Father\n";
|
||||
for ($i = 0; $i < $n; $i++)
|
||||
{
|
||||
$data = str_repeat('A', rand(100, 200));
|
||||
if ($chan->push($data) === false)
|
||||
{
|
||||
echo "channel full\n";
|
||||
usleep(1000);
|
||||
$i--;
|
||||
continue;
|
||||
}
|
||||
$bytes += strlen($data);
|
||||
// echo "#$i\tpush ".strlen($data)." bytes\n";
|
||||
}
|
||||
|
||||
echo "total push bytes: $bytes\n";
|
||||
var_dump($chan->stats());
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "Child\n";
|
||||
for ($i = 0; $i < $n; $i++)
|
||||
{
|
||||
$data = $chan->pop();
|
||||
if ($data === false)
|
||||
{
|
||||
echo "channel empty\n";
|
||||
usleep(1000);
|
||||
$i--;
|
||||
continue;
|
||||
}
|
||||
$bytes += strlen($data);
|
||||
// echo "#$i\tpop " . strlen($data) . " bytes\n";
|
||||
}
|
||||
echo "total pop bytes: $bytes\n";
|
||||
var_dump($chan->stats());
|
||||
}
|
||||
|
35
vendor/swoole/examples/channel/leader_follower.php
vendored
Executable file
35
vendor/swoole/examples/channel/leader_follower.php
vendored
Executable file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
use Swoole\Timer;
|
||||
use Swoole\Process;
|
||||
use Swoole\Channel;
|
||||
|
||||
$chan = new Channel(1024 * 256);
|
||||
|
||||
$worker_num = 4;
|
||||
$workers = array();
|
||||
|
||||
for ($i = 0; $i < $worker_num; $i++)
|
||||
{
|
||||
$process = new Process(function ($worker) use ($chan, $i)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
$data = $chan->pop();
|
||||
if (empty($data))
|
||||
{
|
||||
usleep(200000);
|
||||
continue;
|
||||
}
|
||||
echo "worker#$i\t$data\n";
|
||||
}
|
||||
}, false);
|
||||
$process->id = $i;
|
||||
$pid = $process->start();
|
||||
$workers[$pid] = $process;
|
||||
}
|
||||
|
||||
Timer::tick(2000, function () use ($chan)
|
||||
{
|
||||
static $index = 0;
|
||||
$chan->push("hello-" . $index++);
|
||||
});
|
53
vendor/swoole/examples/client/async.php
vendored
Executable file
53
vendor/swoole/examples/client/async.php
vendored
Executable file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞
|
||||
//$client->set(array(
|
||||
// 'socket_buffer_size' => 1024 * 1024 * 2,
|
||||
// 'open_eof_check' => true,
|
||||
// 'package_eof' => "\r\n\r\n",
|
||||
//));
|
||||
|
||||
$client->_count = 0;
|
||||
$client->on("connect", function(swoole_client $cli) {
|
||||
//swoole_timer_clear($cli->timer);
|
||||
$cli->send("GET / HTTP/1.1\r\n\r\n");
|
||||
//$cli->sendfile(__DIR__.'/test.txt');
|
||||
//$cli->_count = 0;
|
||||
});
|
||||
|
||||
$client->on("receive", function(swoole_client $cli, $data){
|
||||
echo "Receive: $data";
|
||||
$cli->_count++;
|
||||
if ($cli->_count > 5)
|
||||
{
|
||||
//睡眠模式,不再接收新的数据
|
||||
echo "count=10, sleep(5000ms)\n";
|
||||
$cli->sleep();
|
||||
$cli->_count = 0;
|
||||
swoole_timer_after(5000, function() use ($cli) {
|
||||
//唤醒
|
||||
$cli->wakeup();
|
||||
});
|
||||
//$cli->close();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
$cli->send(str_repeat('A', 100)."\n");
|
||||
}
|
||||
});
|
||||
|
||||
$client->on("error", function(swoole_client $cli){
|
||||
echo "error\n";
|
||||
});
|
||||
|
||||
$client->on("close", function(swoole_client $cli){
|
||||
echo "Connection close\n";
|
||||
});
|
||||
|
||||
$client->connect('127.0.0.1', 9501);
|
||||
//$client->timer = swoole_timer_after(1000, function () use ($client) {
|
||||
// echo "socket timeout\n";
|
||||
// $client->close();
|
||||
//});
|
||||
|
||||
//echo "connect to 127.0.0.1:9501\n";
|
34
vendor/swoole/examples/client/get_socket.php
vendored
Executable file
34
vendor/swoole/examples/client/get_socket.php
vendored
Executable file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
function getClient()
|
||||
{
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP);
|
||||
if (!$client->connect('127.0.0.1', 9501, -1))
|
||||
{
|
||||
exit("connect failed. Error: {$client->errCode}\n");
|
||||
}
|
||||
|
||||
$res = $client->getSocket();
|
||||
return $client;
|
||||
}
|
||||
|
||||
$client = getClient();
|
||||
|
||||
$count = 0;
|
||||
//$client->set(array('open_eof_check' => true, 'package_eof' => "\r\n\r\n"));
|
||||
|
||||
//$client = new swoole_client(SWOOLE_SOCK_UNIX_DGRAM, SWOOLE_SOCK_SYNC); //同步阻塞
|
||||
//if (!$client->connect(dirname(__DIR__).'/server/svr.sock', 0, -1, 1))
|
||||
|
||||
|
||||
var_dump($client->getsockname());
|
||||
$client->send("hello world\r\n\r\n");
|
||||
|
||||
//for($i=0; $i < 3; $i ++)
|
||||
{
|
||||
echo $client->recv();
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
$client->close();
|
||||
|
18
vendor/swoole/examples/client/long_tcp.php
vendored
Executable file
18
vendor/swoole/examples/client/long_tcp.php
vendored
Executable file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
for($i=0; $i < 100; $i++)
|
||||
{
|
||||
$client = new swoole_client(SWOOLE_TCP | SWOOLE_KEEP);
|
||||
if(!$client->connect('127.0.0.1', 9501))
|
||||
{
|
||||
exit("connect failed\n");
|
||||
}
|
||||
$client->send(str_repeat("A", 600));
|
||||
$data = $client->recv(7000, 0);
|
||||
if($data === false)
|
||||
{
|
||||
echo "recv fail\n";
|
||||
break;
|
||||
}
|
||||
var_dump($data);
|
||||
$client->close();
|
||||
}
|
32
vendor/swoole/examples/client/select.php
vendored
Executable file
32
vendor/swoole/examples/client/select.php
vendored
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
$clients = array();
|
||||
|
||||
for($i=0; $i< 20; $i++)
|
||||
{
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); //同步阻塞
|
||||
$ret = $client->connect('127.0.0.1', 9501, 0.5, 0);
|
||||
if(!$ret)
|
||||
{
|
||||
echo "Connect Server fail.errCode=".$client->errCode;
|
||||
}
|
||||
else
|
||||
{
|
||||
$client->send("HELLO WORLD\n");
|
||||
$clients[$client->sock] = $client;
|
||||
}
|
||||
}
|
||||
|
||||
while (!empty($clients))
|
||||
{
|
||||
$write = $error = array();
|
||||
$read = array_values($clients);
|
||||
$n = swoole_client_select($read, $write, $error, 0.6);
|
||||
if ($n > 0)
|
||||
{
|
||||
foreach ($read as $index => $c)
|
||||
{
|
||||
echo "Recv #{$c->sock}: " . $c->recv() . "\n";
|
||||
unset($clients[$c->sock]);
|
||||
}
|
||||
}
|
||||
}
|
30
vendor/swoole/examples/client/sync.php
vendored
Executable file
30
vendor/swoole/examples/client/sync.php
vendored
Executable file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP);
|
||||
$count = 0;
|
||||
//$client->set(array('open_eof_check' => true, 'package_eof' => "\r\n\r\n"));
|
||||
|
||||
//$client = new swoole_client(SWOOLE_SOCK_UNIX_DGRAM, SWOOLE_SOCK_SYNC); //同步阻塞
|
||||
//if (!$client->connect(dirname(__DIR__).'/server/svr.sock', 0, -1, 1))
|
||||
|
||||
do_connect:
|
||||
if (!$client->connect('127.0.0.1', 9501, -1))
|
||||
{
|
||||
exit("connect failed. Error: {$client->errCode}\n");
|
||||
}
|
||||
|
||||
var_dump($client->getsockname());
|
||||
$client->send("hello world\r\n\r\n");
|
||||
|
||||
//for($i=0; $i < 3; $i ++)
|
||||
{
|
||||
echo $client->recv();
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
$client->close();
|
||||
$count++;
|
||||
if ($count < 20)
|
||||
{
|
||||
goto do_connect;
|
||||
}
|
||||
|
10
vendor/swoole/examples/client/test.txt
vendored
Executable file
10
vendor/swoole/examples/client/test.txt
vendored
Executable file
@ -0,0 +1,10 @@
|
||||
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
|
||||
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
|
||||
XXXXXXXXXXXXXXXXXXXXXXx
|
||||
XXXXXXXXXXXXXXXXXXXXX
|
||||
XXXXXXXXXXXXXXXXXXXXXXX
|
||||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
||||
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
|
||||
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
|
||||
DDDDDDDDDDDDDDDDDDDDDDDDDDD
|
||||
|
23
vendor/swoole/examples/client/udp_async.php
vendored
Executable file
23
vendor/swoole/examples/client/udp_async.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
$client = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC); //异步非阻塞
|
||||
|
||||
$client->on("connect", function(swoole_client $cli) {
|
||||
echo "connected\n";
|
||||
$cli->send("hello world\n");
|
||||
});
|
||||
|
||||
$client->on('close', function($cli){
|
||||
echo "closed\n";
|
||||
});
|
||||
|
||||
$client->on('error', function($cli){
|
||||
echo "error\n";
|
||||
});
|
||||
|
||||
$client->on("receive", function(swoole_client $cli, $data){
|
||||
echo "received: $data\n";
|
||||
sleep(1);
|
||||
$cli->send("hello_".rand(1000,9999));
|
||||
});
|
||||
|
||||
$client->connect('127.0.0.1', 9502, 0.5);
|
12
vendor/swoole/examples/client/udp_sync.php
vendored
Executable file
12
vendor/swoole/examples/client/udp_sync.php
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
$client = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_SYNC);
|
||||
$client->connect('127.0.0.1', 9502);
|
||||
|
||||
for ($i = 0; $i < 100; $i++)
|
||||
{
|
||||
$client->send("admin");
|
||||
echo $client->recv()."\n";
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
|
21
vendor/swoole/examples/client2.php
vendored
Executable file
21
vendor/swoole/examples/client2.php
vendored
Executable file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
$clients = array();
|
||||
for($i = 0; $i < 1; $i++){
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); //同步阻塞
|
||||
$ret = $client->connect('127.0.0.1', 9501, 0.5, 0);
|
||||
if(!$ret)
|
||||
{
|
||||
echo "Over flow. errno=".$client->errCode;
|
||||
die("\n");
|
||||
}
|
||||
$clients[] = $client;
|
||||
}
|
||||
sleep(1);
|
||||
while (1) {
|
||||
foreach ($clients as $client) {
|
||||
$client->send("sss");
|
||||
$data = $client->recv();
|
||||
var_dump($data);
|
||||
}
|
||||
sleep(1);
|
||||
}
|
117
vendor/swoole/examples/coroutine/TestHttpServ.php
vendored
Executable file
117
vendor/swoole/examples/coroutine/TestHttpServ.php
vendored
Executable file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
/**
|
||||
* @Author: winterswang
|
||||
* @Date: 2015-06-18 16:45:09
|
||||
* @Last Modified by: winterswang
|
||||
* @Last Modified time: 2016-09-18 17:33:51
|
||||
*/
|
||||
|
||||
class TestHttpServer {
|
||||
|
||||
public $http;
|
||||
public $queue;
|
||||
public $setting = array();
|
||||
|
||||
/**
|
||||
* [__construct description]
|
||||
* @param array $setting [description]
|
||||
*/
|
||||
public function __construct(){
|
||||
|
||||
}
|
||||
|
||||
public function set($setting){
|
||||
|
||||
$this ->setting = $setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* [init description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function init(){
|
||||
|
||||
if (!isset($this ->setting['host'])) {
|
||||
$this ->setting['host'] = '0.0.0.0';
|
||||
}
|
||||
if (!isset($this ->setting['port'])) {
|
||||
$this ->setting['port'] = '9999';
|
||||
}
|
||||
|
||||
$this ->http = new swoole_http_server($this ->setting['host'], $this ->setting['port']);
|
||||
$this ->http ->set($this ->setting);
|
||||
|
||||
$this ->http ->on('request', array($this, 'onRequest'));
|
||||
$this ->http ->on('close', array($this, 'onClose'));
|
||||
}
|
||||
|
||||
/**
|
||||
* [onRequest description]
|
||||
* @param [type] $request [description]
|
||||
* @param [type] $response [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function onRequest($request, $response){
|
||||
|
||||
// $udp = new swoole_client(SWOOLE_SOCK_UDP, SWOOLE_SOCK_ASYNC);
|
||||
// $udp->on("connect", function(swoole_client $cli) {
|
||||
// $cli->send("udp test");
|
||||
// });
|
||||
// $udp->on("receive", function(swoole_client $cli, $data)use($response){
|
||||
|
||||
$tcp = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
|
||||
$tcp->on("connect", function(swoole_client $cli) {
|
||||
$cli->send("tcp test");
|
||||
});
|
||||
$tcp->on("receive", function(swoole_client $cli, $data)use($response){
|
||||
$response ->end("<h1> swoole response</h1>");
|
||||
});
|
||||
$tcp->on("close", function(swoole_client $cli){
|
||||
});
|
||||
$tcp->on("error", function(swoole_client $cli){
|
||||
});
|
||||
$tcp->connect('10.100.64.151', 9805);
|
||||
|
||||
// });
|
||||
// $udp->on("close", function(swoole_client $cli){
|
||||
// });
|
||||
// $udp->connect('10.100.65.222', 9906);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* [onClose description]
|
||||
* @param [type] $server [description]
|
||||
* @param [type] $fd [description]
|
||||
* @param [type] $from_id [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function onClose($server, $fd, $from_id){
|
||||
|
||||
//echo " on close fd = $fd from_id = $from_id \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* [start description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function start(){
|
||||
|
||||
$this ->init();
|
||||
$this ->http ->start();
|
||||
}
|
||||
}
|
||||
|
||||
$setting = array(
|
||||
'host' => '0.0.0.0',
|
||||
'port' => 10005,
|
||||
'worker_num' => 4,
|
||||
'dispatch_mode' => 2, //固定分配请求到worker
|
||||
'reactor_num' => 4, //亲核
|
||||
'daemonize' => 1, //守护进程
|
||||
'backlog' => 128,
|
||||
'log_file' => '/data/log/test_http_server.log',
|
||||
);
|
||||
$th = new TestHttpServer();
|
||||
$th ->set($setting);
|
||||
$th ->start();
|
27
vendor/swoole/examples/coroutine/client_send_yield.php
vendored
Executable file
27
vendor/swoole/examples/coroutine/client_send_yield.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$client->set(array(
|
||||
'socket_buffer_size' => 1024 * 512,
|
||||
));
|
||||
if (!$client->connect('127.0.0.1', 9501, -1))
|
||||
{
|
||||
exit("connect failed. Error: {$client->errCode}\n");
|
||||
}
|
||||
$length = 0;
|
||||
$size = 1024 * 64;
|
||||
while (true)
|
||||
{
|
||||
$ret = $client->send(str_repeat('A', $size));
|
||||
if ($ret == false)
|
||||
{
|
||||
var_dump($ret);
|
||||
break;
|
||||
}
|
||||
$length += $size;
|
||||
echo "send $length success\n";
|
||||
}
|
||||
var_dump($client->errCode);
|
||||
});
|
||||
|
||||
swoole_event_wait();
|
27
vendor/swoole/examples/coroutine/client_send_yield_server.php
vendored
Executable file
27
vendor/swoole/examples/coroutine/client_send_yield_server.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
$socket = stream_socket_server("tcp://0.0.0.0:9501", $errno, $errstr);
|
||||
if (!$socket) {
|
||||
echo "$errstr ($errno)<br />\n";
|
||||
} else {
|
||||
while (true) {
|
||||
$conn = stream_socket_accept($socket);
|
||||
if (!$conn) {
|
||||
continue;
|
||||
}
|
||||
$i = 0;
|
||||
$length = 0;
|
||||
while(true) {
|
||||
$data = fread($conn, 8192);
|
||||
if ($data == false)
|
||||
{
|
||||
break;
|
||||
}
|
||||
$length += strlen($data);
|
||||
echo "recv " . $length . " bytes\n";
|
||||
usleep(100000);
|
||||
}
|
||||
fclose($conn);
|
||||
echo "closed\n";
|
||||
}
|
||||
fclose($socket);
|
||||
}
|
19
vendor/swoole/examples/coroutine/coro_array_map.php
vendored
Executable file
19
vendor/swoole/examples/coroutine/coro_array_map.php
vendored
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function() {
|
||||
array_map("test",array("func param\n"));
|
||||
echo "co flow end\n";
|
||||
});
|
||||
|
||||
function test($p) {
|
||||
go(function() use ($p){
|
||||
echo $p;
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
echo "co resume : connect ret = ".var_export($res,1)."\n";
|
||||
echo "map func end \n";
|
||||
});
|
||||
}
|
||||
echo "main end\n";
|
20
vendor/swoole/examples/coroutine/coro_call_user.php
vendored
Executable file
20
vendor/swoole/examples/coroutine/coro_call_user.php
vendored
Executable file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
co::set(['trace_flags' => 1]);
|
||||
|
||||
co::create(function() {
|
||||
echo "co func start\n";
|
||||
$name = "call_user_func";
|
||||
$ret = $name("test","test\n");
|
||||
echo "co func end ret:{$ret}\n";
|
||||
});
|
||||
|
||||
function test($params)
|
||||
{
|
||||
echo "func params:$params";
|
||||
co::sleep(1);
|
||||
echo "func end\n";
|
||||
return "test return\n";
|
||||
}
|
||||
echo "main script last\n";
|
||||
|
25
vendor/swoole/examples/coroutine/coro_channel.php
vendored
Executable file
25
vendor/swoole/examples/coroutine/coro_channel.php
vendored
Executable file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
$http = new swoole_http_server("127.0.0.1", 9501, SWOOLE_BASE);
|
||||
$http->set(array(
|
||||
'log_file' => '/dev/null'
|
||||
));
|
||||
use Swoole\Coroutine as co;
|
||||
// $http->on("WorkerStart", function (\swoole_server $serv)
|
||||
// {
|
||||
//
|
||||
// });
|
||||
$http->on('request', function (swoole_http_request $request, swoole_http_response $response)
|
||||
{
|
||||
$ch = new co\Channel(1);
|
||||
$out = new co\Channel(1);
|
||||
Swoole\Coroutine::create(function() use ($ch, $out) {
|
||||
$out->push("OK");
|
||||
$out->push("OK");
|
||||
});
|
||||
$ret = $out->pop();
|
||||
var_dump($ret);
|
||||
$ret = $out->pop();
|
||||
var_dump($ret);
|
||||
$response->end("$ret\n");
|
||||
});
|
||||
$http->start();
|
28
vendor/swoole/examples/coroutine/coro_destruct.php
vendored
Executable file
28
vendor/swoole/examples/coroutine/coro_destruct.php
vendored
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
class T
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
function test()
|
||||
{
|
||||
echo "call function \n";
|
||||
}
|
||||
|
||||
function __destruct()
|
||||
{
|
||||
go(function () {
|
||||
echo "coro start\n";
|
||||
co::sleep(1.0);
|
||||
echo "coro exit\n";
|
||||
});
|
||||
echo "111\n";
|
||||
}
|
||||
}
|
||||
|
||||
$t = new T();
|
||||
$t->test();
|
||||
echo "end \n";
|
32
vendor/swoole/examples/coroutine/coro_destuct.php
vendored
Executable file
32
vendor/swoole/examples/coroutine/coro_destuct.php
vendored
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
|
||||
class T
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
echo "call __construct \n";
|
||||
}
|
||||
|
||||
function test()
|
||||
{
|
||||
echo "call function \n";
|
||||
}
|
||||
|
||||
function __destruct()
|
||||
{
|
||||
echo "call __destruct \n";
|
||||
go(function () {
|
||||
echo "co[1] start\n";
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
co::sleep(1.0);
|
||||
echo "co[1] resume : connect ret = ".var_export($res,1)."\n";
|
||||
echo "co[1] exit\n";
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
$t = new T();
|
||||
$t->test();
|
||||
unset($t);
|
9
vendor/swoole/examples/coroutine/coro_empty.php
vendored
Executable file
9
vendor/swoole/examples/coroutine/coro_empty.php
vendored
Executable file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
// co::set(['trace_flags' => 1]);
|
||||
|
||||
co::create(function () {
|
||||
echo "no coro exit\n";
|
||||
});
|
||||
echo "exec file end\n";
|
||||
|
11
vendor/swoole/examples/coroutine/coro_gethost.php
vendored
Executable file
11
vendor/swoole/examples/coroutine/coro_gethost.php
vendored
Executable file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function () {
|
||||
$ip = co::gethostbyname('www.baidu.com');
|
||||
var_dump($ip);
|
||||
});
|
||||
echo "111\n";
|
||||
|
||||
echo "222\n";
|
9
vendor/swoole/examples/coroutine/coro_include.php
vendored
Executable file
9
vendor/swoole/examples/coroutine/coro_include.php
vendored
Executable file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
if (substr(PHP_OS, 0, 3) == 'WIN')
|
||||
{
|
||||
exit("skip for Windows");
|
||||
}
|
||||
if (!extension_loaded("swoole"))
|
||||
{
|
||||
exit("swoole extension is required");
|
||||
}
|
22
vendor/swoole/examples/coroutine/coro_invoke.php
vendored
Executable file
22
vendor/swoole/examples/coroutine/coro_invoke.php
vendored
Executable file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
co::set(['trace_flags' => 1]);
|
||||
|
||||
co::create(function() {
|
||||
|
||||
|
||||
$function = new ReflectionFunction('title');
|
||||
|
||||
$function->invoke();
|
||||
echo "invoke444\n";
|
||||
|
||||
});
|
||||
|
||||
function title() {
|
||||
echo "333invoke_________________________________\n";
|
||||
$tcpclient = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
var_dump($tcpclient->connect('127.0.0.1', 9501, 1));
|
||||
|
||||
}
|
||||
|
||||
echo "111\n";
|
18
vendor/swoole/examples/coroutine/coro_nested.php
vendored
Executable file
18
vendor/swoole/examples/coroutine/coro_nested.php
vendored
Executable file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
echo "before coro\n";
|
||||
go(function () {
|
||||
echo "co[1] start\n";
|
||||
go(function () {
|
||||
echo "co[2] start\n";
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
echo "co[2] resume : connect ret = ".var_export($res,1)."\n";
|
||||
echo "co[2] exit\n";
|
||||
});
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
echo "co[1] resume : connect ret = ".var_export($res,1)."\n";
|
||||
echo "co[1] exit\n";
|
||||
});
|
||||
echo "out coro \n";
|
16
vendor/swoole/examples/coroutine/coro_nested_empty.php
vendored
Executable file
16
vendor/swoole/examples/coroutine/coro_nested_empty.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
function test()
|
||||
{
|
||||
echo "before coro\n";
|
||||
go(function () {
|
||||
echo "co[1] start\n";
|
||||
go(function () {
|
||||
echo "co[2] start\n";
|
||||
echo "co[2] exit\n";
|
||||
});
|
||||
echo "co[1] exit\n";
|
||||
});
|
||||
echo "func end \n";
|
||||
}
|
||||
test();
|
39
vendor/swoole/examples/coroutine/coro_serialize.php
vendored
Executable file
39
vendor/swoole/examples/coroutine/coro_serialize.php
vendored
Executable file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
class Obj {
|
||||
public $a;
|
||||
protected $b;
|
||||
private $c;
|
||||
var $d;
|
||||
|
||||
function __construct($a, $b, $c, $d) {
|
||||
$this->a = $a;
|
||||
$this->b = $b;
|
||||
$this->c = $c;
|
||||
$this->d = $d;
|
||||
}
|
||||
|
||||
function __sleep() {
|
||||
// co::sleep(0.5);
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 10);
|
||||
var_dump($res);
|
||||
if ($res)
|
||||
{
|
||||
echo("connect success. Error: {$client->errCode}\n");
|
||||
}
|
||||
echo "sleep\n";
|
||||
return array('a', 'b', 'c');
|
||||
}
|
||||
|
||||
// function __wakeup() {
|
||||
// $this->d = $this->a + $this->b + $this->c;
|
||||
// }
|
||||
}
|
||||
$o = new Obj(1, 2, 3, 4);
|
||||
co::create(function() use($o) {
|
||||
$serialized = serialize($o);
|
||||
$unserialized = unserialize($serialized);
|
||||
echo "res:".var_export($unserialized,1)."\n";
|
||||
echo "call user\n";
|
||||
});
|
9
vendor/swoole/examples/coroutine/coro_sleep.php
vendored
Executable file
9
vendor/swoole/examples/coroutine/coro_sleep.php
vendored
Executable file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
// require __DIR__ . "/coro_include.php";
|
||||
use Swoole\Coroutine as co;
|
||||
co::create(function () {
|
||||
echo "start\n";
|
||||
co::sleep(0.5);
|
||||
echo "OK\n";
|
||||
});
|
||||
echo "11\n";
|
19
vendor/swoole/examples/coroutine/coro_stackless.php
vendored
Executable file
19
vendor/swoole/examples/coroutine/coro_stackless.php
vendored
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
require __DIR__ . "/coro_include.php";
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
echo "start\n";
|
||||
co::create(function () {
|
||||
$client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$res = $client->connect('127.0.0.1', 9501, 1);
|
||||
var_dump($res);
|
||||
if ($res) {
|
||||
echo ("connect success. Error: {$client->errCode}\n");
|
||||
}
|
||||
|
||||
$res = $client->send("hello");
|
||||
echo "send res:" . var_export($res, 1) . "\n";
|
||||
$data = $client->recv();
|
||||
echo "recv data" . var_export($data, 1) . "\n";
|
||||
});
|
||||
echo "end\n";
|
16
vendor/swoole/examples/coroutine/coro_util.php
vendored
Executable file
16
vendor/swoole/examples/coroutine/coro_util.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
$id = go(function(){
|
||||
$id = co::getUid();
|
||||
echo "start coro $id\n";
|
||||
co::suspend($id);
|
||||
echo "resume coro $id @1\n";
|
||||
co::suspend($id);
|
||||
echo "resume coro $id @2\n";
|
||||
});
|
||||
echo "start to resume $id @1\n";
|
||||
co::resume($id);
|
||||
echo "start to resume $id @2\n";
|
||||
co::resume($id);
|
||||
echo "main\n";
|
79
vendor/swoole/examples/coroutine/defer_client.php
vendored
Executable file
79
vendor/swoole/examples/coroutine/defer_client.php
vendored
Executable file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/* new multi implement test */
|
||||
$server = new Swoole\Http\Server("127.0.0.1", 9502, SWOOLE_BASE);
|
||||
|
||||
$server->set([
|
||||
'worker_num' => 1,
|
||||
]);
|
||||
|
||||
$server->on('Request', function ($request, $response) {
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$redis->setDefer(true);
|
||||
$redis->get('key');
|
||||
$res = $redis->get('key');//get false
|
||||
var_dump($res);
|
||||
|
||||
var_dump($redis->setDefer());//get true
|
||||
var_dump($redis->setDefer(false));//get false
|
||||
|
||||
//穿插其他client也能正常工作
|
||||
$redis_tmp = new Swoole\Coroutine\Redis();
|
||||
$res = $redis_tmp->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $redis_tmp->set('key_tmp', 'HaHa');//get true
|
||||
var_dump($res);
|
||||
|
||||
|
||||
$http_client= new Swoole\Coroutine\Http\Client('km.oa.com', 80);
|
||||
$http_client->setDefer();
|
||||
$http_client->get('/');
|
||||
|
||||
$mysql = new Swoole\Coroutine\MySQL();
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$mysql->setDefer(true);
|
||||
$mysql->query('select sleep(1)', 2);
|
||||
|
||||
$udp = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
|
||||
$res = $udp->connect("127.0.0.1", 9906, 2);
|
||||
$udp->send('Hello World!');
|
||||
|
||||
//穿插其他client也能正常工作
|
||||
$udp_tmp = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
|
||||
$res = $udp_tmp->connect("127.0.0.1", 9909, 2);//nonexistent server
|
||||
$res = $udp_tmp->recv();//get false with timeout
|
||||
var_dump($res);
|
||||
|
||||
$udp_res = $udp->recv();
|
||||
$res = $mysql->query('select sleep(1)', 2);//get false
|
||||
var_dump($res);
|
||||
$res = $mysql->setDefer(false);
|
||||
var_dump($res);//get false
|
||||
$res = $mysql->setDefer();
|
||||
var_dump($res);//get true
|
||||
$mysql_res = $mysql->recv();
|
||||
$res = $redis->get('key');//get false
|
||||
var_dump($res);
|
||||
$redis_res = $redis->recv();
|
||||
$res = $http_client->get('/');
|
||||
var_dump($res);//get false
|
||||
$res = $http_client->recv();
|
||||
var_dump($res);//get true
|
||||
|
||||
var_dump($udp_res, $mysql_res, $redis_res, $http_client);
|
||||
var_dump($http_client->setDefer(false));
|
||||
var_dump($mysql->getDefer(), $redis->getDefer(), $http_client->getDefer());
|
||||
$response->end('Test End');
|
||||
});
|
||||
$server->start();
|
27
vendor/swoole/examples/coroutine/enable_coroutine.php
vendored
Executable file
27
vendor/swoole/examples/coroutine/enable_coroutine.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
|
||||
$http = new swoole_http_server('127.0.0.1', 9501);
|
||||
|
||||
$http->set([
|
||||
'enable_coroutine' => false, // close build-in coroutine
|
||||
]);
|
||||
|
||||
$http->on('workerStart', function () {
|
||||
echo "Coroutine is " . (Co::getuid() > 0 ? 'enable' : 'disable')."\n";
|
||||
});
|
||||
|
||||
$http->on("request", function (Request $request, Response $response) {
|
||||
$response->header("Content-Type", "text/plain");
|
||||
if ($request->server['request_uri'] == '/co') {
|
||||
go(function () use ($response) {
|
||||
$response->end("Hello Coroutine #" . Co::getuid());
|
||||
});
|
||||
} else {
|
||||
$response->end("Hello Swoole #" . Co::getuid());
|
||||
}
|
||||
});
|
||||
|
||||
$http->start();
|
16
vendor/swoole/examples/coroutine/exception/empty.php
vendored
Executable file
16
vendor/swoole/examples/coroutine/exception/empty.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
go(function () {
|
||||
try {
|
||||
echo "before\n";
|
||||
co::sleep(0.5);
|
||||
echo "after\n";
|
||||
throw new Exception('coro Exception.');
|
||||
} catch (Exception $e) {
|
||||
echo 'Caught exception: ', $e->getMessage(), "\n";
|
||||
} finally {
|
||||
echo "First finally.\n";
|
||||
}
|
||||
});
|
||||
echo "exec file end\n";
|
||||
|
||||
|
20
vendor/swoole/examples/coroutine/fgets.php
vendored
Executable file
20
vendor/swoole/examples/coroutine/fgets.php
vendored
Executable file
@ -0,0 +1,20 @@
|
||||
<?Php
|
||||
$fp = fopen(__DIR__ . "/defer_client.php", "r");
|
||||
stream_set_chunk_size($fp, 1024);
|
||||
|
||||
go(function () use ($fp)
|
||||
{
|
||||
for($i = 0; $i<100;$i++) {
|
||||
$r = co::fgets($fp);
|
||||
if (empty($r) and feof($fp))
|
||||
{
|
||||
//echo "EOF\n";
|
||||
break;
|
||||
}
|
||||
//echo "len=".strlen($r)."\n";
|
||||
echo $r;
|
||||
//echo "---------------------------------------\n";
|
||||
//var_dump($r);
|
||||
//co::sleep(1);
|
||||
}
|
||||
});
|
11
vendor/swoole/examples/coroutine/fread.php
vendored
Executable file
11
vendor/swoole/examples/coroutine/fread.php
vendored
Executable file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
$fp = fopen(__DIR__ . "/defer_client.php", "r");
|
||||
|
||||
co::create(function () use ($fp)
|
||||
{
|
||||
fseek($fp, 256);
|
||||
$r = co::fread($fp);
|
||||
var_dump($r);
|
||||
});
|
10
vendor/swoole/examples/coroutine/fwrite.php
vendored
Executable file
10
vendor/swoole/examples/coroutine/fwrite.php
vendored
Executable file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
$fp = fopen(__DIR__ . "/test.data", "a+");
|
||||
|
||||
co::create(function () use ($fp)
|
||||
{
|
||||
$r = co::fwrite($fp, "hello world\n", 5);
|
||||
var_dump($r);
|
||||
});
|
8
vendor/swoole/examples/coroutine/gethostbyname.php
vendored
Executable file
8
vendor/swoole/examples/coroutine/gethostbyname.php
vendored
Executable file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function() {
|
||||
$ip = co::gethostbyname("www.baidu.com");
|
||||
echo "IP: $ip\n";
|
||||
});
|
||||
|
71
vendor/swoole/examples/coroutine/http2_client.php
vendored
Executable file
71
vendor/swoole/examples/coroutine/http2_client.php
vendored
Executable file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
//const TEST = array('get', 'post', 'pipeline');
|
||||
const TEST = array('pipeline');
|
||||
|
||||
co::create(function () use ($fp)
|
||||
{
|
||||
$cli = new co\Http2\Client('127.0.0.1', 9518);
|
||||
|
||||
$cli->set([ 'timeout' => 1]);
|
||||
var_dump($cli->connect());
|
||||
|
||||
if (in_array('get', TEST))
|
||||
{
|
||||
$req = new co\Http2\Request;
|
||||
$req->path = "/index.html";
|
||||
$req->headers = [
|
||||
'host' => "localhost",
|
||||
"user-agent" => 'Chrome/49.0.2587.3',
|
||||
'accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'accept-encoding' => 'gzip',
|
||||
];
|
||||
$req->cookies = ['name' => 'rango', 'email' => '1234@qq.com'];
|
||||
var_dump($cli->send($req));
|
||||
|
||||
$resp = $cli->recv();
|
||||
var_dump($resp);
|
||||
}
|
||||
|
||||
if (in_array('post', TEST))
|
||||
{
|
||||
$req2 = new co\Http2\Request;
|
||||
$req2->path = "/index.php";
|
||||
$req2->headers = [
|
||||
'host' => "localhost",
|
||||
"user-agent" => 'Chrome/49.0.2587.3',
|
||||
'accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'accept-encoding' => 'gzip',
|
||||
];
|
||||
$req2->data = "hello world\n";
|
||||
var_dump($cli->send($req2));
|
||||
|
||||
$resp = $cli->recv();
|
||||
var_dump($resp);
|
||||
}
|
||||
|
||||
if (in_array('pipeline', TEST))
|
||||
{
|
||||
$req3 = new co\Http2\Request;
|
||||
$req3->path = "/index.php";
|
||||
$req3->headers = [
|
||||
'host' => "localhost",
|
||||
"user-agent" => 'Chrome/49.0.2587.3',
|
||||
'accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'accept-encoding' => 'gzip',
|
||||
];
|
||||
$req3->pipeline = true;
|
||||
$req3->method = "POST";
|
||||
$streamId = $cli->send($req3);
|
||||
|
||||
$cli->write($streamId, ['int' => rand(1000, 9999)]);
|
||||
$cli->write($streamId, ['int' => rand(1000, 9999)]);
|
||||
//end stream
|
||||
$cli->write($streamId, ['int' => rand(1000, 9999), 'end' => true], true);
|
||||
|
||||
var_dump($cli->recv());
|
||||
}
|
||||
|
||||
// $cli->close();
|
||||
});
|
43
vendor/swoole/examples/coroutine/http_backend_serv.php
vendored
Executable file
43
vendor/swoole/examples/coroutine/http_backend_serv.php
vendored
Executable file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* @Author: syyuanyizhi@163.com
|
||||
connect refuse: errorCode 111
|
||||
I/O timeout:errorCode 110
|
||||
http 9510
|
||||
tcp 9511
|
||||
|
||||
*/
|
||||
class Server
|
||||
{
|
||||
public $server;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->server = new Swoole\Http\Server("0.0.0.0", 9510);
|
||||
$this->server->set([
|
||||
'worker_num' => 1,
|
||||
'daemonize' => true,
|
||||
'log_file' => '/data/markyuan/swoole.log',
|
||||
]);
|
||||
$this->server->on('Request', ['Server', 'onRequest']);
|
||||
$this->server->start();
|
||||
}
|
||||
public static function onRequest($request, $response)
|
||||
{
|
||||
|
||||
$response->end('xxxx');
|
||||
}
|
||||
|
||||
|
||||
public static function staticFunc()
|
||||
{
|
||||
echo "in static function";
|
||||
}
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
|
||||
$server->run();
|
||||
|
||||
|
||||
|
12
vendor/swoole/examples/coroutine/http_client.php
vendored
Executable file
12
vendor/swoole/examples/coroutine/http_client.php
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
co::create(function () {
|
||||
$cli = new co\http\client('127.0.0.1', 9501);
|
||||
$cli->setHeaders(['Host' => 'localhost']);
|
||||
$cli->set(['http_proxy_host' => HTTP_PROXY_HOST, 'http_proxy_port' => HTTP_PROXY_PORT]);
|
||||
$result = $cli->get('/get?json=true');
|
||||
var_dump($cli->body);
|
||||
// assert($result);
|
||||
// $ret = json_decode($cli->body, true);
|
||||
// assert(is_array($ret) and $ret['json'] == 'true');
|
||||
});
|
13
vendor/swoole/examples/coroutine/http_download.php
vendored
Executable file
13
vendor/swoole/examples/coroutine/http_download.php
vendored
Executable file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$host = 'www.swoole.com';
|
||||
$cli = new \Swoole\Coroutine\Http\Client($host, 443, true);
|
||||
$cli->set(['timeout' => -1]);
|
||||
$cli->setHeaders([
|
||||
'Host' => $host,
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
'Accept' => '*',
|
||||
'Accept-Encoding' => 'gzip'
|
||||
]);
|
||||
$cli->download('/static/files/swoole-logo.svg', __DIR__ . '/logo.svg');
|
||||
});
|
37
vendor/swoole/examples/coroutine/http_server.php
vendored
Executable file
37
vendor/swoole/examples/coroutine/http_server.php
vendored
Executable file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
ini_set("memory_limit","512M");
|
||||
use Swoole\Coroutine as co;
|
||||
class Server
|
||||
{
|
||||
public $server;
|
||||
public $redisPool = [];
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->server = new Swoole\Http\Server("0.0.0.0", 9502, SWOOLE_BASE);
|
||||
$this->server->set([
|
||||
'worker_num' => 1,
|
||||
]);
|
||||
|
||||
// $this->server->on('Connect', [$this, 'onConnect']);
|
||||
$this->server->on('Request', [$this, 'onRequest']);
|
||||
// $this->server->on('Close', [$this, 'onClose']);
|
||||
$this->server->set(['trace_flags' => 1 << 15, 'log_level' => 0]);
|
||||
$this->server->start();
|
||||
}
|
||||
|
||||
public function onRequest($request, $response)
|
||||
{
|
||||
$fd = $request->fd;
|
||||
co::create(function () {
|
||||
co::sleep(0.1);
|
||||
});
|
||||
$response->end(111);
|
||||
}
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
Swoole\Coroutine::set(array(
|
||||
'max_coroutine' => 1000,
|
||||
));
|
||||
$server->run();
|
165
vendor/swoole/examples/coroutine/httpmulti.php
vendored
Executable file
165
vendor/swoole/examples/coroutine/httpmulti.php
vendored
Executable file
@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* @Author: syyuanyizhi@163.com
|
||||
connect refuse: errorCode 111
|
||||
I/O timeout:errorCode 110
|
||||
http 9510
|
||||
tcp 9511
|
||||
|
||||
*/
|
||||
class Server
|
||||
{
|
||||
public $server;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->server = new Swoole\Http\Server("0.0.0.0", 9508);
|
||||
$this->server->set([
|
||||
'worker_num' => 1,
|
||||
'daemonize' => true,
|
||||
'log_file' => '/data/markyuan/swoole.log',
|
||||
]);
|
||||
$this->server->on('Request', ['Server', 'onRequest']);
|
||||
$this->server->start();
|
||||
}
|
||||
|
||||
private static function https(){
|
||||
//--enable-openssl
|
||||
for($i=0;$i<2;$i++){
|
||||
$cli = new Swoole\Coroutine\Http\Client('0.0.0.0',443,TRUE );
|
||||
$cli->set([ 'timeout' => 1]);
|
||||
$cli->setHeaders([
|
||||
'Host' => "api.mp.qq.com",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
'Accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'Accept-Encoding' => 'gzip',
|
||||
]);
|
||||
$ret = ($cli->get('/cgi-bin/token?appid=3333&secret=222'.$i.$i.$i.$i.$i));
|
||||
error_log(__LINE__.var_export($cli,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$cli->close();
|
||||
}
|
||||
}
|
||||
|
||||
private static function http(){
|
||||
error_log(__LINE__.'---------- begin --- http --------------'.PHP_EOL,3,'/tmp/markyuan');
|
||||
for($i=0;$i<2;$i++){
|
||||
$cli = new Swoole\Coroutine\Http\Client('0.0.0.0', 9510);
|
||||
$cli->set([ 'timeout' => 1]);
|
||||
$cli->setHeaders([
|
||||
'Host' => "api.mp.qq.com",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
'Accept' => 'text/html,application/xhtml+xml,application/xml',
|
||||
'Accept-Encoding' => 'gzip',
|
||||
]);
|
||||
error_log(__LINE__.var_export($cli,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$ret = ($cli->get('/cn/token?appid=1FxxxxS9V'.$i.$i.$i.$i.$i));
|
||||
error_log(__LINE__.var_export($ret,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cli,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$cli->close();
|
||||
}
|
||||
error_log(__LINE__.'---------- end --- http --------------'.PHP_EOL,3,'/tmp/markyuan');
|
||||
|
||||
}
|
||||
|
||||
private static function multihttp(){
|
||||
|
||||
error_log(__LINE__.'---------- begin --- multi --------------'.PHP_EOL,3,'/tmp/markyuan');
|
||||
|
||||
$cliAA= new Swoole\Coroutine\Http\Client('0.0.0.0', 9510);
|
||||
$cliAA->set(['timeout' => 1]);
|
||||
$cliAA->setHeaders([
|
||||
'Host' => "api.mp.qq.com",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
]);
|
||||
$cliBB= new Swoole\Coroutine\Http\Client('0.0.0.0', 9510);
|
||||
$cliBB->set([ 'timeout' => 1]);//
|
||||
$cliBB->setHeaders([
|
||||
'Host' => "api.mp.qq.com",
|
||||
"User-Agent" => 'Chrome/49.0.2587.3',
|
||||
]);
|
||||
error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$retAA=$cliAA->setDefer(1);
|
||||
$retBB=$cliBB->setDefer(1);
|
||||
error_log(__LINE__.var_export($retAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($retBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$retAA = ($cliAA->get('/cn/token?appid=AAA'));
|
||||
$retBB = ($cliBB->get('/cn/token?appid=BBB'));
|
||||
error_log(__LINE__.var_export($retAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($retBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$retAA=$cliAA->recv();
|
||||
$retBB=$cliBB->recv();
|
||||
error_log(__LINE__.var_export($retAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($retBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliAA,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
error_log(__LINE__.var_export($cliBB,true).PHP_EOL,3,'/tmp/markyuan');
|
||||
$retAA=$cliAA->close();
|
||||
$retBB=$cliBB->close();
|
||||
error_log(__LINE__.'---------- end --- multi --------------'.PHP_EOL,3,'/tmp/markyuan');
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static function tcp(){
|
||||
for($i=0;$i<2;$i++){
|
||||
$tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$ret = $tcp_cli ->connect("0.0.0.0", 9511);
|
||||
$ret = $tcp_cli ->send('test for the coro');
|
||||
$ret = $tcp_cli ->recv();
|
||||
$ret=$tcp_cli->close();
|
||||
}
|
||||
}
|
||||
|
||||
private static function coro_dns(){
|
||||
swoole_async_set(array('use_async_resolver'=>1));
|
||||
swoole_async_set(array('dns_cache_refresh_time'=>0));
|
||||
$ret=swoole_async_dns_lookup_coro("www.baidu.com",0.5);
|
||||
error_log(' ip and host '.$host.print_r($ret,true),'3','/home/yuanyizhi/markyuan/markyuan.log');
|
||||
return $ret;
|
||||
// swoole_async_dns_lookup("www.baidu.com", function($host, $ip){
|
||||
// error_log(' ip and host '.$host.' and ip '.$ip,'3','/home/yuanyizhi/markyuan/markyuan.log');
|
||||
// });
|
||||
}
|
||||
|
||||
|
||||
private static function tcpmulti(){
|
||||
$cliAA = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$cliBB = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
$retAA = $cliAA ->connect("0.0.0.0", 9511);
|
||||
$retBB = $cliBB ->connect("0.0.0.0", 9511);
|
||||
$retAA = $cliAA ->send('test for the coro');
|
||||
$retBB = $cliBB ->send('test for the coro');
|
||||
$retAA = $cliAA->recv();
|
||||
$retBB = $cliBB->recv();
|
||||
$cliAA->close();
|
||||
$cliBB->close();
|
||||
}
|
||||
|
||||
public static function onRequest($request, $response)
|
||||
{
|
||||
// self::multihttp();
|
||||
// self::http();
|
||||
//self::https();
|
||||
// self::tcp();
|
||||
// self::tcpmulti();
|
||||
$ret=self::coro_dns();
|
||||
$response->end(print_r($ret,true));
|
||||
}
|
||||
|
||||
|
||||
public static function staticFunc()
|
||||
{
|
||||
echo "in static function";
|
||||
}
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
|
||||
$server->run();
|
||||
|
||||
|
||||
|
32
vendor/swoole/examples/coroutine/mysql_chan.php
vendored
Executable file
32
vendor/swoole/examples/coroutine/mysql_chan.php
vendored
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
$chan = new chan(4);
|
||||
|
||||
go(function () use ($chan) {
|
||||
|
||||
$db = new co\MySQL();
|
||||
$server = array(
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test',
|
||||
);
|
||||
|
||||
echo "connect\n";
|
||||
$ret1 = $db->connect($server);
|
||||
var_dump($ret1);
|
||||
|
||||
echo "prepare\n";
|
||||
$ret2 = $db->query('SELECT * FROM userinfo WHERE id=3');
|
||||
var_dump($ret2);
|
||||
|
||||
$chan->push($db);
|
||||
});
|
||||
|
||||
go(function () use ($chan) {
|
||||
$db = $chan->pop();
|
||||
$ret2 = $db->query('SELECT * FROM userinfo WHERE id=3');
|
||||
var_dump($ret2);
|
||||
});
|
14
vendor/swoole/examples/coroutine/mysql_execute_empty.php
vendored
Executable file
14
vendor/swoole/examples/coroutine/mysql_execute_empty.php
vendored
Executable file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$db = new Swoole\Coroutine\Mysql;
|
||||
$server = [
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test'
|
||||
];
|
||||
$db->connect($server);
|
||||
$stmt = $db->prepare('SELECT * FROM `userinfo`');
|
||||
$ret = $stmt->execute();
|
||||
var_dump($ret);
|
||||
});
|
42
vendor/swoole/examples/coroutine/mysql_prepare.php
vendored
Executable file
42
vendor/swoole/examples/coroutine/mysql_prepare.php
vendored
Executable file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function() {
|
||||
|
||||
$db = new co\MySQL();
|
||||
$server = array(
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test',
|
||||
);
|
||||
|
||||
echo "connect\n";
|
||||
$ret1 = $db->connect($server);
|
||||
var_dump($ret1);
|
||||
|
||||
echo "prepare [1]\n";
|
||||
$stmt1 = $db->prepare('SELECT * FROM userinfo WHERE id=?');
|
||||
var_dump($stmt1);
|
||||
if ($stmt1 == false)
|
||||
{
|
||||
var_dump($db->errno, $db->error);
|
||||
}
|
||||
|
||||
echo "execute\n";
|
||||
$ret3 = $stmt1->execute(array(10));
|
||||
var_dump(count($ret3));
|
||||
|
||||
echo "prepare [2]\n";
|
||||
$stmt2 = $db->prepare('SELECT * FROM userinfo WHERE id > ? and level > ?');
|
||||
var_dump($stmt2);
|
||||
if ($stmt2 == false)
|
||||
{
|
||||
var_dump($db->errno, $db->error);
|
||||
}
|
||||
|
||||
echo "execute\n";
|
||||
$ret4 = $stmt2->execute(array(10, 99));
|
||||
var_dump($ret4);
|
||||
});
|
||||
|
25
vendor/swoole/examples/coroutine/mysql_prepare_2.php
vendored
Executable file
25
vendor/swoole/examples/coroutine/mysql_prepare_2.php
vendored
Executable file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
|
||||
go(function() {
|
||||
|
||||
$db = new Co\MySQL();
|
||||
$server = array(
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test',
|
||||
);
|
||||
|
||||
echo "connect\n";
|
||||
$ret1 = $db->connect($server);
|
||||
var_dump($ret1);
|
||||
|
||||
echo "prepare [1]\n";
|
||||
$stmt1 = $db->prepare('show tables');
|
||||
echo "execute\n";
|
||||
$ret1 = $stmt1->execute([]);
|
||||
var_dump($ret1);
|
||||
|
||||
});
|
||||
|
28
vendor/swoole/examples/coroutine/mysql_procedure_exec.php
vendored
Executable file
28
vendor/swoole/examples/coroutine/mysql_procedure_exec.php
vendored
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
go(function () {
|
||||
$db = new Swoole\Coroutine\Mysql;
|
||||
$server = [
|
||||
'host' => '127.0.0.1',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test'
|
||||
];
|
||||
|
||||
$clear = <<<SQL
|
||||
DROP PROCEDURE IF EXISTS `say`
|
||||
SQL;
|
||||
$procedure = <<<SQL
|
||||
CREATE DEFINER=`root`@`localhost` PROCEDURE `say`(content varchar(255))
|
||||
BEGIN
|
||||
SELECT concat('you said: \"', content, '\"');
|
||||
END
|
||||
SQL;
|
||||
|
||||
$db->connect($server);
|
||||
if ($db->query($clear) && $db->query($procedure)) {
|
||||
$stmt = $db->prepare('CALL say(?)');
|
||||
$ret = $stmt->execute(['hello mysql!']);
|
||||
var_dump(current($ret[0])); // you said: "hello mysql!"
|
||||
}
|
||||
});
|
26
vendor/swoole/examples/coroutine/mysql_query.php
vendored
Executable file
26
vendor/swoole/examples/coroutine/mysql_query.php
vendored
Executable file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
use Swoole\Coroutine as co;
|
||||
co::set(['trace_flags' => 1]);
|
||||
|
||||
co::create(function() {
|
||||
|
||||
|
||||
$function = new ReflectionFunction('title');
|
||||
|
||||
$function->invoke();
|
||||
echo "invoke444\n";
|
||||
|
||||
});
|
||||
|
||||
function title() {
|
||||
echo "333invoke_________________________________\n";
|
||||
$tcpclient = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
var_dump($tcpclient->connect('127.0.0.1', 9501, 1));
|
||||
|
||||
}
|
||||
|
||||
echo "111\n";
|
||||
|
||||
|
||||
echo "222\n";
|
||||
co::go();
|
15
vendor/swoole/examples/coroutine/mysql_unixsocket.php
vendored
Executable file
15
vendor/swoole/examples/coroutine/mysql_unixsocket.php
vendored
Executable file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
go(function(){
|
||||
$db = new Swoole\Coroutine\Mysql;
|
||||
$server = [
|
||||
'host' => 'unix:/tmp/mysql.sock',
|
||||
'user' => 'root',
|
||||
'password' => 'root',
|
||||
'database' => 'test'
|
||||
];
|
||||
$db->connect($server);
|
||||
$stmt = $db->prepare('SELECT * FROM `user` WHERE id=?');
|
||||
$ret = $stmt->execute([1]);
|
||||
var_dump($ret);
|
||||
});
|
71
vendor/swoole/examples/coroutine/reconnect_test.php
vendored
Executable file
71
vendor/swoole/examples/coroutine/reconnect_test.php
vendored
Executable file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/* new multi implement test */
|
||||
$server = new Swoole\Http\Server("127.0.0.1", 9502, SWOOLE_BASE);
|
||||
|
||||
$server->set([
|
||||
'worker_num' => 1,
|
||||
]);
|
||||
|
||||
$server->on('Request', function ($request, $response) {
|
||||
|
||||
/*
|
||||
$mysql = new Swoole\Coroutine\MySQL();
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$mysql->close();
|
||||
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $mysql->query('select sleep(1)', 2);
|
||||
var_dump($res);
|
||||
|
||||
$res = $mysql->connect(['host' => '192.168.244.128', 'user' => 'mha_manager', 'password' => 'mhapass', 'database' => 'tt']);
|
||||
if ($res == false) {
|
||||
$response->end("MySQL connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $mysql->query('select sleep(1)', 2);
|
||||
var_dump($res);
|
||||
*/
|
||||
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$redis->close();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $redis->get('key');
|
||||
var_dump($res);
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("Redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$res = $redis->get('key');
|
||||
var_dump($res);
|
||||
|
||||
$response->end('Test End');
|
||||
});
|
||||
$server->start();
|
8
vendor/swoole/examples/coroutine/redis/auth.php
vendored
Executable file
8
vendor/swoole/examples/coroutine/redis/auth.php
vendored
Executable file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$redis = new Swoole\Coroutine\Redis;
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$redis->auth('root');
|
||||
$redis->set('key', 'swoole redis work');
|
||||
var_dump($redis->get('key'));
|
||||
});
|
7
vendor/swoole/examples/coroutine/redis/eval.php
vendored
Executable file
7
vendor/swoole/examples/coroutine/redis/eval.php
vendored
Executable file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
go(function (){
|
||||
$redis = new Co\Redis;
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$res = $redis->eval("return redis.call('get', 'key')");
|
||||
var_dump($res);
|
||||
});
|
7
vendor/swoole/examples/coroutine/redis/request.php
vendored
Executable file
7
vendor/swoole/examples/coroutine/redis/request.php
vendored
Executable file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$redis = new Co\Redis;
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
$res = $redis->request(['object', 'encoding', 'key1']);
|
||||
var_dump($res);
|
||||
});
|
23
vendor/swoole/examples/coroutine/redis_pool.php
vendored
Executable file
23
vendor/swoole/examples/coroutine/redis_pool.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
$count = 0;
|
||||
$pool = new SplQueue();
|
||||
$server = new Swoole\Http\Server('127.0.0.1', 9501, SWOOLE_BASE);
|
||||
|
||||
$server->on('Request', function($request, $response) use(&$count, $pool) {
|
||||
if (count($pool) == 0) {
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
if ($res == false) {
|
||||
$response->end("redis connect fail!");
|
||||
return;
|
||||
}
|
||||
$pool->push($redis);
|
||||
}
|
||||
$redis = $pool->pop();
|
||||
$count ++;
|
||||
$ret = $redis->set('key', 'value');
|
||||
$response->end("swoole response is ok, count = $count, result=" . var_export($ret, true));
|
||||
$pool->push($redis);
|
||||
});
|
||||
|
||||
$server->start();
|
15
vendor/swoole/examples/coroutine/redis_subscribe.php
vendored
Executable file
15
vendor/swoole/examples/coroutine/redis_subscribe.php
vendored
Executable file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
use Swoole\Coroutine as co;
|
||||
|
||||
co::create(function () {
|
||||
$redis = new co\Redis();
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
while (true)
|
||||
{
|
||||
$val = $redis->subscribe(['test']);
|
||||
//订阅的channel,以第一次调用subscribe时的channel为准,后续的subscribe调用是为了收取Redis Server的回包
|
||||
//如果需要改变订阅的channel,请close掉连接,再调用subscribe
|
||||
var_dump($val);
|
||||
}
|
||||
});
|
33
vendor/swoole/examples/coroutine/select/1.php
vendored
Executable file
33
vendor/swoole/examples/coroutine/select/1.php
vendored
Executable file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
$c1 = new chan();
|
||||
//consumer first with select mode
|
||||
$num = 10;
|
||||
go(function () use ($c1,$num) {
|
||||
$read_list = [$c1];
|
||||
$write_list = null;
|
||||
echo "select yield\n";
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
echo "select resume res: ".var_export($result,1)."\n";
|
||||
if ($read_list)
|
||||
{
|
||||
foreach($read_list as $ch)
|
||||
{
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $ch->pop();
|
||||
echo "pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
echo "push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->push("data-$i");
|
||||
echo "push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
|
||||
});
|
||||
echo "main end\n";
|
23
vendor/swoole/examples/coroutine/select/2.php
vendored
Executable file
23
vendor/swoole/examples/coroutine/select/2.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
$c1 = new chan();
|
||||
//consumer first without select mode
|
||||
$num = 10;
|
||||
go(function () use ($c1, $num) {
|
||||
echo "pop start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->pop();
|
||||
echo "pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
echo "push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->push("data-$i");
|
||||
echo "push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
|
||||
});
|
||||
echo "main end\n";
|
32
vendor/swoole/examples/coroutine/select/3.php
vendored
Executable file
32
vendor/swoole/examples/coroutine/select/3.php
vendored
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
$c1 = new chan(1);
|
||||
//product first with select mode
|
||||
$num = 10;
|
||||
go(function () use ($c1,$num) {
|
||||
echo "push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->push("data-$i");
|
||||
echo "push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
$read_list = [$c1];
|
||||
$write_list = null;
|
||||
echo "select yield\n";
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
echo "select resume res: ".var_export($result,1)."\n";
|
||||
if ($read_list)
|
||||
{
|
||||
foreach($read_list as $ch)
|
||||
{
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $ch->pop();
|
||||
echo "pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
echo "main end\n";
|
23
vendor/swoole/examples/coroutine/select/4.php
vendored
Executable file
23
vendor/swoole/examples/coroutine/select/4.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
$c1 = new chan(2);
|
||||
//product first without select mode
|
||||
$num = 10;
|
||||
go(function () use ($c1,$num) {
|
||||
echo "push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->push("data-$i");
|
||||
echo "push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1, $num) {
|
||||
echo "pop start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c1->pop();
|
||||
echo "pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
echo "main end\n";
|
||||
|
37
vendor/swoole/examples/coroutine/select/5.php
vendored
Executable file
37
vendor/swoole/examples/coroutine/select/5.php
vendored
Executable file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
$c1 = new chan();
|
||||
$c2 = new chan();
|
||||
function fibonacci($c1, $c2)
|
||||
{
|
||||
go(function () use ($c1, $c2) {
|
||||
$a = 0;
|
||||
$b = 1;
|
||||
while(1) {
|
||||
$read_list = [$c2];
|
||||
$write_list = [$c1];
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
if ($write_list) {
|
||||
$t = $a + $b;
|
||||
$a = $b;
|
||||
$b = $t;
|
||||
$write_list[0]->push($a);
|
||||
}
|
||||
if ($read_list) {
|
||||
$ret = $read_list[0]->pop();
|
||||
if ($ret === 1) {
|
||||
echo "quit\n";
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
$num = 10;
|
||||
go(function () use ($c1, $c2, $num) {
|
||||
for ($i = 0; $i < $num; $i ++) {
|
||||
$ret = $c1->pop();
|
||||
echo "fibonacci @$i $ret\n";
|
||||
}
|
||||
$c2->push(1);
|
||||
});
|
||||
fibonacci($c1, $c2);
|
38
vendor/swoole/examples/coroutine/select/6.php
vendored
Executable file
38
vendor/swoole/examples/coroutine/select/6.php
vendored
Executable file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
$c1 = new chan();
|
||||
|
||||
$num = 10;
|
||||
go(function () use ($c1,$num) {
|
||||
$read_list = [$c1];
|
||||
$write_list = null;
|
||||
echo "select yield\n";
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
echo "select resume res: ".var_export($result,1)."\n";
|
||||
if ($read_list)
|
||||
{
|
||||
foreach($read_list as $ch)
|
||||
{
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $ch->pop();
|
||||
echo "pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
echo "push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
if ($i == 2) {
|
||||
echo "start sleep\n";
|
||||
co:sleep(1);
|
||||
echo "end sleep\n";
|
||||
}
|
||||
$ret = $c1->push("data-$i");
|
||||
echo "push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
|
||||
});
|
||||
echo "main end\n";
|
54
vendor/swoole/examples/coroutine/select/7.php
vendored
Executable file
54
vendor/swoole/examples/coroutine/select/7.php
vendored
Executable file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
//chan1 block and chan buffer
|
||||
$c1 = new chan();
|
||||
$c2 = new chan(10);
|
||||
$num = 10;
|
||||
go(function () use ($c2,$num) {
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c2->push("chan2-$i");
|
||||
echo "chan 2 push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
go(function () use ($c1,$num) {
|
||||
$read_list = [$c1];
|
||||
$write_list = null;
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
echo "select resume res: ".var_export($result,1)."\n";
|
||||
if ($read_list)
|
||||
{
|
||||
foreach($read_list as $ch)
|
||||
{
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $ch->pop();
|
||||
echo "chan1 pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
echo "chan1 push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
if ($i == 2) {
|
||||
echo "start sleep\n";
|
||||
co:sleep(1);
|
||||
echo "end sleep\n";
|
||||
}
|
||||
$ret = $c1->push("chan1-$i");
|
||||
echo "chan1 push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
go(function () use ($c2,$num) {
|
||||
echo "chan2 pop start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c2->pop();
|
||||
echo "chan2 pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
echo "main end\n";
|
48
vendor/swoole/examples/coroutine/select/8.php
vendored
Executable file
48
vendor/swoole/examples/coroutine/select/8.php
vendored
Executable file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
//chan1 block and chan buffer
|
||||
$c1 = new chan();
|
||||
$c2 = new chan(10);
|
||||
$num = 10;
|
||||
go(function () use ($c2,$num) {
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $c2->push("chan2-$i");
|
||||
echo "chan 2 push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$c2,$num) {
|
||||
$ori_list = $read_list = [$c1,$c2];
|
||||
$write_list = null;
|
||||
$result = chan::select($read_list, $write_list, 2);
|
||||
echo "select resume res: ".var_export($result,1)."\n";
|
||||
|
||||
if ($ori_list)
|
||||
{
|
||||
foreach ($ori_list as $chan => $ch)
|
||||
{
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
$ret = $ch->pop();
|
||||
$chan_id = $chan + 1;
|
||||
echo "chan{$chan_id} pop [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
go(function () use ($c1,$num) {
|
||||
echo "chan1 push start\n";
|
||||
for ($i=0;$i<$num;$i++)
|
||||
{
|
||||
if ($i == 2) {
|
||||
echo "start sleep\n";
|
||||
co:sleep(1);
|
||||
echo "end sleep\n";
|
||||
}
|
||||
$ret = $c1->push("chan1-$i");
|
||||
echo "chan1 push [#$i] ret:".var_export($ret,1)."\n";
|
||||
}
|
||||
|
||||
});
|
||||
echo "main end\n";
|
28
vendor/swoole/examples/coroutine/send_yield.php
vendored
Executable file
28
vendor/swoole/examples/coroutine/send_yield.php
vendored
Executable file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
$serv = new Swoole\Server("0.0.0.0", 9501, SWOOLE_BASE);
|
||||
$serv->set(array(
|
||||
'worker_num' => 1,
|
||||
'send_yield' => true,
|
||||
'socket_buffer_size' => 512 * 1024,
|
||||
'kernel_socket_buffer_size' => 65536,
|
||||
));
|
||||
$serv->on('connect', function ($serv, $fd) {
|
||||
echo "Client:Connect.\n";
|
||||
});
|
||||
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
|
||||
$length = 0;
|
||||
$size = 1024 * 128;
|
||||
while (true)
|
||||
{
|
||||
$ret = $serv->send($fd, str_repeat('A', $size));
|
||||
if ($ret == false) {
|
||||
break;
|
||||
}
|
||||
$length += $size;
|
||||
echo "send $length success\n";
|
||||
}
|
||||
});
|
||||
$serv->on('close', function ($serv, $fd) {
|
||||
echo "Client: Close.\n";
|
||||
});
|
||||
$serv->start();
|
26
vendor/swoole/examples/coroutine/send_yield_client.php
vendored
Executable file
26
vendor/swoole/examples/coroutine/send_yield_client.php
vendored
Executable file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP);
|
||||
$client->set(array(
|
||||
'kernel_socket_buffer_size' => 65536,
|
||||
));
|
||||
|
||||
if (!$client->connect('127.0.0.1', 9501, -1))
|
||||
{
|
||||
exit("connect failed. Error: {$client->errCode}\n");
|
||||
}
|
||||
|
||||
var_dump($client->getsockname());
|
||||
|
||||
$client->send("start\n");
|
||||
$length = 0;
|
||||
|
||||
while(true)
|
||||
{
|
||||
$data = $client->recv(65536);
|
||||
if ($data == false) {
|
||||
break;
|
||||
}
|
||||
$length += strlen($data);
|
||||
echo "recv ".$length." bytes\n";
|
||||
usleep(100000);
|
||||
}
|
13
vendor/swoole/examples/coroutine/sleep.php
vendored
Executable file
13
vendor/swoole/examples/coroutine/sleep.php
vendored
Executable file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
$server = new Swoole\Http\Server("127.0.0.1", 9502, SWOOLE_BASE);
|
||||
|
||||
$server->set([
|
||||
'worker_num' => 1,
|
||||
]);
|
||||
|
||||
$server->on('Request', function ($request, $response) {
|
||||
Swoole\Coroutine::sleep(0.2);
|
||||
$response->end('Test End');
|
||||
});
|
||||
|
||||
$server->start();
|
17
vendor/swoole/examples/coroutine/socket/accept.php
vendored
Executable file
17
vendor/swoole/examples/coroutine/socket/accept.php
vendored
Executable file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
go(function () {
|
||||
$sock = new Swoole\Coroutine\Socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
||||
$ret = $sock->bind('127.0.0.1', 9601);
|
||||
var_dump($ret);
|
||||
assert($sock->listen(512));
|
||||
$conn = $sock->accept();
|
||||
|
||||
$data = $conn->recv();
|
||||
var_dump($data);
|
||||
$json = json_decode($data, true);
|
||||
var_dump($json);
|
||||
$ret = $conn->send("world\n");
|
||||
echo "send res {$ret} \n";
|
||||
$conn->close();
|
||||
});
|
||||
|
12
vendor/swoole/examples/coroutine/socket/sendto.php
vendored
Executable file
12
vendor/swoole/examples/coroutine/socket/sendto.php
vendored
Executable file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
echo "start \n";
|
||||
go(function () {
|
||||
$conn = new Swoole\Coroutine\Socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
||||
$ret = $conn->connect('127.0.0.1', 9601);
|
||||
echo "connect ret:".var_export($ret,1)." error:".var_export($conn->errCode,1)."\n";
|
||||
$ret = $conn->send(json_encode(['data' => 'hello']));
|
||||
echo "send ret:".var_export($ret,1)."\n";
|
||||
echo $conn->recv();
|
||||
});
|
||||
echo "end \n";
|
20
vendor/swoole/examples/coroutine/stack.php
vendored
Executable file
20
vendor/swoole/examples/coroutine/stack.php
vendored
Executable file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
co::set(['stack_size' => 8192*4]);
|
||||
|
||||
function test($n)
|
||||
{
|
||||
$a = 1;
|
||||
$b = 2;
|
||||
$c = 3;
|
||||
$d = 4;
|
||||
static $i;
|
||||
|
||||
usleep(100000);
|
||||
echo "index=".($i++)."\n";
|
||||
|
||||
return test($n + $a + $b + $c + $d);
|
||||
}
|
||||
|
||||
go(function () {
|
||||
test(9);
|
||||
});
|
27
vendor/swoole/examples/coroutine/task_co.php
vendored
Executable file
27
vendor/swoole/examples/coroutine/task_co.php
vendored
Executable file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
$server = new Swoole\Http\Server("127.0.0.1", 9502, SWOOLE_BASE);
|
||||
|
||||
$server->set([
|
||||
'worker_num' => 1,
|
||||
'task_worker_num' => 2,
|
||||
]);
|
||||
|
||||
$server->on('Task', function (swoole_server $serv, $task_id, $worker_id, $data) {
|
||||
echo "#{$serv->worker_id}\tonTask: worker_id={$worker_id}, task_id=$task_id\n";
|
||||
if ($serv->worker_id == 1) {
|
||||
sleep(1);
|
||||
}
|
||||
return $data;
|
||||
});
|
||||
|
||||
$server->on('Finish', function (swoole_server $serv, $task_id, $data) {
|
||||
echo "Task#$task_id finished, data_len=".strlen($data).PHP_EOL;
|
||||
});
|
||||
|
||||
$server->on('Request', function ($request, $response) use ($server)
|
||||
{
|
||||
$result = $server->taskCo(["hello world", ['data' => 1234, 'code' => 200]], 0.5);
|
||||
$response->end('Test End, Result: '.var_export($result, true));
|
||||
});
|
||||
|
||||
$server->start();
|
18
vendor/swoole/examples/coroutine/tcp_backend_serv.php
vendored
Executable file
18
vendor/swoole/examples/coroutine/tcp_backend_serv.php
vendored
Executable file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
$serv = new Swoole\Server("0.0.0.0", 9511);
|
||||
$serv->set(array(
|
||||
'worker_num' => 1, //工作进程数量
|
||||
'daemonize' => true, //是否作为守护进程
|
||||
));
|
||||
$serv->on('connect', function ($serv, $fd){
|
||||
echo "Client:Connect.\n";
|
||||
});
|
||||
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
|
||||
$serv->send($fd, 'Swoole: '.$data);
|
||||
$serv->close($fd);
|
||||
});
|
||||
$serv->on('close', function ($serv, $fd) {
|
||||
echo "Client: Close.\n";
|
||||
});
|
||||
$serv->start();
|
||||
|
23
vendor/swoole/examples/coroutine/tcp_echo.php
vendored
Executable file
23
vendor/swoole/examples/coroutine/tcp_echo.php
vendored
Executable file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
$serv = new swoole_server("0.0.0.0", 9501);
|
||||
$serv->on('connect', function ($serv, $fd, $reactor_id){
|
||||
echo "[#".posix_getpid()."]\tClient@[$fd]: Connect.\n";
|
||||
});
|
||||
$serv->set(array(
|
||||
'worker_num' => 1,
|
||||
|
||||
));
|
||||
|
||||
$serv->on('receive', function (swoole_server $serv, $fd, $reactor_id, $data) {
|
||||
echo "[#".$serv->worker_id."]\tClient[$fd] receive data: $data\n";
|
||||
if ($serv->send($fd, "hello {$data}\n") == false)
|
||||
{
|
||||
echo "error\n";
|
||||
}
|
||||
});
|
||||
|
||||
$serv->on('close', function ($serv, $fd, $reactor_id) {
|
||||
echo "[#".posix_getpid()."]\tClient@[$fd]: Close.\n";
|
||||
});
|
||||
|
||||
$serv->start();
|
16
vendor/swoole/examples/coroutine/timer_test.php
vendored
Executable file
16
vendor/swoole/examples/coroutine/timer_test.php
vendored
Executable file
@ -0,0 +1,16 @@
|
||||
<?
|
||||
/**
|
||||
* @Author: winterswang
|
||||
* @Date: 2016-06-26 16:34:02
|
||||
* @Last Modified by: winterswang
|
||||
* @Last Modified time: 2016-06-26 16:41:46
|
||||
*/
|
||||
|
||||
swoole_timer_after(1000, function(){
|
||||
echo " timer after timeout\n";
|
||||
});
|
||||
|
||||
swoole_timer_tick(1000, function(){
|
||||
echo "timer tick timeout\n";
|
||||
});
|
||||
?>
|
74
vendor/swoole/examples/coroutine/udp_client.php
vendored
Executable file
74
vendor/swoole/examples/coroutine/udp_client.php
vendored
Executable file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
class Client
|
||||
{
|
||||
private $ip = "127.0.0.1";
|
||||
const PORT = 8888;
|
||||
private $data;
|
||||
|
||||
public function sendRequest()
|
||||
{
|
||||
$this->data = "swoole test";
|
||||
$this->send();
|
||||
$this->moreThanOneRecv();
|
||||
return $ret;
|
||||
}
|
||||
|
||||
public function send()
|
||||
{
|
||||
$cli = new swoole_client_coro(SWOOLE_SOCK_UDP);
|
||||
$ret = $cli->connect($this->ip, self::PORT);
|
||||
$cli->send($this->data);
|
||||
$ret = $cli->recv();
|
||||
$cli->close();
|
||||
}
|
||||
|
||||
public function moreThanOneRecv()
|
||||
{
|
||||
$cli = new swoole_client_coro(SWOOLE_SOCK_UDP);
|
||||
$ret = $cli->connect($this->ip, self::PORT);
|
||||
$cli->send("sent by cli");
|
||||
|
||||
$cli2 = new swoole_client_coro(SWOOLE_SOCK_UDP);
|
||||
$ret = $cli2->connect($this->ip, self::PORT);
|
||||
$cli2->send("sent by cli2");
|
||||
|
||||
$cli3 = new swoole_client_coro(SWOOLE_SOCK_UDP);
|
||||
$ret = $cli3->connect($this->ip, self::PORT);
|
||||
$cli3->send("sent by cli3");
|
||||
|
||||
sleep(1);
|
||||
$ret = $cli3->recv();
|
||||
$ret = $cli2->recv();
|
||||
$ret = $cli->recv();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
class Server
|
||||
{
|
||||
public $server;
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->server = new swoole_http_server("127.0.0.1", 9502);
|
||||
$this->server->set([
|
||||
'worker_num' => 1,
|
||||
'daemonize' => true,
|
||||
'log_file' => '/tmp/swoole.log',
|
||||
]);
|
||||
$this->server->on('Request',['Server', 'onRequest']);
|
||||
$this->server->start();
|
||||
}
|
||||
|
||||
public static function onRequest($request, $response)
|
||||
{
|
||||
self::staticFunc();
|
||||
$cli = new swoole_client_coro(SWOOLE_SOCK_UDP);
|
||||
$client = new Client();
|
||||
$ret = $client->sendRequest();
|
||||
$response->end($ret);
|
||||
}
|
||||
}
|
||||
|
||||
$server = new Server();
|
||||
$server->run();
|
126
vendor/swoole/examples/coroutine/udp_tcp_timeout.php
vendored
Executable file
126
vendor/swoole/examples/coroutine/udp_tcp_timeout.php
vendored
Executable file
@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/**
|
||||
* @Author: winterswang
|
||||
* @Date: 2015-06-18 16:45:09
|
||||
* @Last Modified by: winterswang
|
||||
* @Last Modified time: 2016-09-18 17:36:18
|
||||
*/
|
||||
|
||||
class TestHttpServer {
|
||||
|
||||
public $http;
|
||||
public $queue;
|
||||
public $setting = array();
|
||||
|
||||
/**
|
||||
* [__construct description]
|
||||
* @param array $setting [description]
|
||||
*/
|
||||
public function __construct(){
|
||||
|
||||
}
|
||||
|
||||
public function set($setting){
|
||||
|
||||
$this ->setting = $setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* [init description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function init(){
|
||||
|
||||
if (!isset($this ->setting['host'])) {
|
||||
$this ->setting['host'] = '0.0.0.0';
|
||||
}
|
||||
if (!isset($this ->setting['port'])) {
|
||||
$this ->setting['port'] = '9999';
|
||||
}
|
||||
|
||||
$this ->http = new Swoole\Http\Server($this ->setting['host'], $this ->setting['port']);
|
||||
$this ->http ->set($this ->setting);
|
||||
|
||||
$this ->http ->on('request', array($this, 'onRequest'));
|
||||
$this ->http ->on('close', array($this, 'onClose'));
|
||||
}
|
||||
|
||||
/**
|
||||
* [onRequest description]
|
||||
* @param [type] $request [description]
|
||||
* @param [type] $response [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function onRequest($request, $response){
|
||||
|
||||
|
||||
//$udp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
|
||||
$tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
|
||||
|
||||
// $ret = $udp_cli ->connect('10.100.65.222', 9906);
|
||||
// $ret = $udp_cli ->send('test for the coro');
|
||||
// $ret = $udp_cli ->recv(100);
|
||||
// $udp_cli->close();
|
||||
|
||||
// if ($ret) {
|
||||
// //error_log(" udp cli get rsp == " . print_r($ret, true),3, '/data/log/udp_timeout.log');
|
||||
// }
|
||||
// else{
|
||||
// error_log(" udp cli timeout \n",3, '/data/log/udp_timeout.log');
|
||||
// }
|
||||
|
||||
$ret = $tcp_cli ->connect("10.100.64.151", 9805);
|
||||
$ret = $tcp_cli ->send('test for the coro');
|
||||
$ret = $tcp_cli ->recv(100);
|
||||
$tcp_cli->close();
|
||||
|
||||
if ($ret) {
|
||||
//error_log(" tcp cli get rsp == " . print_r($ret, true) . PHP_EOL, 3, '/data/log/udp_timeout.log');
|
||||
}
|
||||
else{
|
||||
error_log(" tcp cli timeout \n",3, '/data/log/udp_timeout.log');
|
||||
}
|
||||
|
||||
$response ->end(" swoole response is ok");
|
||||
}
|
||||
|
||||
/**
|
||||
* [onClose description]
|
||||
* @param [type] $server [description]
|
||||
* @param [type] $fd [description]
|
||||
* @param [type] $from_id [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function onClose($server, $fd, $from_id){
|
||||
|
||||
//echo " on close fd = $fd from_id = $from_id \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* [start description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function start(){
|
||||
|
||||
$this ->init();
|
||||
$this ->http ->start();
|
||||
}
|
||||
}
|
||||
|
||||
$setting = array(
|
||||
'host' => '0.0.0.0',
|
||||
'port' => 10006,
|
||||
'worker_num' => 4,
|
||||
'dispatch_mode' => 3, //固定分配请求到worker
|
||||
'reactor_num' => 4, //亲核
|
||||
'daemonize' => 1, //守护进程
|
||||
'backlog' => 128,
|
||||
'log_file' => '/data/log/test_http_server.log',
|
||||
);
|
||||
$th = new TestHttpServer();
|
||||
$th ->set($setting);
|
||||
$th ->start();
|
||||
|
||||
|
||||
|
||||
|
17
vendor/swoole/examples/coroutine/user_coroutine.php
vendored
Executable file
17
vendor/swoole/examples/coroutine/user_coroutine.php
vendored
Executable file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
for($i = 0; $i < 100; $i++) {
|
||||
Swoole\Coroutine::create(function() use ($i) {
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
$ret = $redis->incr('coroutine');
|
||||
$redis->close();
|
||||
if ($i == 50) {
|
||||
Swoole\Coroutine::create(function() use ($i) {
|
||||
$redis = new Swoole\Coroutine\Redis();
|
||||
$res = $redis->connect('127.0.0.1', 6379);
|
||||
$ret = $redis->set('coroutine_i', 50);
|
||||
$redis->close();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
24
vendor/swoole/examples/coroutine/websocket.php
vendored
Executable file
24
vendor/swoole/examples/coroutine/websocket.php
vendored
Executable file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
$ws = new swoole_websocket_server("127.0.0.1", 9501, SWOOLE_BASE);
|
||||
$ws->set(array(
|
||||
'log_file' => '/dev/null'
|
||||
));
|
||||
$ws->on("WorkerStart", function (\swoole_server $serv) {
|
||||
|
||||
});
|
||||
|
||||
$ws->on('open', function ($serv, swoole_http_request $request) {
|
||||
//$ip = co::gethostbyname('www.baidu.com');
|
||||
if (1) {
|
||||
$serv->push($request->fd, "start\n");
|
||||
}
|
||||
});
|
||||
|
||||
$ws->on('message', function ($serv, $frame) {
|
||||
var_dump($frame);
|
||||
co::sleep(0.1);
|
||||
$data = $frame->data;
|
||||
$serv->push($frame->fd, "hello client {$data}\n");
|
||||
});
|
||||
|
||||
$ws->start();
|
32
vendor/swoole/examples/coroutine/websocket_client.php
vendored
Executable file
32
vendor/swoole/examples/coroutine/websocket_client.php
vendored
Executable file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
// go(function () {
|
||||
// $cli = new Co\http\Client("127.0.0.1", 9501);
|
||||
// $ret = $cli->upgrade("/");
|
||||
|
||||
// if ($ret) {
|
||||
// while(true) {
|
||||
// $cli->push("hello");
|
||||
// var_dump($cli->recv());
|
||||
// co::sleep(0.1);
|
||||
// }
|
||||
// }
|
||||
|
||||
// });
|
||||
go(function () {
|
||||
$cli = new Co\http\Client("127.0.0.1", 9501);
|
||||
$cli->set([
|
||||
'timeout' => 1
|
||||
]);
|
||||
$ret = $cli->upgrade("/");
|
||||
|
||||
if (!$ret) {
|
||||
echo "ERROR\n";
|
||||
return;
|
||||
}
|
||||
var_dump($cli->recv());
|
||||
for ($i = 0; $i < 5; $i ++) {
|
||||
$cli->push("hello @$i");
|
||||
var_dump($cli->recv());
|
||||
co::sleep(0.1);
|
||||
}
|
||||
});
|
58
vendor/swoole/examples/db_pool.php
vendored
Executable file
58
vendor/swoole/examples/db_pool.php
vendored
Executable file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
$serv = new swoole_http_server("127.0.0.1", 9500);
|
||||
|
||||
$serv->set(array(
|
||||
'worker_num' => 100,
|
||||
'task_worker_num' => 20, //database connection pool
|
||||
'db_uri' => 'mysql:host=127.0.0.1;dbname=test',
|
||||
'db_user' => 'root',
|
||||
'db_passwd' => 'root',
|
||||
));
|
||||
|
||||
function my_onRequest_sync($req, $resp)
|
||||
{
|
||||
global $serv;
|
||||
$result = $serv->taskwait("show tables");
|
||||
if ($result !== false)
|
||||
{
|
||||
$resp->end(var_export($result['data'], true));
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
$resp->status(500);
|
||||
$resp->end("Server Error, Timeout\n");
|
||||
}
|
||||
}
|
||||
|
||||
function my_onTask($serv, $task_id, $from_id, $sql)
|
||||
{
|
||||
static $link = null;
|
||||
if ($link == null)
|
||||
{
|
||||
$link = new PDO($serv->setting['db_uri'], $serv->setting['db_user'], $serv->setting['db_passwd']);;
|
||||
if (!$link)
|
||||
{
|
||||
$link = null;
|
||||
return array("data" => '', 'error' => "connect database failed.");
|
||||
}
|
||||
}
|
||||
$result = $link->query($sql);
|
||||
if (!$result)
|
||||
{
|
||||
return array("data" => '', 'error' => "query error");
|
||||
}
|
||||
$data = $result->fetchAll();
|
||||
return array("data" => $data);
|
||||
}
|
||||
|
||||
function my_onFinish($serv, $data)
|
||||
{
|
||||
echo "AsyncTask Finish:Connect.PID=" . posix_getpid() . PHP_EOL;
|
||||
}
|
||||
|
||||
$serv->on('Request', 'my_onRequest_sync');
|
||||
$serv->on('Task', 'my_onTask');
|
||||
$serv->on('Finish', 'my_onFinish');
|
||||
|
||||
$serv->start();
|
36
vendor/swoole/examples/eof/async_client.php
vendored
Executable file
36
vendor/swoole/examples/eof/async_client.php
vendored
Executable file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
function send(swoole_client $cli)
|
||||
{
|
||||
$_send = str_repeat('A', rand(10000, 50000)) . "\r\n\r\n";
|
||||
$cli->send($_send);
|
||||
echo "send ".strlen($_send)." bytes\n";
|
||||
}
|
||||
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); //异步非阻塞
|
||||
$client->set(array('open_eof_check' => true, 'package_eof' => "\r\n\r\n"));
|
||||
|
||||
$client->on("connect", function(swoole_client $cli) {
|
||||
send($cli);
|
||||
});
|
||||
|
||||
$client->on("receive", function (swoole_client $cli, $data) {
|
||||
static $i = 0;
|
||||
if ($i % 100 == 1)
|
||||
{
|
||||
echo "received " . strlen($data) . " bytes\n";
|
||||
}
|
||||
$i ++;
|
||||
//usleep(200000);
|
||||
//send($cli);
|
||||
});
|
||||
|
||||
$client->on("error", function(swoole_client $cli){
|
||||
echo "error\n";
|
||||
});
|
||||
|
||||
$client->on("close", function(swoole_client $cli){
|
||||
echo "Connection close\n";
|
||||
});
|
||||
|
||||
$client->connect('127.0.0.1', 9501);
|
||||
|
75
vendor/swoole/examples/eof/client.php
vendored
Executable file
75
vendor/swoole/examples/eof/client.php
vendored
Executable file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* 分段发送数据
|
||||
*
|
||||
* @param swoole_client $client
|
||||
* @param string $data
|
||||
* @param int $chunk_size
|
||||
*/
|
||||
function send_chunk(swoole_client $client, $data, $chunk_size = 1024)
|
||||
{
|
||||
$len = strlen($data);
|
||||
$chunk_num = intval($len / $chunk_size) + 1;
|
||||
for ($i = 0; $i < $chunk_num; $i++)
|
||||
{
|
||||
if ($len < ($i + 1) * $chunk_size)
|
||||
{
|
||||
$sendn = $len - ($i * $chunk_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
$sendn = $chunk_size;
|
||||
}
|
||||
$client->send(substr($data, $i * $chunk_size, $sendn));
|
||||
}
|
||||
}
|
||||
|
||||
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_SYNC); //同步阻塞
|
||||
if(!$client->connect('127.0.0.1', 9501, 0.5, 0))
|
||||
{
|
||||
echo "Over flow. errno=".$client->errCode;
|
||||
die("\n");
|
||||
}
|
||||
|
||||
//for ($i = 0; $i < 10; $i++)
|
||||
//{
|
||||
// $client->send("hello world\r\n\r\n");
|
||||
// echo "send\n";
|
||||
//}
|
||||
//exit;
|
||||
|
||||
$data = array(
|
||||
'name' => __FILE__,
|
||||
'content' => str_repeat('A', 8192 * rand(1, 3)), //800K
|
||||
);
|
||||
|
||||
$_serialize_data = serialize($data);
|
||||
|
||||
$_send = $_serialize_data."__doit__";
|
||||
|
||||
echo "serialize_data length=".strlen($_serialize_data)."send length=".strlen($_send)."\n";
|
||||
//send_chunk($client, $_send);
|
||||
|
||||
//
|
||||
if(!$client->send($_send))
|
||||
{
|
||||
die("send failed.\n");
|
||||
}
|
||||
|
||||
//$client->send("\r\n".substr($_serialize_data, 0, 8000));
|
||||
|
||||
echo $client->recv();
|
||||
exit;
|
||||
|
||||
$client->send(substr($_serialize_data, 8000));
|
||||
|
||||
//usleep(500000);
|
||||
|
||||
if (!$client->send("\r\n\r\n"))
|
||||
{
|
||||
die("send failed.\n");
|
||||
}
|
||||
|
||||
echo $client->recv();
|
||||
|
||||
//sleep(1);
|
31
vendor/swoole/examples/eof/server.php
vendored
Executable file
31
vendor/swoole/examples/eof/server.php
vendored
Executable file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
$serv = new swoole_server("127.0.0.1", 9501, SWOOLE_BASE);
|
||||
$serv->set(array(
|
||||
'package_eof' => "\r\n\r\n",
|
||||
'open_eof_check' => true,
|
||||
'open_eof_split' => true,
|
||||
// 'worker_num' => 4,
|
||||
'dispatch_mode' => 3,
|
||||
'package_max_length' => 1024 * 1024 * 2, //2M
|
||||
));
|
||||
//$serv->on('connect', function ($serv, $fd) {
|
||||
// //echo "[#" . posix_getpid() . "]\tClient:Connect.\n";
|
||||
//});
|
||||
$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data)
|
||||
{
|
||||
echo '#' . $serv->worker_id . " recv: " . strlen($data) . "\n";
|
||||
for ($i = 0; $i < 1000; $i++)
|
||||
{
|
||||
$resp = str_repeat('A', rand(10000, 50000)) . "\r\n\r\n";
|
||||
$serv->send($fd, $resp);
|
||||
if ($i % 100 == 1)
|
||||
{
|
||||
sleep(1);
|
||||
echo "send ".strlen($resp)." bytes\n";
|
||||
}
|
||||
}
|
||||
});
|
||||
//$serv->on('close', function ($serv, $fd) {
|
||||
//echo "[#" . posix_getpid() . "]\tClient: Close.\n";
|
||||
//});
|
||||
$serv->start();
|
13
vendor/swoole/examples/event/cycle.php
vendored
Executable file
13
vendor/swoole/examples/event/cycle.php
vendored
Executable file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
Swoole\Timer::tick(2000, function ($id) {
|
||||
var_dump($id);
|
||||
});
|
||||
|
||||
Swoole\Event::cycle(function () {
|
||||
echo "hello [1]\n";
|
||||
Swoole\Event::cycle(function () {
|
||||
echo "hello [2]\n";
|
||||
Swoole\Event::cycle(null);
|
||||
});
|
||||
});
|
15
vendor/swoole/examples/event/inotify.php
vendored
Executable file
15
vendor/swoole/examples/event/inotify.php
vendored
Executable file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
//创建一个inotify句柄
|
||||
$fd = inotify_init();
|
||||
|
||||
//监听文件,仅监听修改操作,如果想要监听所有事件可以使用IN_ALL_EVENTS
|
||||
$watch_descriptor = inotify_add_watch($fd, __DIR__.'/inotify.data', IN_MODIFY);
|
||||
|
||||
swoole_event_add($fd, function ($fd) {
|
||||
$events = inotify_read($fd);
|
||||
if ($events) {
|
||||
foreach ($events as $event) {
|
||||
echo "inotify Event :" . var_export($event, 1) . "\n";
|
||||
}
|
||||
}
|
||||
});
|
53
vendor/swoole/examples/event/sockets.php
vendored
Executable file
53
vendor/swoole/examples/event/sockets.php
vendored
Executable file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* require ./configure --enable-sockets
|
||||
*/
|
||||
|
||||
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Unable to create socket\n");
|
||||
|
||||
socket_set_nonblock($socket) or die("Unable to set nonblock on socket\n");
|
||||
|
||||
function socket_onRead($socket)
|
||||
{
|
||||
static $i = 0;
|
||||
|
||||
echo socket_read($socket, 8192)."\n";
|
||||
$i ++;
|
||||
if ($i > 10)
|
||||
{
|
||||
echo "finish\n";
|
||||
swoole_event_del($socket);
|
||||
socket_close($socket);
|
||||
}
|
||||
else
|
||||
{
|
||||
sleep(1);
|
||||
swoole_event_set($socket, null, 'socket_onWrite', SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE);
|
||||
}
|
||||
}
|
||||
|
||||
function socket_onWrite($socket)
|
||||
{
|
||||
socket_write($socket, "hi swoole");
|
||||
swoole_event_set($socket, null, null, SWOOLE_EVENT_READ);
|
||||
}
|
||||
|
||||
function socket_onConnect($socket)
|
||||
{
|
||||
$err = socket_get_option($socket, SOL_SOCKET, SO_ERROR);
|
||||
if ($err == 0)
|
||||
{
|
||||
echo "connect server success\n";
|
||||
swoole_event_set($socket, null, 'socket_onWrite', SWOOLE_EVENT_READ);
|
||||
socket_write($socket, "first package\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "connect server failed\n";
|
||||
swoole_event_del($socket);
|
||||
socket_close($socket);
|
||||
}
|
||||
}
|
||||
|
||||
swoole_event_add($socket, 'socket_onRead', 'socket_onConnect', SWOOLE_EVENT_WRITE);
|
||||
@socket_connect($socket, '127.0.0.1', 9501);
|
4
vendor/swoole/examples/event/stdin.php
vendored
Executable file
4
vendor/swoole/examples/event/stdin.php
vendored
Executable file
@ -0,0 +1,4 @@
|
||||
<?php
|
||||
swoole_event_add(STDIN, function($fp) {
|
||||
echo "STDIN: ".fread($fp, 8192);
|
||||
});
|
21
vendor/swoole/examples/event/stream.php
vendored
Executable file
21
vendor/swoole/examples/event/stream.php
vendored
Executable file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
$fp = stream_socket_client("tcp://127.0.0.1:9502", $errno, $errstr, 30);
|
||||
if (!$fp) {
|
||||
exit("$errstr ($errno)<br />\n");
|
||||
}
|
||||
fwrite($fp, "HELLO world");
|
||||
|
||||
function stream_onRead($fp)
|
||||
{
|
||||
echo fread($fp, 1024)."\n";
|
||||
sleep(1);
|
||||
swoole_event_write($fp, "hello world");
|
||||
//swoole_event_set($fp, null, null, SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE);
|
||||
//swoole_event_del($fp);
|
||||
//fclose($fp);
|
||||
}
|
||||
|
||||
|
||||
swoole_event_add($fp, 'stream_onRead');
|
||||
|
||||
echo "start\n";
|
10
vendor/swoole/examples/event/test.php
vendored
Executable file
10
vendor/swoole/examples/event/test.php
vendored
Executable file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
$fp = stream_socket_client("tcp://127.0.0.1:9501", $errno, $errstr, 30);
|
||||
fwrite($fp, "HELLO world");
|
||||
|
||||
swoole_event_add($fp, function ($fp) {
|
||||
echo fread($fp, 1024)."\n";
|
||||
swoole_event_del($fp);
|
||||
fclose($fp);
|
||||
});
|
||||
|
2
vendor/swoole/examples/get_local_ip.php
vendored
Executable file
2
vendor/swoole/examples/get_local_ip.php
vendored
Executable file
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
var_dump(swoole_get_local_ip());
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user