Workerman
What is it
Workerman is a library for event-driven programming in PHP. It has a huge number of features. Each worker is able to handle thousands of connections.
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
test.php
<?php
use Workerman\Worker;
require_once './Workerman/Autoloader.php'; // Create a Websocket server
$ws_worker = new Worker("websocket://0.0.0.0:2346"); // 4 processes
$ws_worker->count = ; // 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
test.php
require_once './Workerman/Autoloader.php';
use Workerman\Worker; // #### http worker ####
$http_worker = new Worker("http://0.0.0.0:2345"); // 4 processes
$http_worker->count = ; // 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
test.php
require_once './Workerman/Autoloader.php';
use Workerman\WebServer; // WebServer
$web = new WebServer("http://0.0.0.0:80"); // 4 processes
$web->count = ; // 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
test.php
require_once './Workerman/Autoloader.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 = ; // 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
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 ;
}
// Return length of the package
return $pos+;
} public static function decode($recv_buffer)
{
return trim($recv_buffer);
} public static function encode($data)
{
return $data."\n";
}
}
test.php
require_once './Workerman/Autoloader.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
test.php
require_once './Workerman/Autoloader.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();
run with:
php test.php start
Available commands
php test.php startphp test.php start -dphp test.php statusphp test.php stopphp test.php restartphp test.php reload
Documentation
中文文档: http://doc3.workerman.net
Documentation:https://github.com/walkor/workerman-manual
Workerman的更多相关文章
- workerman centos 7 开机自动启动
第一步: vim /lib/systemd/system/workerman.service 第二步:复制以下代码保存退出,注意修改你的workerman路径 [Unit] Description=w ...
- WebSocket实战之————Workerman服务器的安装启动
安装php apt-get install php5-cli root@iZ23b64pe35Z:/home/www# php -v PHP 5.5.9-1ubuntu4.20 (cli) (buil ...
- workerman 的回调函数
接下来,记录一下workerman 的回调函数 <?php /** * Created by PhpStorm. * User: zeopean * Date: 2016-08-26 * Tim ...
- workerman 的属性
<?php /** * Created by PhpStorm. * User: zeopean * Date: 2016-08-26 * Time: 16:35 */ use Workerma ...
- workerman & swoole
Socket 开发 workerman swoole swoole与phpdaemon/reactphp/workerman等纯PHP网络库的差异
- workerman是一个高性能的PHP socket服务器框架
workerman-chatorkerman是一款纯PHP开发的开源高性能的PHP socket服务器框架.被广泛的用于手机app.手游服务端.网络游戏服务器.聊天室服务器.硬件通讯服务器.智能家居. ...
- workerman使用编译安装workerman的php环境
提示 workerman只是一个代码包,如果php环境满足要求,下载后即可使用,实际上没有安装过程. workerman对php环境的要求是: 1.php>=5.3.3,可以运行命令php-v查 ...
- workerman安装
1.workerman安装 workerman是php的一个socket框架,简化了socket编程,已经为很多企业所用,今天在centos的ngix+php下安装了workerman,过程记录如下. ...
- workerman简单例子
workerman下载地址 http://www.workerman.net/ html <!DOCTYPE html> <html> <head> <tit ...
- workerman例子无法工作
现象 workerman已经正常启动,但是按照官网写的例子或者下载的demo无法工作,例如页面打不开,socket连接失败等 解决方法 一般这种workerman启动没报错,但是无法打开页面或者无法连 ...
随机推荐
- 【jQuery】清空表单内容
function resertForm(){ $(':input','#formId') .not(':button, :submit, :reset, :hidden') .val('') .rem ...
- JMeter学习笔记---性能分析
图像结果: 通过观察平均采样响应时长,用户可以直观地看到,随着并发压力的加大,以及性能测试时间的延长,系统性能所发生的变化.正常情况下,平均采样响应时长曲线应该是平滑的,并大致平行于图像下边界. 异常 ...
- OAF_OAF控件系列8 - SubTab的实现(案例)
2014-06-02 Created By BaoXinjian
- codeforces#254DIV2解题报告
今天简直大爆发啊... 吃了顿烧烤竟然这么管事. . . .. 本弱渣竟然做出来了3道,并且B题是我第一次在CF中用到算法..(曾经最多也就是贪心. . . ). 题目地址:codeforces#22 ...
- 以太网帧格式、IP数据报格式、TCP段格式+UDP段格式 详解
转载:http://www.cnblogs.com/lifan3a/articles/6649970.html 以太网帧格式.IP数据报格式.TCP段格式+UDP段格式 详解 1.ISO开放系统有 ...
- sqlserver计算时间差DATEDIFF 函数
DATEDIFF 函数 [日期和时间] 功能 返回两个日期之间的间隔. 语法 DATEDIFF ( date-part, date-expression-1, date-expression-2 ) ...
- django的hello world 项目
一.新建一个django项目bester: django-admin startproject bester 二.在bester项目中建一个叫polls的应用程序: cd bester/ python ...
- CSS实现超级链接需要通过双击后跳转
超级链接需要双击后跳转如何实现. CSS代码.test3 span { position: relative;}.test3 span a { position: relative;z-index: ...
- [py]python的继承体系
python的继承体系 python中一切皆对象 随着类的定义而开辟执行 class Foo(object): print 'Loading...' spam = 'eggs' print 'Done ...
- python 高阶函数 map lambda filter等
map 描述 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表. ...