PHP code example of swokit / server
1. Go to this page and download the library: Download swokit/server 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/ */
swokit / server example snippets
$server = new TcpServer([
'rootPath' => __DIR__,
'server' => [
'port' => 12091
]
]);
....
$server->start();
$config = [
'debug' => true,
'name' => 'demo',
'rootPath' => __DIR__,
'pidFile' => __DIR__ . '/logs/test_server.pid',
// main server
'server' => [
'type' => 'tcp', // http https tcp udp ws wss rds
'port' => 9501,
],
// attach port server by config
'ports' => [
'port1' => [
'host' => '0.0.0.0',
'port' => '9761',
'type' => 'udp',
// must setting the handler class in config.
'listener' => \Swokit\Server\Listener\Port\UdpListener::class,
]
],
// for swoole
'swoole' => [
'user' => 'www-data',
'worker_num' => 4,
'task_worker_num' => 2,
'daemonize' => false,
'max_request' => 10000,
// 'log_file' => PROJECT_PATH . '/temp/logs/my_swoole_server.log',
]
];
//file: server.php
$mainServer = new swoole_http_server('0.0.0.0', 9501);
// 追加监听tcp端口
// listen 是 addListener 的别名
$port = $mainServer->listen('0.0.0.0', 9601, SWOOLE_SOCK_TCP);
$port->on('receive', function($srv, $fd, $fromId, $data){
$srv->send($fd, "Server: ".$data);
});
$mainServer->start();