PHP code example of meshell / surf
1. Go to this page and download the library: Download meshell/surf library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
meshell / surf example snippets
# config.php
return [
'host' => '0.0.0.0',
'port' => 9527,
'setting' => [
'reactor_num' => 8,
'worker_num' => 10,
]
];
$app = new \Surf\Application(__DIR__, [
'app.config' => __DIR__ . '/config.php'
]);
$app->addGet('/', function() {
return "Hello world";
});
$app->register(new \Surf\Provider\PoolServiceProvider());
$app->run();
$config = erver'] = 'tcp';
$config['protocol'] = \Surf\Server\Tcp\Protocol\JsonProtocol::class;
$app = new \Surf\Application(__DIR__, [
'app.config' => $config
]);
$app->addProtocol('user.name', Examples\TestTcpController::class . ':name');
$app->run();
$client = new Swoole\Client(SWOOLE_SOCK_TCP);
if (!$client->connect('127.0.0.1', 9527, -1)) {
exit("connection failed");
}
$message = json_encode([
'name' => 'meShell',
'age' => 18,
'job' => 'engineer'
]);
$hex = pack('A64NA*', "name=user.name;format=json", strlen($message), $message);
$client->send($hex);
echo $client->recv();
$client->close();
$ php tcp.php
$ php client.php
#config.php
return [
...
'database' => [
'default' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'root',
'password' => "123456",
'options' => [],
],
],
'cache' => [
'default' => [
'driver' => 'redis',
'host' => '127.0.0.1',
'port' => 6379,
// 'prefix' => 'SURF:'
// 'auth' => 'password'
],
],
'pool' => [
'interval' => 100, //心跳检测时间,以毫秒为单位
'database' => [
'default' => [
'start_number' => 10 //默认开启
],
],
'cache' => [
'default' => [
'start_number' => 10 //默认开启
],
],
],
];
$app->register(new \Surf\Provider\PoolServiceProvider());
/**
* @var $pool PoolManager
*/
$pool = $this->container->get('pool'); //获取连接池管理对象
/**
* @var $pdo \Surf\Pool\Connection 获取一个数据库对象
*/
$pdo = $pool->pop('database.default'); //从database.default池子中获取当前对象
/**
* 获取一个缓存对象 需要注册
* $app->register(new \Surf\Provider\CacheServiceProvider());
*/
$redis = $pool->pop('cache.default'); //从cache.default池子中获取当前对象
use Surf\Server\Http\Cookie\CookieAttributes;
$app->addGet('/', function($routeVars, Request $request, Cookies $cookies) {
$session = $request->session; //获取session 对象
$session->set('userInfo', ['id' => 1]);
//使用cookie, 传入一个CookieAttributes对象
$cookies->set(CookieAttributes::create('name', 'value', 0));
});
use Surf\Server\Http\Cookie\CookieAttributes;
$app->addGet('/', 'SessionController:index');
...
class SessionController extends HttpController
{
...
public function index($routeVars)
{
$session = $this->request->session; // or $this->session; 获取session 对象
$session->set('userInfo', ['id' => 1]);
//使用cookie
$this->cookies->set(CookieAttributes::create('name', 'value', 0));
}
}
...
public function taskTest()
{
$taskId = $this->task('push all message worker' . $this->workerId, PushTaskHandle::class);
//$status = $this->syncTask('sync push all message', PushTaskHandle::class);
//var_dump($status);
return "task push id:" . $taskId . ", workId:" . $this->workerId;
}
...
$app->addTicker(100, \Surf\Examples\HeartbeatTicker::class);
try {
$app->run();
} catch (\Surf\Exception\ServerNotFoundException $e) {
}
shell
$ php http.php