• Stars
    star
    136
  • Rank 267,670 (Top 6 %)
  • Language
    PHP
  • Created about 9 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Interprocess communication component for workerman

Channel

基于订阅的多进程通讯组件,用于workerman进程间通讯或者服务器集群通讯,类似redis订阅发布机制。基于workerman开发。

Channel 提供两种通讯形式,分别是发布订阅的事件机制和消息队列机制。

它们的主要区别是:

  • 事件机制是消息发出后,所有订阅该事件的客户端都能收到消息。
  • 消息队列机制是消息发出后,所有订阅该消息的客户端只有一个会收到消息,如果客户端忙消息会进行排队直到有客户端闲置后重新取到消息。
  • 需要注意的是 Channel 只是提供一种通讯方式,本身并不提供消息确认、重试、延迟、持久化等功能,请根据实际情况合理使用。

手册地址

Channel手册

服务端

use Workerman\Worker;

//Tcp 通讯方式
$channel_server = new Channel\Server('0.0.0.0', 2206);

//Unix Domain Socket 通讯方式
//$channel_server = new Channel\Server('unix:///tmp/workerman-channel.sock');

if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}

客户端

use Workerman\Worker;

$worker = new Worker();
$worker->onWorkerStart = function()
{
    // Channel客户端连接到Channel服务端
    Channel\Client::connect('<Channel服务端ip>', 2206);

    // 使用 Unix Domain Socket 通讯
    //Channel\Client::connect('unix:///tmp/workerman-channel.sock');

    // 要订阅的事件名称(名称可以为任意的数字和字符串组合)
    $event_name = 'event_xxxx';
    // 订阅某个自定义事件并注册回调,收到事件后会自动触发此回调
    Channel\Client::on($event_name, function($event_data){
        var_dump($event_data);
    });
};
$worker->onMessage = function($connection, $data)
{
    // 要发布的事件名称
    $event_name = 'event_xxxx';
    // 事件数据(数据格式可以为数字、字符串、数组),会传递给客户端回调函数作为参数
    $event_data = array('some data.', 'some data..');
    // 发布某个自定义事件,订阅这个事件的客户端会收到事件数据,并触发客户端对应的事件回调
    Channel\Client::publish($event_name, $event_data);
};

if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}

消息队列示例

use Workerman\Worker;
use Workerman\Timer;

$worker = new Worker();
$worker->name = 'Producer';
$worker->onWorkerStart = function()
{
    Client::connect();

    $count = 0;
    Timer::add(1, function() {
        Client::enqueue('queue', 'Hello World '.time());
    });
};

$mq = new Worker();
$mq->name = 'Consumer';
$mq->count = 4;
$mq->onWorkerStart = function($worker) {
    Client::connect();

    //订阅消息 queue
    Client::watch('queue', function($data) use ($worker) {
        echo "Worker {$worker->id} get queue: $data\n";
    });

    //10 秒后取消订阅该消息
    Timer::add(10, function() {
        Client::unwatch('queue');
    }, [], false);
};

Worker::runAll();

More Repositories

1

workerman

An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols.
PHP
11,067
star
2

phpsocket.io

A server side alternative implementation of socket.io in PHP based on workerman.
PHP
2,284
star
3

webman

Probably the fastest PHP web framework in the world.
PHP
2,126
star
4

workerman-todpole

HTML5+WebSocket+PHP(Workerman) , rumpetroll server writen using php
PHP
1,844
star
5

workerman-chat

Websocket chat room written in PHP based on workerman.
PHP
1,185
star
6

GatewayWorker

Distributed realtime messaging framework based on workerman.
PHP
1,007
star
7

web-msg-sender

Web message pusher written in PHP based on workerman.
PHP
922
star
8

live-camera

Live camera based on HTML5+Canvas+Websocket+PHP (Workerman).
PHP
458
star
9

BrowserQuest-PHP

BrowserQuest server in PHP base on workerman. A PHP/HTML5/JavaScript multiplayer game experiment.
PHP
376
star
10

workerman-statistics

一个分布式统计监控系统 包含PHP客户端 、服务端
PHP
350
star
11

GatewayClient

Sdk For GatewayWorker
PHP
334
star
12

workerman-thrift

Thrift RPC for php based on workerman.
PHP
274
star
13

workerman-jsonrpc

workerman作为进程管理器,json作为协议的远程服务调用的框架
PHP
251
star
14

workerman-webrtc

php webrtc demo based on workerman
JavaScript
251
star
15

live-ascii-camera

利用HTML5将摄像头视频转换为ascii字符,通过websocket实时传输给其它页面。服务端使用workerman
PHP
215
star
16

php-socks5

socks5 proxy written in PHP based on workerman.
PHP
208
star
17

mqtt

Asynchronous MQTT client for PHP based on workerman.
PHP
203
star
18

php-http-proxy

HTTP proxy written in PHP based on workerman.
PHP
201
star
19

phptty

Share your terminal as a web application. PHP terminal emulator based on workerman.
PHP
173
star
20

workerman-vmstat

在浏览器里面显示以更友好的方式实时显示vmstats信息,包括内存、IO、cpu等信息
PHP
160
star
21

workerman-flappy-bird

flappy bird 多人在线版
PHP
119
star
22

webman-framework

webman-framework
PHP
116
star
23

workerman-for-win

workerman 3.x windows 版本(目前linux版本已经兼容windows,此版本不再维护)
PHP
109
star
24

workerman-queue

workerman 消息队列
PHP
107
star
25

GlobalData

进程间变量共享组件,用于分布式数据共享
PHP
73
star
26

workerman-todpole-web

小蝌蚪互动聊天室-适合虚拟空间使用(后端直接与workerman主机通讯。请保留页面上workerman的相关链接)
JavaScript
73
star
27

workerman-filemonitor

监控文件更新并自动reload workerman
PHP
67
star
28

workerman-manual

Workerman manual
PHP
65
star
29

crontab

A crontab written in PHP based on workerman
PHP
60
star
30

mysql

Long-living MySQL connection for daemon.
PHP
55
star
31

workerman-chat-for-win

workerman-chat windows版本
PHP
48
star
32

http-client

Asynchronous http client for PHP based on workerman.
PHP
47
star
33

rabbitmq

Asynchronous rabbitmq client for PHP based on workerman.
PHP
35
star
34

redis-queue

Message queue system written in PHP based on workerman and backed by Redis.
PHP
35
star
35

workerman-todpole-for-win

workerman-todpole-for-win
PHP
25
star
36

workerman.net

workerman.net主页源代码
PHP
25
star
37

phpsocket.io-emitter

PHP
23
star
38

workerman-MT

workerman多线程版本,同时支持Linux平台与Windows平台,接口与原Linux多进程版本兼容
PHP
22
star
39

redis

Asynchronous redis client for PHP based on workerman.
PHP
18
star
40

workerman-filemonitor-inotify

workerman文件监控并自动reload inotify版本
PHP
17
star
41

workerman-manual-zh

workerman中文手册
17
star
42

web-msg-sender-for-win

web-msg-sender for win
PHP
17
star
43

GatewayWorker-for-win

GatewayWorker for windows.
PHP
16
star
44

global-timer

Distributed timer for workerman.
PHP
15
star
45

phpsocket.io-for-win

PHP
12
star
46

webserver-example

PHP
10
star
47

stomp

Asynchronous Stomp client for PHP based on workerman.
PHP
8
star
48

tcp-proxy-demo

一个tcp代理服务器的demo
PHP
8
star
49

en.workerman.net

en.workerman.net
PHP
4
star
50

workerman-teach

PHP
4
star
51

gateway-doc

GatewayWorker manual
3
star