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启动没报错,但是无法打开页面或者无法连 ...
随机推荐
- H5版如何在微信外(非微信浏览器)进行微信支付技术方案
官方是支持在非微信内置浏览器中调起微信支付的!H5支付是基于公众号基础开发的一种非微信内浏览器支付方式(需要单独申请支付权限),可以满足在微信外的手机H5页面进行微信支付的需求.同时,由于H5链接传播 ...
- iOS - CodeReview 代码评审
1.CodeReview Code Review 中文应该译作 "代码审查" 或是 "代码评审",这是一个流程,当开发人员写好代码后,需要让别人来 review ...
- python练习笔记——计算1/1-1/3+1/5-1/7……的和
1 / 1 - 1 / 3 + 1 / 5 - 1 / 7 + ....求100000个这样的分式计算之为是多少?将此值乘以4后打印出来,看看是什么? num_list = [] count = -1 ...
- Kruskal算法 - C语言详解
最小生成树 在含有n个顶点的连通图中选择n-1条边,构成一棵极小连通子图,并使该连通子图中n-1条边上权值之和达到最小,则称其为连通网的最小生成树. 例如,对于如上图G4所示的连通网可以有多棵权值总 ...
- Python chr() 函数
描述 chr() 用一个范围在 range(256)内的(就是0-255)整数作参数,返回一个对应的字符. 语法 以下是 chr() 方法的语法: chr(i) 参数 i -- 可以是10进制也可以是 ...
- MySQL的binlog日志恢复(转)
binlog 基本认识 MySQL的二进制日志可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日 ...
- 利用ichart绘制网页图表
首先,最好的教程在这里:ichartjs 有了这个网站,要绘制网页图表简直方便愉快! 接下来说一下使用方法~~~ 进入网站,点击在线设计器 在线设计器的使用方法就不说了,摸索一下就会了!关键在于两个地 ...
- find命令之exec和xargs
exec: find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是c ...
- mysql服务器无法连接10055错误解决示例
windows服务器下,mysql运行一段时间之后忽然无法连接,但是mysql服务启动正常,连接时报错: can't connect to mysql server on 'localhost ...
- 【C语言】调整数组使奇数所有都位于偶数前面(改动)
//调整数组使奇数全部都位于偶数前面. //输入一个整数数组.实现一个函数,来调整该数组中数字的顺序使得数组中全部的奇数位于数组的前半部分,全部偶数位于数组的后半部分 #include <std ...