# 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, [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 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();
```
### 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
```
```php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Clu
没有合适的资源?快使用搜索试试~ 我知道了~
最新H5端 影视短视频素材网站完美运行 v1.0
共605个文件
php:326个
png:35个
css:35个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 3 下载量 28 浏览量
2022-03-09
10:26:44
上传
评论
收藏 24.8MB ZIP 举报
温馨提示
影视短视频素材网站H5端为H5端短视频素材下载平台,包括上传视频,自定义设置视频分类,会员管理等多功能。 平台主要功能: 1、视频上传:视频本地上传,格式为PM4; 2、下载分享功能:视频素材可下载分享; 3、会员管理:本系统可免费观看、下载和分享需要购买会员; 4、会员卡号:会员卡号可兑换会员,具体卡号和兑换时间可通过后台设置 平台端口:H5 开发环境: 开发语言:php( 7.0及以上版本) 数据库:mysql 安装注意事项:需要将根目录指向public目录。 并配置相应的thinkPHP伪静态配置。 后台访问路径是:http://域名/admin/login/index 账号:admin 密码:admin
资源推荐
资源详情
资源评论
收起资源包目录
最新H5端 影视短视频素材网站完美运行 v1.0 (605个子文件)
test.bmp 0B
bootstrap.min.css 118KB
mui.css 95KB
mui.min.css 84KB
esui.css 76KB
mui.min.css 74KB
all.css 71KB
animate.css 68KB
loaders.min.css 46KB
page.css 46KB
union.css 39KB
login.css 14KB
swiper.min.css 13KB
swiper.min.css 13KB
index.css 13KB
base.css 13KB
iconfont.css 11KB
message.min.css 7KB
bootstrap-select.min.css 6KB
mui.picker.min.css 5KB
style.mobile.css 5KB
share.min.css 4KB
myTheme.css 4KB
slick.css 4KB
mobileSelect.css 3KB
area.css 3KB
common.css 3KB
fileUpload.css 3KB
mui.picker.css 2KB
jqAlert.css 2KB
defaultTheme.css 2KB
store.css 1KB
loading.css 1KB
honeySwitch.css 1KB
table.css 1KB
input_tags.css 829B
Migration.template.php.dist 756B
Seed.template.php.dist 326B
安装说明文件.docx 177KB
slick.eot 2KB
hiddeninput.exe 9KB
test.gif 233KB
ajax-loader.gif 4KB
.gitignore 231B
.gitignore 184B
.gitignore 51B
.gitignore 43B
.gitignore 43B
.gitignore 42B
.gitignore 39B
.gitignore 29B
.gitignore 29B
.gitignore 28B
.gitignore 16B
.gitignore 14B
.gitignore 14B
.gitignore 14B
.gitignore 13B
.gitignore 7B
.htaccess 216B
.htaccess 13B
.htaccess 13B
nginx.htaccess 0B
.htaccess 0B
nginx.htaccess 0B
login.html 6KB
layout.html 6KB
target.html 5KB
index.html 5KB
index.html 4KB
cate.html 4KB
layout.html 4KB
videolist.html 4KB
index.html 4KB
lis.html 3KB
findWord.html 3KB
index.html 3KB
login.html 3KB
card.html 3KB
user_update.html 2KB
user.html 2KB
index.html 2KB
user_add.html 2KB
price.html 1KB
index.html 1KB
create.html 1KB
index.html 462B
index.html 462B
favicon.ico 1KB
shipin.iml 1KB
shipin.iml 1KB
3.jpg 31KB
6.jpg 31KB
1.jpg 30KB
7.jpg 30KB
8.jpg 29KB
2.jpg 29KB
4.jpg 28KB
5.jpg 27KB
test.jpg 9KB
共 605 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
- weixin_430143652023-01-06资源简直太好了,完美解决了当下遇到的难题,这样的资源很难不支持~
- 新柳科技2024-01-17这个资源对我启发很大,受益匪浅,学到了很多,谢谢分享~
- 2201_758864862023-03-26超赞的资源,感谢资源主分享,大家一起进步!
智慧浩海
- 粉丝: 1w+
- 资源: 5113
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功