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
958699d
commit d0ce82e
Showing
60 changed files
with
3,447 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,19 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: lancelot | ||
* Date: 16-8-6 | ||
* Time: 下午4:02 | ||
*/ | ||
require_once "../vendor/autoload.php"; | ||
|
||
use Hprose\Future; | ||
|
||
$p2 = Future\sync(function() { | ||
sleep(1); | ||
return array(Future\value(1), Future\value(2)); | ||
}); | ||
$p2->then(function($value) { | ||
var_dump($value); | ||
}); | ||
var_dump("hello"); |
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,78 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: lancelot | ||
* Date: 16-8-6 | ||
* Time: 下午4:26 | ||
*/ | ||
require_once "../vendor/autoload.php"; | ||
|
||
use Hprose\Swoole\Server; | ||
use Hprose\Swoole\Socket\Service; | ||
|
||
class SwooleServer | ||
{ | ||
private $serv; | ||
public static $PDO; | ||
|
||
public function __construct() { | ||
$this->serv = new Server("http://0.0.0.0:9501"); | ||
$this->serv->set(array( | ||
'worker_num' => 1, | ||
'daemonize' => false, | ||
'max_request' => 10000, | ||
'dispatch_mode' => 2, | ||
)); | ||
|
||
$this->serv->on('Start', array($this, 'onStart')); | ||
$this->serv->on('WorkerStart', array($this, 'onWorkerStart')); | ||
|
||
// 加载配置文件 | ||
$config = require "config.php"; | ||
|
||
// 在这里注册对外服务接口 | ||
foreach($config['api_list'] as $controller) | ||
{ | ||
require_once __DIR__ . "/ctrl/$controller.php"; | ||
$this->serv->add( new $controller() ); | ||
} | ||
|
||
$port = $this->serv->listen("0.0.0.0", 9502, SWOOLE_SOCK_TCP); | ||
$rpc_service = new Service(); | ||
$rpc_service->socketHandle($port); | ||
foreach($config['api_list'] as $controller) | ||
{ | ||
require_once __DIR__ . "/ctrl/$controller.php"; | ||
$rpc_service->add( new $controller() ); | ||
} | ||
|
||
$this->serv->start(); | ||
} | ||
|
||
public function onStart( $serv ) { | ||
echo "Start\n"; | ||
//INIT | ||
|
||
} | ||
|
||
public function onWorkerStart( $serv, $worker_id ) { | ||
echo "Worker {$worker_id} start\n"; | ||
|
||
// TODO 在这里加载可能的全局变量 | ||
if(!$serv->taskworker){ | ||
SwooleServer::$PDO = new PDO( | ||
"mysql:host=localhost;port=3306;dbname=Test", | ||
"root", | ||
"123456", | ||
array( | ||
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8';", | ||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | ||
PDO::ATTR_PERSISTENT => true | ||
) | ||
); | ||
echo "Worker\n"; | ||
} | ||
} | ||
} | ||
|
||
new SwooleServer(); |
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,57 @@ | ||
<?php | ||
require_once "../vendor/autoload.php"; | ||
|
||
use \Hprose\Future; | ||
use \Hprose\Swoole\Client; | ||
use \HProse\Http\Client; | ||
use \Hprose\Socket\Client; | ||
|
||
$test = new Client("tcp://127.0.0.1:1314"); | ||
$test->fullDuplex = true; | ||
$var_dump = Future\wrap("var_dump"); | ||
|
||
Future\co(function() use ($test) { | ||
try { | ||
var_dump((yield $test->hello("yield world1"))); | ||
var_dump((yield $test->hello("yield world2"))); | ||
var_dump((yield $test->hello("yield world3"))); | ||
var_dump((yield $test->hello("yield world4"))); | ||
var_dump((yield $test->hello("yield world5"))); | ||
var_dump((yield $test->hello("yield world6"))); | ||
} | ||
catch (\Exception $e) { | ||
echo ($e); | ||
} | ||
}); | ||
|
||
$var_dump($test->hello("async world1")); | ||
$var_dump($test->hello("async world2")); | ||
$var_dump($test->hello("async world3")); | ||
$var_dump($test->hello("async world4")); | ||
$var_dump($test->hello("async world5")); | ||
$var_dump($test->hello("async world6")); | ||
|
||
$test->sum(1,2) | ||
->then(function($result) use ($test) { | ||
var_dump($result); | ||
return $test->sum($result , 1); | ||
}) | ||
->then(function($result) use ($test) { | ||
var_dump($result); | ||
return $test->sum($result , 1); | ||
}) | ||
->then(function($result) use ($test) { | ||
var_dump($result); | ||
return $test->sum($result, 1); | ||
}) | ||
->then(function($result) use ($test) { | ||
var_dump($result); | ||
return $test->sum($result , 1); | ||
}) | ||
->then(function($result) use ($test) { | ||
var_dump($result); | ||
return $test->sum($result , 1); | ||
}) | ||
->then(function($result) { | ||
var_dump($result); | ||
}); |
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,35 @@ | ||
<?php | ||
require_once "../vendor/autoload.php"; | ||
|
||
use Hprose\Swoole\Server; | ||
|
||
function hello($name) { | ||
return "Hello $name!"; | ||
} | ||
|
||
class Controller | ||
{ | ||
public function sum($a, $b) | ||
{ | ||
if( !is_int($a) ) | ||
{ | ||
return null; | ||
} | ||
if( !is_int($b) ) | ||
{ | ||
return null; | ||
} | ||
return $a + $b; | ||
} | ||
|
||
public function sub(){ | ||
|
||
} | ||
} | ||
|
||
$server = new Server("tcp://0.0.0.0:1314"); | ||
$server->setErrorTypes(E_ALL); | ||
$server->setDebugEnabled(); | ||
$server->addFunction('hello'); | ||
$server->add(new Controller()); | ||
$server->start(); |
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,27 @@ | ||
<?php | ||
require_once "../vendor/autoload.php"; | ||
|
||
use \Hprose\Future; | ||
use \Hprose\Swoole\Client; | ||
use \Hprose\Http\Client as HttpClient; | ||
|
||
$test = new Client("http://127.0.0.1:9501"); | ||
|
||
$http = new HttpClient("http://127.0.0.1:9501", false); | ||
|
||
|
||
var_dump($http->sum(1,2)); | ||
$test->sum(1,2) | ||
->then(function($result) use ($test) { | ||
var_dump($result); | ||
//swoole_event_exit(); | ||
}); | ||
$test->getResult("show tables") | ||
->then(function($result) use ($test) { | ||
var_dump($result); | ||
swoole_event_exit(); | ||
}); | ||
|
||
$test->subscribe($topic, function($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,13 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: lancelot | ||
* Date: 16-8-6 | ||
* Time: 下午4:55 | ||
*/ | ||
|
||
return [ | ||
'api_list' => [ | ||
'Api' | ||
] | ||
]; |
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,35 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: lancelot | ||
* Date: 16-8-6 | ||
* Time: 下午4:45 | ||
*/ | ||
|
||
require_once __DIR__."/../SwooleServer.php"; | ||
|
||
class Api | ||
{ | ||
public function sum($a, $b) | ||
{ | ||
return $a + $b; | ||
} | ||
|
||
public function sub($a, $b) | ||
{ | ||
return $a - $b; | ||
} | ||
|
||
public function getResult($sql) | ||
{ | ||
$statement = SwooleServer::$PDO->prepare($sql); | ||
$statement->execute(); | ||
return $statement->fetchAll(\PDO::FETCH_ASSOC); | ||
} | ||
|
||
public function publish($topic, $id, $msg, $offset) | ||
{ | ||
// offset , msg | ||
$serv->push($topic, $id, $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,8 @@ | ||
{ | ||
"require": { | ||
|
||
"hprose/hprose": "dev-master", | ||
"hprose/hprose-swoole": "dev-master" | ||
|
||
} | ||
} |
Oops, something went wrong.