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. iOS_21团购_Popover适应iPad横竖屏切换

    终于效果图: 代码片段: // // DockItemLocation.m // 帅哥_团购 // // Created by beyond on 14-8-13. // Copyright (c) ...

  2. 使用Spring boot开发RestFul 风格项目PUT/DELETE方法不起作用

    在使用Spring boot 开发restful 风格的项目,put.delete方法不起作用,解决办法. 实体类Student @Data public class Student { privat ...

  3. django1.8输出一些非HTML内容

    在reportlab库中可以生成pdf文件 在https://www.reportlab.com/pypi/packages/    下载需要的版本然后,在命令行里通过pip安装.pip instal ...

  4. Windows 7/8/10 系统下Laravel框架的开发环境安装及部署详解(Vagrant + Homestead)

    注意! laravel/homestead box项目地址已经不再是原来的 https://atlas.hashicorp.com/laravel/boxes/homestead 而已经变更成 htt ...

  5. cocos2dx CallFunc注意事项

     CCDelayTime*delay=CCDelayTime::create(2); auto act = CallFunc::create([=](){   //func body ...  }); ...

  6. JAX-RS(REST Web Services)2.0 can not be installed: One or more constraints have not been satisfied

    eclipse出错: JAX-RS(REST Web Services)2.0 can not be installed: One or more constraints have not been ...

  7. Android Gradle 引用本地 AAR 的几种方式

    折衷方案: 1.方式2 - 不完美解决办法2 2.再使用"自定义Gradle代码"来减轻重复设置的问题. 自定义Gradle代码如下: repositories { flatDir ...

  8. [svc]cisco ipsec使用证书认证

    基础配置 用的c7200-adventerprisek9-mz.151-4.M2.bin - R1 conf t int f0/0 ip add 202.100.1.1 255.255.255.0 n ...

  9. Android手机WiFi调试

    一.判断手机是否能被电脑所识别: 二.输入adb tcpip 8888 设置连接的端口为8888(可以设置为任意数字,默认为5555, 后面连接的时候若不想输入端口可将端口设置为5555.)如下图所示 ...

  10. 如何創建一個自己的 Composer/Packagist 包 (PHP)

    如何創建一個自己的 Composer/Packagist 包 首先讓我們踏着歡快的腳步去Github創建一個新庫,這裏取名 composer-car,又歡快的將它克隆到本地: git clone ht ...