workman php教程_workerman的基本用法(示例详解)

workman php教程_workerman的基本用法(示例详解)_第1张图片

Workerman是一个异步事件驱动的PHP框架,具有高性能,可轻松构建快速,可扩展的网络应用程序。支持HTTP,Websocket,SSL和其他自定义协议。支持libevent,HHVM,ReactPHP。

要求PHP 5.3或更高版本

兼容POSIX的操作系统(Linux,OSX,BSD)

用于PHP的POSIX和PCNTL扩展

安装composer require workerman/workerman

基本用法

websocket服务器

require_once __DIR__ . '/vendor/autoload.php';

use Workerman\Worker;

// 创建一个Websocket服务器

$ws_worker = new Worker("websocket://0.0.0.0:2346");

$ws_worker->count = 4;

// 在新连接到来时发出

$ws_worker->onConnect = function($connection)

{

echo "New connection\n";

};

// 接收数据时发出

$ws_worker->onMessage = function($connection, $data)

{

// Send hello $data

$connection->send('hello ' . $data);

};

// 连接关闭时发出

$ws_worker->onClose = function($connection)

{

echo "Connection closed\n";

};

// 运行worker

Worker::runAll();

http服务器require_once __DIR__ . '/vendor/autoload.php';

use Workerman\Worker;

// #### http worker ####

$http_worker = new Worker("http://0.0.0.0:2345");

$http_worker->count = 4;

// 接收数据时发出

$http_worker->onMessage = function($connection, $data)

{

//$_GET、$_POST、$_COOKIE、$_SESSION、$_SERVER、$_FILES都是可用的

var_dump($_GET, $_POST, $_COOKIE, $_SESSION, $_SERVER, $_FILES);

// 发送数据给客户端

$connection->send("hello world \n");

};

// 运行所有workers

Worker::runAll();

WebServerrequire_once __DIR__ . '/vendor/autoload.php';

use Workerman\WebServer;

use Workerman\Worker;

// WebServer

$web = new WebServer("http://0.0.0.0:80");

$web->count = 4;

$web->addRoot('www.your_domain.com', '/your/path/Web');

$web->addRoot('www.another_domain.com', '/another/path/Web');

Worker::runAll();

TCP服务器require_once __DIR__ . '/vendor/autoload.php';

use Workerman\Worker;

// #### 创建socket并监听1234端口 ####

$tcp_worker = new Worker("tcp://0.0.0.0:1234");

$tcp_worker->count = 4;

//在新连接到来时发出

$tcp_worker->onConnect = function($connection)

{

echo "New Connection\n";

};

// 接收数据时发出

$tcp_worker->onMessage = function($connection, $data)

{

// 发送数据给客户端

$connection->send("hello $data \n");

};

// 在新连接到来时发出

$tcp_worker->onClose = function($connection)

{

echo "Connection closed\n";

};

Worker::runAll();

启用SSL

require_on

你可能感兴趣的:(workman,php教程)