msg_push.php 881 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. echo "Sending text to msg queue.\n";
  3. class SwooleTask
  4. {
  5. protected $queueId;
  6. protected $workerId;
  7. function __construct($key, $workerId)
  8. {
  9. $this->queueId = msg_get_queue($key);
  10. if ($this->queueId === false)
  11. {
  12. throw new \Swoole\Exception("msg_get_queue() failed.");
  13. }
  14. $this->workerId = $workerId;
  15. }
  16. function dispatch($data)
  17. {
  18. if (!msg_send($this->queueId, $this->workerId + 1, Swoole\Server\Task::pack($data), false))
  19. {
  20. return false;
  21. }
  22. else
  23. {
  24. return true;
  25. }
  26. }
  27. }
  28. $task = new SwooleTask(0x70001001, 0);
  29. //普通字符串
  30. $task->dispatch("Hello from PHP!");
  31. //数组
  32. $task->dispatch(array('data' => str_repeat('A', 1024), 'type' => 1));
  33. //大包
  34. $task->dispatch(array('data' => str_repeat('B', 1024 * 32), 'type' => 2));