Cara Install FrameWork Swoole

Halo agan agan tukang coders

disini saya ingin menjelaskan bagaimana caranya menginstall franework Swoole

Swoole sendiri adalah framework PHP jenis baru yang di buat untuk memudahkan dalam membuat sebuah project

Fitur fitur yang ada dalam framework ini antara lain

Event-Driven, Asynchronous Programming For PHP
Async TCP / UDP / HTTP / Websocket / HTTP2

Client/Server Side API

IPv4 / IPv6 / Unixsocket / TCP/ UDP And SSL / TLS Support

Fast Serializer / Unserializer

High Performance, Scalable, Support C1000K

Milliseconds Task Scheduler

Open Source

Daemonize

Lanjut kita bahas cara install nya




Untuk Linux User

#!/bin/bash
pecl install swoole

Untuk Mac users

brew install php
#!/bin/bash
pecl install swoole



Sett lanjut pada settingan client dan server


$http = new swoole_http_server("127.0.0.1", 9501);

$http->on("start", function ($server) {
echo "Swoole http server is started at http://127.0.0.1:9501\n";
});

$http->on("request", function ($request, $response) {
$response->header("Content-Type", "text/plain");
$response->end("Hello World\n");
});

$http->start();


Untuk Web Socket


$server = new swoole_websocket_server("127.0.0.1", 9502);

$server->on('open', function($server, $req) {
echo "connection open: {$req->fd}\n";
});

$server->on('message', function($server, $frame) {
echo "received message: {$frame->data}\n";
$server->push($frame->fd, json_encode(["hello", "world"]));
});

$server->on('close', function($server, $fd) {
echo "connection close: {$fd}\n";
});
$server->start();


TCP Server
$server = new swoole_server("127.0.0.1", 9503);
$server->on('connect', function ($server, $fd){
echo "connection open: {$fd}\n";
});
$server->on('receive', function ($server, $fd, $from_id, $data) {
$server->send($fd, "Swoole: {$data}");
$server->close($fd);
});
$server->on('close', function ($server, $fd) {
echo "connection close: {$fd}\n";
});
$server->start();

TCP Client

$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function($cli) {
$cli->send("hello world\n");
});
$client->on("receive", function($cli, $data){
echo "received: {$data}\n";
});
$client->on("error", function($cli){
echo "connect failed\n";
});
$client->on("close", function($cli){
echo "connection close\n";
});
$client->connect("127.0.0.1", 9501, 0.5);


Untuk settingan Async Redis / HTTP / WebSocket Client


$redis = new Swoole\Redis;
$redis->connect('127.0.0.1', 6379, function ($redis, $result) {
$redis->set('test_key', 'value', function ($redis, $result) {
$redis->get('test_key', function ($redis, $result) {
var_dump($result);
});
});
});

$cli = new Swoole\Http\Client('127.0.0.1', 80);
$cli->setHeaders(array('User-Agent' => 'swoole-http-client'));
$cli->setCookies(array('test' => 'value'));

$cli->post('/dump.php', array("test" => 'abc'), function ($cli) {
var_dump($cli->body);
$cli->get('/index.php', function ($cli) {
var_dump($cli->cookies);
var_dump($cli->headers);
});
});


Setting Tasks nya juga

$server = new swoole_server("127.0.0.1", 9502);
$server->set(array('task_worker_num' => 4));
$server->on('receive', function($server, $fd, $from_id, $data) {
$task_id = $server->task("Async");
echo "Dispath AsyncTask: [id=$task_id]\n";
});
$server->on('task', function ($server, $task_id, $from_id, $data) {
echo "New AsyncTask[id=$task_id]\n";
$server->finish("$data -> OK");
});
$server->on('finish', function ($server, $task_id, $data) {
echo "AsyncTask[$task_id] finished: {$data}\n";
});
$server->start();



Oke cukup sekian dan terimakasih

Subscribe to receive free email updates: