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 start
php test.php start -d
php test.php status
php test.php stop
php test.php restart
php 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启动没报错,但是无法打开页面或者无法连 ...
随机推荐
- Hadoop On Demand
原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/hod.html 简介 文档 简介 Hadoop On Demand(HOD)是一个能在大型物理集群上供应虚拟 ...
- Python学习笔记016——面向对象
面向对象是指用类来描述一个对象(实例),用类来建立实例与实例的关联关系 对象 : object 实例 : instance 1 类 1.1 什么是类 类是用来描述对象的工具,用类可以创建一个或 ...
- sublime text怎样安装ctags来定位函数
sublime确实是一款非常不错的开发软件.用起来非常爽,里面集成了非常多插件.仅仅要安装就可以, 下来来介绍下sublime中ctags插件的安装,安装这个插件之后就能够高速定位某函数了,很方便. ...
- python标准库介绍——12 time 模块详解
==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...
- 项目打成jar包
distributionManagement 为发布到本地参考的地址 repository 设置从本地maven库拉取jar包 <project xmlns:xsi="http://w ...
- cocos2dx 3.x draw debug
有时候需要用ccDrawXXX绘制debug线框来调试图形程序. 在cocos2dx 2.x中,由于是立即模式,所以如果在draw函数中用ccDrawXXX画线框,要用节点的局部坐标. 在cocos2 ...
- 【VBA研究】浮点数计算总是有误差的
作者:iamlaosong 数字有两种表达方式.一种是整数,一种是浮点数.浮点数是属于有理数中某特定子集的数的数字表示,在计算机中用以近似表示随意某个实数.详细的说,这个实数由一个整数或定点数(即尾数 ...
- ISE约束文件*.ucf的写法
之前一直相不明白,为什么从官网下载的AC97的IP不能跑起来,整个IP就像空壳一样,bit_clk输进去,没有任何信号输出来.从IP的RTL来看,即使是IP不连到CPU的BUS上,只要是综合进FPGA ...
- linux 双显卡问题。。。
bumblebee的作用是禁用nvidia独立显卡,需要使用独显时,使用”optirun 程序名“手动开启nvidia来运行需要加速的程序,如optirun vmware. 打开N卡设置: optir ...
- webpack vue2.0项目配置文件详解
const path = require('path') const webpack = require('webpack') const HtmlWebpackPlugin = require('h ...