Custom Processes
In Webman, you can customize listeners or processes similar to Workerman.
Note
Windows users need to start Webman withphp windows.php
to run custom processes.
Custom HTTP Service
Sometimes you may have specific requirements to modify the core code of Webman's HTTP service. In such cases, you can implement this using a custom process.
For example, create app\Server.php
<?php
namespace app;
use Webman\App;
class Server extends App
{
// Override methods in Webman\App here
}
Add the following configuration in config/process.php
use Workerman\Worker;
return [
// ... Other configurations omitted ...
'my-http' => [
'handler' => app\Server::class,
'listen' => 'http://0.0.0.0:8686',
'count' => 8, // Number of processes
'user' => '',
'group' => '',
'reusePort' => true,
'constructor' => [
'requestClass' => \support\Request::class, // Request class settings
'logger' => \support\Log::channel('default'), // Log instance
'appPath' => app_path(), // App directory location
'publicPath' => public_path() // Public directory location
]
]
];
Tip
To disable the built-in HTTP process of Webman, simply setlisten=>''
inconfig/server.php
.
Custom WebSocket Listener Example
Create app/Pusher.php
<?php
namespace app;
use Workerman\Connection\TcpConnection;
class Pusher
{
public function onConnect(TcpConnection $connection)
{
echo "onConnect\n";
}
public function onWebSocketConnect(TcpConnection $connection, $http_buffer)
{
echo "onWebSocketConnect\n";
}
public function onMessage(TcpConnection $connection, $data)
{
$connection->send($data);
}
public function onClose(TcpConnection $connection)
{
echo "onClose\n";
}
}
Note: All onXXX methods should be public.
Add the following configuration in config/process.php
return [
// ... Other process configurations omitted ...
// websocket_test is the process name
'websocket_test' => [
// Specify the process class, which is the Pusher class defined above
'handler' => app\Pusher::class,
'listen' => 'websocket://0.0.0.0:8888',
'count' => 1,
],
];
Custom Non-Listening Process Example
Create app/TaskTest.php
<?php
namespace app;
use Workerman\Timer;
use support\Db;
class TaskTest
{
public function onWorkerStart()
{
// Check for new user registrations in the database every 10 seconds
Timer::add(10, function(){
Db::table('users')->where('regist_timestamp', '>', time()-10)->get();
});
}
}
Add the following configuration in config/process.php
return [
// ... Other process configurations omitted ...
'task' => [
'handler' => app\TaskTest::class
],
];
Note: Omitting listen means it does not listen on any port, and omitting count defaults the number of processes to 1.
Configuration File Description
A complete process configuration is defined as follows:
return [
// ...
// websocket_test is the process name
'websocket_test' => [
// Specify the process class
'handler' => app\Pusher::class,
// Listening protocol, IP, and port (optional)
'listen' => 'websocket://0.0.0.0:8888',
// Number of processes (optional, default is 1)
'count' => 2,
// User to run the process (optional, default is current user)
'user' => '',
// Group to run the process (optional, default is current group)
'group' => '',
// Whether the current process supports reload (optional, default is true)
'reloadable' => true,
// Enable reusePort
'reusePort' => true,
// Transport (optional, set to ssl when SSL is needed, default is tcp)
'transport' => 'tcp',
// Context (optional, need to pass certificate path when transport is ssl)
'context' => [],
// Constructor parameters for the process class, e.g., for process\Pusher::class (optional)
'constructor' => [],
// Whether the current process is enabled
'enable' => true
],
];
Summary
The custom processes in Webman are essentially a simple encapsulation of Workerman, which separates configuration from business logic and implements Workerman's onXXX
callbacks via class methods. All other usages are identical to Workerman.