PHPackages                             zyuyou/workerman - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. zyuyou/workerman

ActiveProject[Framework](/categories/framework)

zyuyou/workerman
================

 High performance Socket server framework for network applications implemented in PHP using libevent

v3.1.4(11y ago)117MITPHPPHP &gt;=5.3

Since Jan 30Pushed 11y ago1 watchersCompare

[ Source](https://github.com/zyuyou/workerman)[ Packagist](https://packagist.org/packages/zyuyou/workerman)[ Docs](http://www.workerman.net)[ RSS](/packages/zyuyou-workerman/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (6)Used By (0)

Workerman 3.0
-------------

[](#workerman-30)

文档:

Documentation:[https://github.com/walkor/workerman-manual](https://github.com/walkor/workerman-manual/blob/master/english/src/SUMMARY.md)

Home page:

What is it
----------

[](#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.

Usage
-----

[](#usage)

### A tcp server

[](#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");
//create 4 hello_worker processes
$tcp_worker->count = 4;
// when client send data to 1234 port
$tcp_worker->onMessage = function($connection, $data)
{
    // send data to client
    $connection->send("hello $data \n");
};

Worker::runAll();
```

### A http server

[](#a-http-server)

test.php

```
require_once './Workerman/Autoloader.php';
use Workerman\Worker;

// #### http worker ####
$http_worker = new Worker("http://0.0.0.0:2345");
$http_worker->count = 4;
$http_worker->onMessage = function($connection, $data)
{
    // send data to client
    $connection->send("hello world \n");
};

// run all workers
Worker::runAll();
```

### A websocket server

[](#a-websocket-server)

test.php

```
require_once './Workerman/Autoloader.php';
use Workerman\Worker
// #### websocket worker ####
$ws_worker = new Worker("websocket://0.0.0.0:5678");
$ws_worker->onMessage =  function($connection, $data)
{
    // send data to client
    $connection->send("hello world \n");
};

// run all workers
Worker::runAll();
```

### User defined protocol

[](#user-defined-protocol)

Protocols/MyTextProtocol.php

```
/**
 * 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";
    }
}
```

test.php

```
require_once './Workerman/Autoloader.php';
use Workerman\Worker
// #### MyTextProtocol worker ####
$text_worker = new Worker("MyTextProtocol://0.0.0.0:5678");
$text_worker->onMessage =  function($connection, $data)
{
    // send data to client
    $connection->send("hello world \n");
};

// run all workers
Worker::runAll();
```

### A WebServer

[](#a-webserver)

test.php

```
require_once './Workerman/Autoloader.php';
use \Workerman\WebServer;
// WebServer
$web = new WebServer("http://0.0.0.0:8686");
$web->count = 2;
$web->addRoot('www.your_domain.com', __DIR__.'/Web');
// run all workers
Worker::runAll();
```

### Timer

[](#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 width

`php test.php start`

Available commands
------------------

[](#available-commands)

`php test.php start  `
`php test.php start -d  `
[![workerman start](https://camo.githubusercontent.com/5cd1fe4085c31dd022da885a2a20ef38b1235bab419022f0c390a2c3a405f2cf/687474703a2f2f7777772e776f726b65726d616e2e6e65742f696d672f776f726b65726d616e2d73746172742e706e67)](https://camo.githubusercontent.com/5cd1fe4085c31dd022da885a2a20ef38b1235bab419022f0c390a2c3a405f2cf/687474703a2f2f7777772e776f726b65726d616e2e6e65742f696d672f776f726b65726d616e2d73746172742e706e67)
`php test.php status  `
[![workerman satus](https://camo.githubusercontent.com/9a63984ef855f1bdd4bd340b125c055edfc536014327a4cf57ee994282f5adcf/687474703a2f2f7777772e776f726b65726d616e2e6e65742f696d672f776f726b65726d616e2d7374617475732e706e673f613d313233)](https://camo.githubusercontent.com/9a63984ef855f1bdd4bd340b125c055edfc536014327a4cf57ee994282f5adcf/687474703a2f2f7777772e776f726b65726d616e2e6e65742f696d672f776f726b65726d616e2d7374617475732e706e673f613d313233)`php test.php stop  `
`php test.php restart  `
`php test.php reload  `

Benchmarks
==========

[](#benchmarks)

```
CPU:      Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz and 4 processors totally
Memory:   8G
OS:       Ubuntu 14.04 LTS
Software: ab
PHP:      5.5.9

```

**Codes**

```
