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://www.workerman.net

中文文档: http://doc3.workerman.net

Documentation:https://github.com/walkor/workerman-manual

Workerman的更多相关文章

  1. workerman centos 7 开机自动启动

    第一步: vim /lib/systemd/system/workerman.service 第二步:复制以下代码保存退出,注意修改你的workerman路径 [Unit] Description=w ...

  2. WebSocket实战之————Workerman服务器的安装启动

    安装php apt-get install php5-cli root@iZ23b64pe35Z:/home/www# php -v PHP 5.5.9-1ubuntu4.20 (cli) (buil ...

  3. workerman 的回调函数

    接下来,记录一下workerman 的回调函数 <?php /** * Created by PhpStorm. * User: zeopean * Date: 2016-08-26 * Tim ...

  4. workerman 的属性

    <?php /** * Created by PhpStorm. * User: zeopean * Date: 2016-08-26 * Time: 16:35 */ use Workerma ...

  5. workerman & swoole

    Socket 开发 workerman swoole swoole与phpdaemon/reactphp/workerman等纯PHP网络库的差异

  6. workerman是一个高性能的PHP socket服务器框架

    workerman-chatorkerman是一款纯PHP开发的开源高性能的PHP socket服务器框架.被广泛的用于手机app.手游服务端.网络游戏服务器.聊天室服务器.硬件通讯服务器.智能家居. ...

  7. workerman使用编译安装workerman的php环境

    提示 workerman只是一个代码包,如果php环境满足要求,下载后即可使用,实际上没有安装过程. workerman对php环境的要求是: 1.php>=5.3.3,可以运行命令php-v查 ...

  8. workerman安装

    1.workerman安装 workerman是php的一个socket框架,简化了socket编程,已经为很多企业所用,今天在centos的ngix+php下安装了workerman,过程记录如下. ...

  9. workerman简单例子

    workerman下载地址 http://www.workerman.net/ html <!DOCTYPE html> <html> <head> <tit ...

  10. workerman例子无法工作

    现象 workerman已经正常启动,但是按照官网写的例子或者下载的demo无法工作,例如页面打不开,socket连接失败等 解决方法 一般这种workerman启动没报错,但是无法打开页面或者无法连 ...

随机推荐

  1. Js操作Select大全(取值、设置选中等等)

    jquery操作select(取值,设置选中) 每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selecto ...

  2. MQTT Stresser

    go环境请参考https://www.cnblogs.com/saryli/p/9833253.html Load testing tool to stress MQTT message broker ...

  3. 经纬度 lbs 笔记

      string Lat = objRequest.HeadLat.ToString();   纬度                 string Lng = objRequest.HeadLng.T ...

  4. POSIX 共享内存和 系列函数

    在前面介绍了system v 共享内存的相关知识,现在来稍微看看posix 共享内存 和系列函数. 共享内存简单来说就是一块真正的物理内存区域,可以使用一些函数将这块区域映射到进程的地址空间进行读写, ...

  5. C++11新特性(1) 右值引用

    在C++中,左值(lvalue)是能够获取其地址的一个量.因为常常出如今赋值语句的左边.因此称之为左值.比如一个有名称的变量. 比如: int a=10; //a就是一个左值. 传统的C++引用,都是 ...

  6. Python2 字典 has_key() 方法

    描述 Python2 字典 has_key() 方法用于判断键(key)是否存在于字典(D)中,如果键在字典中返回True,否则返回False. 官方文档推荐用 in 操作符,因为它更短更通俗易懂.h ...

  7. js 正则表达式 exec 和 match的使用

    match body.match(/\d\.\d\.\d\.\d:\d/g); // 推荐使用exec可以拿到多个数组 exec var a = [ 'PHPSESSID=sglvjui97o18bg ...

  8. jquery中常见问题及解决办法小结

    1 在开发开放聊天室的过程中,遇到使用ajax提交表单插入数据库时会插入两条数据的情况 解决办法,在ajax函数返回后,return false. $("#btn").click( ...

  9. 4X4矩阵键盘扫描程序

    4X4矩阵键盘扫描: 1. 4根行线的GIO均设为Output,根列线的GIO均设为Input: 2. 4根行线的GIO分别置为0111.1011.1101.1110,读逐一读取列线GIO的值,可确定 ...

  10. 【Android】3.16 离线地图功能

    分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 百度地图目前已经支持矢量离线地图数据的下载.更新. 使用离线地图,可满足在无网络环境下查看地图信息的 ...