# Workerman
[![Gitter](https://badges.gitter.im/walkor/Workerman.svg)](https://gitter.im/walkor/Workerman?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge)
[![Latest Stable Version](https://poser.pugx.org/workerman/workerman/v/stable)](https://packagist.org/packages/workerman/workerman)
[![Total Downloads](https://poser.pugx.org/workerman/workerman/downloads)](https://packagist.org/packages/workerman/workerman)
[![Monthly Downloads](https://poser.pugx.org/workerman/workerman/d/monthly)](https://packagist.org/packages/workerman/workerman)
[![Daily Downloads](https://poser.pugx.org/workerman/workerman/d/daily)](https://packagist.org/packages/workerman/workerman)
[![License](https://poser.pugx.org/workerman/workerman/license)](https://packagist.org/packages/workerman/workerman)
## What is it
Workerman is an asynchronous event driven PHP framework with high performance for easily building fast, scalable network applications. Supports HTTP, Websocket, SSL and other custom protocols. Supports libevent/event extension, [HHVM](https://github.com/facebook/hhvm) , [ReactPHP](https://github.com/reactphp/react).
## Requires
PHP 5.3 or Higher
A POSIX compatible operating system (Linux, OSX, BSD)
POSIX and PCNTL extensions required
Event extension recommended for better performance
## Installation
```
composer require workerman/workerman
```
## Basic Usage
### A websocket server
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
// Create a Websocket server
$ws_worker = new Worker("websocket://0.0.0.0:2346");
// 4 processes
$ws_worker->count = 4;
// Emitted when new connection come
$ws_worker->onConnect = function($connection)
{
echo "New connection\n";
};
// Emitted when data received
$ws_worker->onMessage = function($connection, $data)
{
// Send hello $data
$connection->send('hello ' . $data);
};
// Emitted when connection closed
$ws_worker->onClose = function($connection)
{
echo "Connection closed\n";
};
// Run worker
Worker::runAll();
```
### An http server
```php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
// #### http worker ####
$http_worker = new Worker("http://0.0.0.0:2345");
// 4 processes
$http_worker->count = 4;
// Emitted when data received
$http_worker->onMessage = function($connection, $data)
{
// $_GET, $_POST, $_COOKIE, $_SESSION, $_SERVER, $_FILES are available
var_dump($_GET, $_POST, $_COOKIE, $_SESSION, $_SERVER, $_FILES);
// send data to client
$connection->send("hello world \n");
};
// run all workers
Worker::runAll();
```
### A WebServer
```php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\WebServer;
use Workerman\Worker;
// WebServer
$web = new WebServer("http://0.0.0.0:80");
// 4 processes
$web->count = 4;
// Set the root of domains
$web->addRoot('www.your_domain.com', '/your/path/Web');
$web->addRoot('www.another_domain.com', '/another/path/Web');
// run all workers
Worker::runAll();
```
### A tcp server
```php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
// #### create socket and listen 1234 port ####
$tcp_worker = new Worker("tcp://0.0.0.0:1234");
// 4 processes
$tcp_worker->count = 4;
// Emitted when new connection come
$tcp_worker->onConnect = function($connection)
{
echo "New Connection\n";
};
// Emitted when data received
$tcp_worker->onMessage = function($connection, $data)
{
// send data to client
$connection->send("hello $data \n");
};
// Emitted when new connection come
$tcp_worker->onClose = function($connection)
{
echo "Connection closed\n";
};
Worker::runAll();
```
### Enable SSL
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
// SSL context.
$context = array(
'ssl' => array(
'local_cert' => '/your/path/of/server.pem',
'local_pk' => '/your/path/of/server.key',
'verify_peer' => false,
)
);
// Create a Websocket server with ssl context.
$ws_worker = new Worker("websocket://0.0.0.0:2346", $context);
// Enable SSL. WebSocket+SSL means that Secure WebSocket (wss://).
// The similar approaches for Https etc.
$ws_worker->transport = 'ssl';
$ws_worker->onMessage = function($connection, $data)
{
// Send hello $data
$connection->send('hello ' . $data);
};
Worker::runAll();
```
### Custom protocol
Protocols/MyTextProtocol.php
```php
namespace Protocols;
/**
* User defined protocol
* Format Text+"\n"
*/
class MyTextProtocol
{
public static function input($recv_buffer)
{
// Find the position of the first occurrence of "\n"
$pos = strpos($recv_buffer, "\n");
// Not a complete package. Return 0 because the length of package can not be calculated
if($pos === false)
{
return 0;
}
// Return length of the package
return $pos+1;
}
public static function decode($recv_buffer)
{
return trim($recv_buffer);
}
public static function encode($data)
{
return $data."\n";
}
}
```
```php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
// #### MyTextProtocol worker ####
$text_worker = new Worker("MyTextProtocol://0.0.0.0:5678");
$text_worker->onConnect = function($connection)
{
echo "New connection\n";
};
$text_worker->onMessage = function($connection, $data)
{
// send data to client
$connection->send("hello world \n");
};
$text_worker->onClose = function($connection)
{
echo "Connection closed\n";
};
// run all workers
Worker::runAll();
```
### Timer
```php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Lib\Timer;
$task = new Worker();
$task->onWorkerStart = function($task)
{
// 2.5 seconds
$time_interval = 2.5;
$timer_id = Timer::add($time_interval,
function()
{
echo "Timer run\n";
}
);
};
// run all workers
Worker::runAll();
```
### AsyncTcpConnection (tcp/ws/text/frame etc...)
```php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
$worker = new Worker();
$worker->onWorkerStart = function()
{
// Websocket protocol for client.
$ws_connection = new AsyncTcpConnection("ws://echo.websocket.org:80");
$ws_connection->onConnect = function($connection){
$connection->send('hello');
};
$ws_connection->onMessage = function($connection, $data){
echo "recv: $data\n";
};
$ws_connection->onError = function($connection, $code, $msg){
echo "error: $msg\n";
};
$ws_connection->onClose = function($connection){
echo "connection closed\n";
};
$ws_connection->connect();
};
Worker::runAll();
```
### Async Mysql of ReactPHP
```
composer require react/mysql
```
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
$worker = new Worker('tcp://0.0.0.0:6161');
$worker->onWorkerStart = function() {
global $mysql;
$loop = Worker::getEventLoop();
$mysql = new React\MySQL\Connection($loop, array(
'host' => '127.0.0.1',
'dbname' => 'dbname',
'user' => 'user',
'passwd' => 'passwd',
));
$mysql->on('error', function($e){
echo $e;
});
$mysql->connect(function ($e) {
if($e) {
echo $e;
} else {
echo "connect success\n";
}
});
};
$worker->onMessage = function($connection, $data) {
global $mysql;
$mysql->query('show databases' /*trim($data)*/, function ($command, $mysql) use ($connection) {
if ($command->hasError()) {
$error = $command->getError();
} else {
$results = $command->resultRows;
$fields = $command->resultFields;
$connection->send(json_encode($results));
}
});
};
Worker::runAll();
```
### Async Redis of ReactPHP
```
composer require clue/redis-react
`
没有合适的资源?快使用搜索试试~ 我知道了~
AI拓客商家联盟小程序 hc_toker 2.3.0 安装更新一体包.rar
共1138个文件
php:377个
png:268个
jpg:113个
需积分: 5 1 下载量 97 浏览量
2023-06-08
16:57:02
上传
评论
收藏 43.34MB RAR 举报
温馨提示
版权声明: 本资源仅供学习参考,不得进行任何商业用途!
资源推荐
资源详情
资源评论
收起资源包目录
AI拓客商家联盟小程序 hc_toker 2.3.0 安装更新一体包.rar (1138个子文件)
module.cer 50B
index.css 25KB
index.css 21KB
index.css 15KB
edit.css 8KB
edit.css 6KB
edit.css 6KB
bootstrap-select.css 6KB
bootstrap-select.min.css 5KB
edit_left.css 5KB
edit_right.css 5KB
edit_left.css 4KB
edit_left.css 4KB
edit_right.css 4KB
edit_right.css 4KB
comment.css 355B
comment.css 355B
comment.css 355B
.DS_Store 10KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.gitignore 51B
create.html 33KB
create.html 33KB
create.html 30KB
create.html 30KB
create.html 30KB
setting.html 28KB
create.html 24KB
diy.html 14KB
diy_bak1.html 10KB
withdraw.html 7KB
examine.html 7KB
commission.html 7KB
recommend.html 7KB
saleractive.html 6KB
users.html 6KB
diy_basic.html 5KB
payment.html 5KB
card.html 5KB
examine_edit.html 4KB
card_edit.html 4KB
upgrade_edit.html 4KB
saleractive_edit.html 4KB
users_edit.html 3KB
active.html 3KB
banner.html 3KB
banner_post.html 3KB
upgrade.html 2KB
express.html 2KB
help.html 2KB
saleractive_joinlog.html 2KB
help_edit.html 2KB
maketpl.html 107B
system.html 23B
5d009496a5412.jpeg 1004KB
5d074d06688b5.jpeg 371KB
201906250401145d11d4ca3f67a.jpeg 162KB
201906250401235d11d4d312ef9.jpeg 100KB
5d0c95d0c93a7bca573a7bca57.jpeg 2KB
5d07621664879.jpg 398KB
5d54fadc18954.jpg 322KB
5d54fae96ba18.jpg 303KB
5d07621c378e9.jpg 292KB
5d4ba11dcec42.jpg 286KB
banner.jpg 256KB
5d00f10206928.jpg 251KB
bg.jpg 223KB
bg.jpg 223KB
5d54f9fc1c0bb.jpg 217KB
5d5504a761e86.jpg 202KB
5d677d6f0c8ad.jpg 179KB
bg_top.jpg 171KB
bg_top.jpg 171KB
bg_top.jpg 171KB
5d006ecb4fe5b.jpg 156KB
5d676d004c640.jpg 149KB
5d677e824e6ad.jpg 144KB
banner1.jpg 143KB
bg_top.jpg 136KB
5d677f5f29430.jpg 136KB
5d06fc9418234.jpg 129KB
5cf7599dcafec.jpg 128KB
5cf757a7ab407.jpg 128KB
5d00d3071ff2d.jpg 126KB
5d676cf53fec8.jpg 125KB
5d677dc54e52e.jpg 125KB
bg.jpg 114KB
5d06f705333d3.jpg 112KB
5d06f7b07324e.jpg 112KB
5d06f70b7295a.jpg 112KB
5d06f6f13f306.jpg 112KB
forward_thumb.jpg 111KB
5d0b250f80617.jpg 111KB
5d677e897c9ac.jpg 107KB
saler_invite_bg.jpg 100KB
共 1138 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12
资源评论
2301_78600126
- 粉丝: 1
- 资源: 686
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功