例子1

(要求Workerman版本>=3.3.0)

基于Worker的多进程(分布式集群)推送系统,集群群发、集群广播。

start_channel.php 整个系统只能部署一个start_channel服务。假设运行在192.168.1.1。

  1. <?php
  2. use Workerman\Worker;
  3. require_once __DIR__ . '/vendor/autoload.php';
  4. // 初始化一个Channel服务端
  5. $channel_server = new Channel\Server('0.0.0.0', 2206);
  6. Worker::runAll();

start_ws.php 整个系统可以部署多个start_ws服务,假设运行在 192.168.1.2和192.168.1.3 两台服务器上。

  1. <?php
  2. use Workerman\Worker;
  3. use Workerman\Connection\TcpConnection;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. // websocket服务端
  6. $worker = new Worker('websocket://0.0.0.0:4236');
  7. $worker->count=2;
  8. $worker->name = 'pusher';
  9. $worker->onWorkerStart = function($worker)
  10. {
  11. // Channel客户端连接到Channel服务端
  12. Channel\Client::connect('192.168.1.1', 2206);
  13. // 以自己的进程id为事件名称
  14. $event_name = $worker->id;
  15. // 订阅worker->id事件并注册事件处理函数
  16. Channel\Client::on($event_name, function($event_data)use($worker){
  17. $to_connection_id = $event_data['to_connection_id'];
  18. $message = $event_data['content'];
  19. if(!isset($worker->connections[$to_connection_id]))
  20. {
  21. echo "connection not exists\n";
  22. return;
  23. }
  24. $to_connection = $worker->connections[$to_connection_id];
  25. $to_connection->send($message);
  26. });
  27. // 订阅广播事件
  28. $event_name = '广播';
  29. // 收到广播事件后向当前进程内所有客户端连接发送广播数据
  30. Channel\Client::on($event_name, function($event_data)use($worker){
  31. $message = $event_data['content'];
  32. foreach($worker->connections as $connection)
  33. {
  34. $connection->send($message);
  35. }
  36. });
  37. };
  38. $worker->onConnect = function(TcpConnection $connection)use($worker)
  39. {
  40. $msg = "workerID:{$worker->id} connectionID:{$connection->id} connected\n";
  41. echo $msg;
  42. $connection->send($msg);
  43. };
  44. Worker::runAll();

start_http.php 整个系统可以部署多个start_ws服务,假设运行在 192.168.1.4和192.168.1.5 两台服务器上。

  1. <?php
  2. use Workerman\Worker;
  3. use Workerman\Connection\TcpConnection;
  4. require_once __DIR__ . '/vendor/autoload.php';
  5. // 用来处理http请求,向任意客户端推送数据,需要传workerID和connectionID
  6. $http_worker = new Worker('http://0.0.0.0:4237');
  7. $http_worker->name = 'publisher';
  8. $http_worker->onWorkerStart = function()
  9. {
  10. Channel\Client::connect('192.168.1.1', 2206);
  11. };
  12. $http_worker->onMessage = function(TcpConnection $connection, $request)
  13. {
  14. // 兼容workerman4.x
  15. if (!is_array($request)) {
  16. $_GET = $request->get();
  17. }
  18. $connection->send('ok');
  19. if(empty($_GET['content'])) return;
  20. // 是向某个worker进程中某个连接推送数据
  21. if(isset($_GET['to_worker_id']) && isset($_GET['to_connection_id']))
  22. {
  23. $event_name = $_GET['to_worker_id'];
  24. $to_connection_id = $_GET['to_connection_id'];
  25. $content = $_GET['content'];
  26. Channel\Client::publish($event_name, array(
  27. 'to_connection_id' => $to_connection_id,
  28. 'content' => $content
  29. ));
  30. }
  31. // 是全局广播数据
  32. else
  33. {
  34. $event_name = '广播';
  35. $content = $_GET['content'];
  36. Channel\Client::publish($event_name, array(
  37. 'content' => $content
  38. ));
  39. }
  40. };
  41. Worker::runAll();

测试

1、运行个个服务器上的服务

2、客户端连接服务端

打开chrome浏览器,按F12打开调试控制台,在Console一栏输入(或者把下面代码放入到html页面用js运行)

  1. // 也可以连ws://192.168.1.3:4236
  2. ws = new WebSocket("ws://192.168.1.2:4236");
  3. ws.onmessage = function(e) {
  4. alert("收到服务端的消息:" + e.data);
  5. };

3、通过调用http接口推送

url访问 http://192.168.1.4:4237/?content={$content} 或者 http://192.168.1.5:4237/?content={$content}向所有客户端连接推送$content数据

url访问 http://192.168.1.4:4237/?to_worker_id={$worker_id}&to_connection_id={$connection_id}&content={$content} 或者http://192.168.1.5:4237/?to_worker_id={$worker_id}&to_connection_id={$connection_id}&content={$content} 向某个worker进程中的某个客户端连接推送$content数据

注意:测试时把 {$worker_id} {$connection_id}{$content} 换成实际值