SerializeTrait.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman\Connection\Traits;
  15. trait SerializeTrait
  16. {
  17. public function jsonSerialize()
  18. {
  19. $data = [];
  20. if ($this->transport === 'tcp') {
  21. $data = [
  22. 'id' => $this->id,
  23. 'status' => $this->getStatus(),
  24. ];
  25. }
  26. return $data + [
  27. 'transport' => $this->transport,
  28. 'getRemoteIp' => $this->getRemoteIp(),
  29. 'remotePort' => $this->getRemotePort(),
  30. 'getRemoteAddress' => $this->getRemoteAddress(),
  31. 'getLocalIp' => $this->getLocalIp(),
  32. 'getLocalPort' => $this->getLocalPort(),
  33. 'getLocalAddress' => $this->getLocalAddress(),
  34. 'isIpV4' => $this->isIpV4(),
  35. 'isIpV6' => $this->isIpV6(),
  36. ];
  37. }
  38. }