# 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)
## What is it
Workerman is an asynchronous event driven PHP framework with high performance for easily building fast, scalable network applications.Supports HTTP, Websocket and other custom protocols. Supports libevent, 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 for PHP
## 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();
```
### 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
```
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Clue\React\Redis\Factory;
use Clue\React\Redis\Client;
use Workerman\Worker;
$worker = new Worker('tcp://0.0.0.0:6161');
$worker->onWorkerStart = function() {
global $factory;
$loop = Worker::getEventLoop();
$factory = new Factory($loop);
};
$worker->onMessage = function($connection, $data) {
global $factory;
$factory->createClient('localhost:6379')->then(function (Client $client) use ($connection) {
$client->set('greeting', 'Hello world');
$client->append('greeting', '!');
$client->get('greeting')->then(function ($greeting) use ($connection){
// Hello world!
echo $greeting . PHP_EOL;
$connection->send($greeting);
});
$client->incr('invocation')->then(function ($n) use ($connection){
echo 'This is invocation #' . $n . PHP_EOL;
$connection->send($n);
});
});
};
Worker::runAll();
```
### Aysnc dns of ReactPHP
```
composer require react/dns
```
```php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
$worker = new Worker('tcp://0.0.0.0:6161');
$worker->onWorkerStart = function() {
global $dns;
// Get event-loop.
$loop = Worker::getEventLoop();
$factory = new React\Dns\Resolver\Factory();
$dns = $factory->create('8.8.8.8', $loop);
};
$worker->onMessage = function($conn
没有合适的资源?快使用搜索试试~ 我知道了~
EasyAdmin极简社区源码_论坛源码
共2385个文件
php:1300个
gif:154个
phpt:136个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 99 浏览量
2022-06-28
10:27:24
上传
评论
收藏 7.5MB ZIP 举报
温馨提示
asyAdmin极简社区是基于LayUI模板制作的一套社区程序,后台采用TP5框架进行开发。 极简社区后台制作为通用后台,集合了数据库的备份与还原,权限控制,后台菜单控制等通用功能,可以供开发者开发各类前台使用 功能特色 针对个别空间安装出现两个3306进行了修正,并且增加了版主设置等插件; 增加了锁屏功能; 极简社区,意在极简,更多功能需求,可通过插件实现; 附件上传插件增加了进度条指示,并能根据需求上传大文件; 后台增加了积分记录和管理员操作记录; 前台编辑器增加了xss处理; 增加了手机浏览时的发帖按钮; 增加了后台和前台自动判断导航链接; 增加了伪静态后台配置; 修正了验证码提示不正确的问题; 增加了自定义编辑器表情; 优化了编辑器上传图片路径不统一的问题; 修复了存储型安全漏洞和csrf攻击漏洞; 增加了支付宝支付和第三方登录接口; 增加了积分名称修改;
资源推荐
资源详情
资源评论
收起资源包目录
EasyAdmin极简社区源码_论坛源码 (2385个子文件)
phpunit.bat 113B
test.bmp 0B
Nginx.conf 109B
web.Config 569B
bootstrap.min.css 115KB
font-awesome.css 37KB
layui.css 31KB
layui.css 30KB
font-awesome.min.css 30KB
global.css 28KB
_all.css 20KB
global.css 19KB
wangEditor.min.css 15KB
layer.css 14KB
layer.css 14KB
_all.css 14KB
_all.css 14KB
_all.css 12KB
nv.d3.min.css 9KB
laydate.css 8KB
laydate.css 8KB
layui.mobile.css 8KB
layui.mobile.css 8KB
admin.css 4KB
main.css 4KB
purple.css 2KB
yellow.css 2KB
orange.css 2KB
green.css 2KB
aero.css 2KB
grey.css 2KB
pink.css 2KB
blue.css 2KB
red.css 2KB
line.css 2KB
style.css 2KB
icheck.css 2KB
full.css 2KB
purple.css 1KB
yellow.css 1KB
orange.css 1KB
orange.css 1KB
yellow.css 1KB
purple.css 1KB
green.css 1KB
green.css 1KB
blue.css 1KB
pink.css 1KB
grey.css 1KB
aero.css 1KB
grey.css 1KB
aero.css 1KB
blue.css 1KB
pink.css 1KB
red.css 1KB
red.css 1KB
minimal.css 1KB
polaris.css 1KB
square.css 1KB
yellow.css 1KB
orange.css 1KB
purple.css 1KB
green.css 1KB
pink.css 1KB
aero.css 1KB
blue.css 1KB
grey.css 1KB
futurico.css 1KB
red.css 1KB
flat.css 1KB
code.css 1KB
code.css 1KB
webuploader.css 515B
attach.css 469B
form.css 234B
dashboard.html.dist 7KB
file.html.dist 3KB
TestCaseMethod.tpl.dist 3KB
directory.html.dist 2KB
phpmd.xml.dist 1KB
mocked_class.tpl.dist 1KB
phpunit.xml.dist 903B
file_item.html.dist 871B
phpmd.xml.dist 845B
phpmd.xml.dist 845B
phpunit.xml.dist 836B
phpunit.xml.dist 830B
directory_item.html.dist 821B
phpunit.xml.dist 803B
phpunit.xml.dist 773B
Migration.template.php.dist 756B
proxied_method.tpl.dist 716B
phpunit.xml.dist 681B
phpunit.xml.dist 677B
phpunit.xml.dist 661B
phpunit.xml.dist 658B
phpunit.xml.dist 654B
mocked_method.tpl.dist 644B
phpunit.xml.dist 635B
method_item.html.dist 632B
共 2385 条
- 1
- 2
- 3
- 4
- 5
- 6
- 24
资源评论
智慧浩海
- 粉丝: 1w+
- 资源: 5113
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功