PushMessageService.php 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: gevnc
  5. * Date: 2018/8/16
  6. * Time: 14:03
  7. */
  8. namespace App\Services;
  9. use App\Traits\Singleton;
  10. use Illuminate\Support\Facades\Redis;
  11. class PushMessageService
  12. {
  13. use Singleton;
  14. const PUSH_QUEUE_DELAY = "bs_push_queue:delay";
  15. /**
  16. * Websocket 消息发送接口
  17. * @param $channel
  18. * @param $action
  19. * @param $data
  20. * @param $userId
  21. * @param int $delay
  22. * @return null
  23. */
  24. public function pushByUserId($channel, $action, $data, $userId, $delay = 0)
  25. {
  26. $value = [
  27. "key" => uniqid(),
  28. "channel" => $channel,
  29. "action" => $action,
  30. "data" => $data,
  31. "user_id" => $userId
  32. ];
  33. $userOnlineInfo = UserService::getInstance()->getOnlineInfo($userId);
  34. if(isset($userOnlineInfo["fd"])){
  35. if($userOnlineInfo["fd"]>0){
  36. $server = $userOnlineInfo["server"];
  37. $score = time() + $delay;
  38. return Redis::zAdd(self::PUSH_QUEUE_DELAY . ":" . $server, $score, json_encode($value));
  39. }else{
  40. return null;
  41. }
  42. }else{
  43. return null;
  44. }
  45. }
  46. }