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
ec08fee
commit 24a6c43
Showing
2 changed files
with
102 additions
and
0 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,29 @@ | ||
<?php | ||
|
||
class Client | ||
{ | ||
private $client; | ||
|
||
public function __construct() { | ||
$this->client = new swoole_client(SWOOLE_SOCK_TCP); | ||
} | ||
|
||
public function connect() { | ||
if( !$this->client->connect("127.0.0.1", 9501 , 1) ) { | ||
echo "Error: {$fp->errMsg}[{$fp->errCode}]\n"; | ||
} | ||
|
||
fwrite(STDOUT, "请输入消息:"); | ||
$msg = trim(fgets(STDIN)); | ||
$this->client->send( $msg ); | ||
} | ||
|
||
public function test() { | ||
$this->client = new swoole_client(SWOOLE_SOCK_TCP); | ||
|
||
} | ||
} | ||
|
||
$client = new Client(); | ||
$client->connect(); | ||
|
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,73 @@ | ||
<?php | ||
|
||
class Server | ||
{ | ||
private $serv; | ||
|
||
public function __construct() { | ||
$this->serv = new swoole_server("0.0.0.0", 9501); | ||
$this->serv->set(array( | ||
'worker_num' => 8, | ||
'daemonize' => false, | ||
'max_request' => 10000, | ||
'dispatch_mode' => 2, | ||
)); | ||
|
||
$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 onReceive( swoole_server $serv, $fd, $from_id, $data ) { | ||
echo $serv->worker_pid . PHP_EOL; | ||
|
||
$case = intval( $data ); | ||
|
||
switch ( $case ) { | ||
case 1: | ||
swoole_async_readfile( __DIR__."/Test.txt", function($filename, $content) { | ||
echo "$filename: $content"; | ||
}); | ||
break; | ||
case 2: | ||
swoole_async_writefile('test_2.log', "This is a test log", function($filename) { | ||
echo "wirte ok.\n"; | ||
}); | ||
break; | ||
case 3: | ||
swoole_async_read( __DIR__."/Test.txt" , function($filename, $content){ | ||
if( empty( $content ) ) { | ||
return false; | ||
} else { | ||
echo "$filename: $content"; | ||
return true; | ||
} | ||
} , 16 ); | ||
break; | ||
default: | ||
// 注:此处存在一个Bug,如果文件不存在并且没有指定offset(或者指定为-1),会引发一个错误。这里需要注意一下。 | ||
swoole_async_write( 'test_1.log', "This is a test log\n" , -1 , function(){ | ||
var_dump( func_get_args() ); | ||
}); | ||
break; | ||
} | ||
} | ||
|
||
public function onClose( $serv, $fd, $from_id ) { | ||
echo "Client {$fd} close connection\n"; | ||
} | ||
} | ||
|
||
new Server(); |