forked from LinkedDestiny/swoole-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b02416b
commit e6b521b
Showing
7 changed files
with
266 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
$socket = stream_socket_client("tcp://127.0.0.1:9501", $errno, $errstr, 30); | ||
|
||
function onRead() | ||
{ | ||
global $socket; | ||
$buffer = stream_socket_recvfrom($socket, 1024); | ||
if( !$buffer ) | ||
{ | ||
echo "Server closed\n" | ||
swoole_event_del($socket); | ||
} | ||
echo "\nRECV: {$buffer}\n"; | ||
fwrite(STDOUT, "Enter Msg:"); | ||
} | ||
|
||
function onWrite() | ||
{ | ||
global $socket; | ||
echo "on Write\n"; | ||
} | ||
|
||
function onInput() | ||
{ | ||
global $socket; | ||
$msg = trim(fgets(STDIN)); | ||
if( $msg == 'exit' ) | ||
{ | ||
swoole_event_exit(); | ||
exit(); | ||
} | ||
swoole_event_write($socket, $msg); | ||
fwrite(STDOUT, "Enter Msg:"); | ||
} | ||
|
||
swoole_event_add($socket , 'onRead', 'onWrite'); | ||
|
||
swoole_event_add(STDIN , 'onInput' ); | ||
|
||
fwrite(STDOUT, "Enter Msg:"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
class Server | ||
{ | ||
private $serv; | ||
private $test; | ||
|
||
public function __construct() { | ||
$this->serv = new swoole_server("0.0.0.0", 9501); | ||
$this->serv->set(array( | ||
'worker_num' => 1, | ||
)); | ||
$this->serv->on('Start', array($this, 'onStart')); | ||
$this->serv->on('Connect', array($this, 'onConnect')); | ||
$this->serv->on('Receive', array($this, 'onReceive')); | ||
$this->serv->on('Close', array($this, 'onClose')); | ||
|
||
$this->serv->start(); | ||
} | ||
public function onStart( $serv ) { | ||
echo "Start\n"; | ||
} | ||
public function onConnect( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} connect\n"; | ||
} | ||
public function onClose( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} close connection\n"; | ||
} | ||
|
||
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { | ||
echo "Get Message From Client {$fd}:{$data}\n"; | ||
foreach ($serv->connections as $client) { | ||
if( $fd != $client) | ||
$serv->send($client, $data); | ||
} | ||
} | ||
} | ||
$server = new Server(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?php | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
class BaseProcess | ||
{ | ||
|
||
private $process; | ||
|
||
private $process_list = []; | ||
private $process_use = []; | ||
private $min_worker_num = 3; | ||
private $max_worker_num = 6; | ||
|
||
private $current_num; | ||
|
||
public function __construct() | ||
{ | ||
$this->process = new swoole_process(array($this, 'run') , false , 2); | ||
$this->process->start(); | ||
|
||
swoole_process::wait(); | ||
} | ||
|
||
public function run() | ||
{ | ||
$this->current_num = $this->min_worker_num; | ||
|
||
for($i=0;$i<$this->current_num ; $i++){ | ||
$process = new swoole_process(array($this, 'task_run') , false , 2); | ||
$pid = $process->start(); | ||
$this->process_list[$pid] = $process; | ||
$this->process_use[$pid] = 0; | ||
} | ||
foreach ($this->process_list as $process) { | ||
swoole_event_add($process->pipe, function ($pipe) use($process) { | ||
$data = $process->read(); | ||
var_dump($data); | ||
$this->process_use[$data] = 0; | ||
}); | ||
} | ||
|
||
|
||
swoole_timer_tick(1000, function($timer_id) { | ||
static $index = 0; | ||
$index = $index + 1; | ||
$flag = true; | ||
foreach ($this->process_use as $pid => $used) { | ||
if($used == 0) | ||
{ | ||
$flag = false; | ||
$this->process_use[$pid] = 1; | ||
$this->process_list[$pid]->write($index . "Hello"); | ||
break; | ||
} | ||
} | ||
if( $flag && $this->current_num < $this->max_worker_num ) | ||
{ | ||
$process = new swoole_process(array($this, 'task_run') , false , 2); | ||
$pid = $process->start(); | ||
$this->process_list[$pid] = $process; | ||
$this->process_use[$pid] = 1; | ||
$this->process_list[$pid]->write($index . "Hello"); | ||
$this->current_num ++; | ||
} | ||
var_dump($index); | ||
if( $index == 10 ) | ||
{ | ||
foreach ($this->process_list as $process) { | ||
$process->write("exit"); | ||
} | ||
swoole_timer_clear($timer_id); | ||
$this->process->exit(); | ||
} | ||
}); | ||
} | ||
|
||
public function task_run($worker) | ||
{ | ||
swoole_event_add($worker->pipe, function ($pipe) use ($worker){ | ||
$data = $worker->read(); | ||
var_dump($worker->pid . ": " . $data); | ||
if($data == 'exit') | ||
{ | ||
$worker->exit(); | ||
exit; | ||
} | ||
sleep(5); | ||
|
||
$worker->write("" . $worker->pid); | ||
}); | ||
} | ||
} | ||
|
||
new BaseProcess(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: lancelot | ||
* Date: 16-6-29 | ||
* Time: 下午8:35 | ||
*/ | ||
|
||
class BaseProcess | ||
{ | ||
|
||
private $process; | ||
|
||
public function __construct() | ||
{ | ||
$this->process = new swoole_process(array($this, 'run') , false , true); | ||
if( !$this->process->useQueue( 123 ) ) | ||
{ | ||
var_dump(swoole_strerror(swoole_errno())); | ||
exit; | ||
} | ||
$this->process->start(); | ||
|
||
while(true) | ||
{ | ||
$data = $this->process->pop(); | ||
echo "RECV: " . $data.PHP_EOL; | ||
} | ||
} | ||
|
||
public function run($worker) | ||
{ | ||
swoole_timer_tick(1000, function($timer_id ) { | ||
static $index = 0; | ||
$index = $index + 1; | ||
$this->process->push("Hello"); | ||
var_dump($index); | ||
if( $index == 10 ) | ||
{ | ||
swoole_timer_clear($timer_id); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
new BaseProcess(); | ||
swoole_process::signal(SIGCHLD, function($sig) { | ||
//必须为false,非阻塞模式 | ||
while($ret = swoole_process::wait(false)) { | ||
echo "PID={$ret['pid']}\n"; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters