当前位置:   article > 正文

Yii2结合Workerman的websocket示例详解_yii websocket

yii websocket

1、安装workerman

composer require workerman/workerman

2、启动workerman

  1. ./yii workerman-web-socket -s restart -d
  2. ./yii workerman-web-socket -s reload
  3. ./yii workerman-web-socket -s status -g
  4. ./yii workerman-web-socket -s connections
  5. ./yii workerman-web-socket -s stop
  1. <?php
  2. /**
  3. * WorkmanWebSocket 服务相关
  4. */
  5. namespace app\commands;
  6. use Workerman\Worker;
  7. use yii\console\Controller;
  8. use yii\helpers\Console;
  9. /**
  10. *
  11. * WorkermanWebSocket
  12. *
  13. * @author durban.zhang <durban.zhang@gmail.com>
  14. */
  15. class WorkermanWebSocketController extends Controller
  16. {
  17. public $send;
  18. public $daemon;
  19. public $gracefully;
  20. // 这里不需要设置,会读取配置文件中的配置
  21. public $config = [];
  22. private $ip = '127.0.0.1';
  23. private $port = '2346';
  24. public function options($actionID)
  25. {
  26. return ['send', 'daemon', 'gracefully'];
  27. }
  28. public function optionAliases()
  29. {
  30. return [
  31. 's' => 'send',
  32. 'd' => 'daemon',
  33. 'g' => 'gracefully',
  34. ];
  35. }
  36. public function actionIndex()
  37. {
  38. if ('start' == $this->send) {
  39. try {
  40. $this->start($this->daemon);
  41. } catch (\Exception $e) {
  42. $this->stderr($e->getMessage() . "\n", Console::FG_RED);
  43. }
  44. } else if ('stop' == $this->send) {
  45. $this->stop();
  46. } else if ('restart' == $this->send) {
  47. $this->restart();
  48. } else if ('reload' == $this->send) {
  49. $this->reload();
  50. } else if ('status' == $this->send) {
  51. $this->status();
  52. } else if ('connections' == $this->send) {
  53. $this->connections();
  54. }
  55. }
  56. public function initWorker()
  57. {
  58. $ip = isset($this->config['ip']) ? $this->config['ip'] : $this->ip;
  59. $port = isset($this->config['port']) ? $this->config['port'] : $this->port;
  60. $wsWorker = new Worker("websocket://{$ip}:{$port}");
  61. // 4 processes
  62. $wsWorker->count = 4;
  63. // Emitted when new connection come
  64. $wsWorker->onConnect = function ($connection) {
  65. echo "New connection\n";
  66. };
  67. // Emitted when data received
  68. $wsWorker->onMessage = function ($connection, $data) {
  69. // Send hello $data
  70. $connection->send('dddd hello ' . $data);
  71. };
  72. // Emitted when connection closed
  73. $wsWorker->onClose = function ($connection) {
  74. echo "Connection closed\n";
  75. };
  76. }
  77. /**
  78. * workman websocket start
  79. */
  80. public function start()
  81. {
  82. $this->initWorker();
  83. // 重置参数以匹配Worker
  84. global $argv;
  85. $argv[0] = $argv[1];
  86. $argv[1] = 'start';
  87. if ($this->daemon) {
  88. $argv[2] = '-d';
  89. }
  90. // Run worker
  91. Worker::runAll();
  92. }
  93. /**
  94. * workman websocket restart
  95. */
  96. public function restart()
  97. {
  98. $this->initWorker();
  99. // 重置参数以匹配Worker
  100. global $argv;
  101. $argv[0] = $argv[1];
  102. $argv[1] = 'restart';
  103. if ($this->daemon) {
  104. $argv[2] = '-d';
  105. }
  106. if ($this->gracefully) {
  107. $argv[2] = '-g';
  108. }
  109. // Run worker
  110. Worker::runAll();
  111. }
  112. /**
  113. * workman websocket stop
  114. */
  115. public function stop()
  116. {
  117. $this->initWorker();
  118. // 重置参数以匹配Worker
  119. global $argv;
  120. $argv[0] = $argv[1];
  121. $argv[1] = 'stop';
  122. if ($this->gracefully) {
  123. $argv[2] = '-g';
  124. }
  125. // Run worker
  126. Worker::runAll();
  127. }
  128. /**
  129. * workman websocket reload
  130. */
  131. public function reload()
  132. {
  133. $this->initWorker();
  134. // 重置参数以匹配Worker
  135. global $argv;
  136. $argv[0] = $argv[1];
  137. $argv[1] = 'reload';
  138. if ($this->gracefully) {
  139. $argv[2] = '-g';
  140. }
  141. // Run worker
  142. Worker::runAll();
  143. }
  144. /**
  145. * workman websocket status
  146. */
  147. public function status()
  148. {
  149. $this->initWorker();
  150. // 重置参数以匹配Worker
  151. global $argv;
  152. $argv[0] = $argv[1];
  153. $argv[1] = 'status';
  154. if ($this->daemon) {
  155. $argv[2] = '-d';
  156. }
  157. // Run worker
  158. Worker::runAll();
  159. }
  160. /**
  161. * workman websocket connections
  162. */
  163. public function connections()
  164. {
  165. $this->initWorker();
  166. // 重置参数以匹配Worker
  167. global $argv;
  168. $argv[0] = $argv[1];
  169. $argv[1] = 'connections';
  170. // Run worker
  171. Worker::runAll();
  172. }
  173. }

workerman websocket支持的其他命令

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/309470
推荐阅读
相关标签
  

闽ICP备14008679号