ChatService.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Services\Socket;
  3. use App\Services\RedisService;
  4. use Ratchet\MessageComponentInterface;
  5. use Ratchet\ConnectionInterface;
  6. class ChatService implements MessageComponentInterface
  7. {
  8. protected $clients;
  9. public function __construct()
  10. {
  11. $this->clients = new \SplObjectStorage;
  12. }
  13. public function onOpen(ConnectionInterface $conn)
  14. {
  15. $this->clients->attach($conn);
  16. echo "Socket:创建【{$conn->resourceId}】连接成功! \n";
  17. }
  18. public function onMessage(ConnectionInterface $from, $msg)
  19. {
  20. echo "Socket:".$msg.'++'.$from->resourceId."\n";
  21. $fromId = $from->resourceId;
  22. $data = $msg? json_decode($msg, true) : [];
  23. $type = isset($data['type'])? $data['type'] : '';
  24. $ct = isset($data['ct'])? $data['ct'] : '';
  25. $uuid = isset($data['uuid'])? $data['uuid'] : uniqid();
  26. $fromData = isset($data['data'])? $data['data'] : '';
  27. $date = date('Y-m-d H:i:s');
  28. switch($type){
  29. case 'bind':
  30. if($ct){
  31. RedisService::set("caches:server:{$fromId}", ['uuid'=>$uuid,'fromId'=>$fromId,'date'=>$date], 7 * 3600);
  32. }
  33. RedisService::set("caches:{$uuid}:{$type}", ['uuid'=>$uuid,'qrcode'=>$fromData,'formId'=>$fromId,'date'=>$date], 7 * 24 * 3600);
  34. //RedisService::set("caches:{$uuid}:qrcode", ['uuid'=>$uuid,'qrcode'=>$formData,'formId'=>$formId,'date'=>$date], 7 * 24 * 3600);
  35. $from->send(json_encode(['type'=>'bind','result'=>'ok']));
  36. break;
  37. case 'login-bind':
  38. var_dump($this->clients);
  39. RedisService::set("caches:binds:login", ['uuid'=>$uuid,'data'=>$fromData,'formId'=>$fromId,'date'=>$date], 7 * 24 * 3600);
  40. RedisService::set("caches:{$uuid}:{$type}", ['uuid'=>$uuid,'data'=>$fromData,'formId'=>$fromId,'date'=>$date], 7 * 24 * 3600);
  41. foreach ($this->clients as $conn){
  42. if(RedisService::get("caches:server:".$conn->resourceId)){
  43. $conn->send(json_encode(['type'=>'login-load','uuid'=>$uuid,'mid'=>$conn->resourceId,'data'=> $fromData,'t'=>time()], 256));
  44. }
  45. }
  46. break;
  47. default:
  48. RedisService::set("caches:{$uuid}:{$type}", ['uuid'=>$uuid,'data'=>$fromData,'formId'=>$fromId,'date'=>$date], 7 * 24 * 3600);
  49. //RedisService::set("caches:{$uuid}:qrcode", ['uuid'=>$uuid,'qrcode'=>$formData,'formId'=>$formId,'date'=>$date], 7 * 24 * 3600);
  50. break;
  51. }
  52. $from->send(json_encode(['type'=> $type,'result'=>'ok','t'=>time()], 256));
  53. }
  54. public function onClose(ConnectionInterface $conn)
  55. {
  56. $this->clients->detach($conn);
  57. RedisService::keyDel("caches:server:".$conn->resourceId);
  58. echo "Socket:Connection {$conn->resourceId} has disconnected\n";
  59. }
  60. public function onError(ConnectionInterface $conn, \Exception $e)
  61. {
  62. echo "Socket:An error has occurred: {$e->getMessage()}\n";
  63. $conn->close();
  64. }
  65. }