CmdInf.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\logic\cmdpro;
  3. /**
  4. * Created by PhpStorm.
  5. * User: Administrator
  6. * Date: 2019/5/21
  7. * Time: 16:48
  8. */
  9. /*
  10. Swoole\Server\Task Object
  11. (
  12. [data] => Swoole\WebSocket\Frame Object
  13. (
  14. [fd] => 1
  15. [data] => {'cmd':'xxx','act'=>'xxx','data':'xxxx','time':xxxx,'token':xxx}
  16. [opcode] => 1
  17. [finish] => 1
  18. )
  19. [id] => 0
  20. [worker_id] => 2
  21. [flags] => 6
  22. )
  23. data:
  24. {'cmd':'xxx','act'=>'xxx','data':'xxxx','time':xxxx,'token':xxx}
  25. **/
  26. use app\lib\DataPack;
  27. use app\lib\Wlog;
  28. use app\lib\GlobConfigs;
  29. class CmdInf
  30. {
  31. public function paraCmd($serv, \Swoole\Server\Task $task)
  32. {
  33. $fd = $task->data->fd;
  34. $dataPack = json_decode($task->data->data, true);
  35. $method = isset($dataPack['act']) ? $dataPack['act'] : '';
  36. if (empty($method) || !(method_exists($this, $method))) {
  37. $this->sendTo($serv, $fd, DataPack::toJson(['mtype' => 'error', 'data' => '无效的参数']), true);
  38. return;
  39. }
  40. try {
  41. return $this->$method($serv, $task);
  42. } catch (\Exception $e) {
  43. echo "发生异常或错误:" . $e->getFile() . ' ' . $e->getLine() . ' ' . $e->getCode() . ' ' . $e->getMessage() . "\r\n";
  44. return;
  45. }
  46. }
  47. //单发
  48. public function sendTo($serv, $fd, $dataString, $close = false)
  49. {
  50. $serv->push($fd, $dataString);
  51. if ($close) {
  52. $serv->disconnect($fd);
  53. }
  54. return true;
  55. }
  56. //在线广播
  57. public function onlineBroad($serv, $dataString)
  58. {
  59. foreach ($serv->connections as $fd) {
  60. $serv->push($fd, $dataString);
  61. }
  62. }
  63. //用uid发
  64. public function sendToUid($serv, $uid, $dataString, $close = false)
  65. {
  66. $fd = intval($serv->utable->get($uid,'fid'));
  67. if ($fd) {
  68. return $this->sendTo($serv, $fd, $dataString, $close);
  69. }
  70. return false;
  71. }
  72. }