Sfoglia il codice sorgente

Merge pull request #759 from mouyong/master

添加对 Connection 的序列化操作
walkor 3 anni fa
parent
commit
b06f1fc4da
2 ha cambiato i file con 44 aggiunte e 2 eliminazioni
  1. 23 1
      src/Connection/TcpConnection.php
  2. 21 1
      src/Connection/UdpConnection.php

+ 23 - 1
src/Connection/TcpConnection.php

@@ -21,7 +21,7 @@ use Workerman\Worker;
 /**
  * TcpConnection.
  */
-class TcpConnection extends ConnectionInterface
+class TcpConnection extends ConnectionInterface implements \JsonSerializable
 {
     /**
      * Read buffer size.
@@ -969,6 +969,28 @@ class TcpConnection extends ConnectionInterface
     {
         static::$_enableCache = (bool)$value;
     }
+    
+    /**
+     * Get the json_encode informattion.
+     *
+     * @return string
+     */
+    public function jsonSerialize()
+    {        
+        return [
+            'id' => $this->id,
+            'status' => $this->getStatus(),
+            'transport' => $this->transport,
+            'getRemoteIp' => $this->getRemoteIp(),
+            'remotePort' => $this->getRemotePort(),
+            'getRemoteAddress' => $this->getRemoteAddress(),
+            'getLocalIp' => $this->getLocalIp(),
+            'getLocalPort' => $this->getLocalPort(),
+            'getLocalAddress' => $this->getLocalAddress(),
+            'isIpV4' => $this->isIpV4(),
+            'isIpV6' => $this->isIpV6(),
+        ];
+    }
 
     /**
      * Destruct.

+ 21 - 1
src/Connection/UdpConnection.php

@@ -17,7 +17,7 @@ namespace Workerman\Connection;
 /**
  * UdpConnection.
  */
-class UdpConnection extends ConnectionInterface
+class UdpConnection extends ConnectionInterface implements \JsonSerializable
 {
     /**
      * Application layer protocol.
@@ -206,4 +206,24 @@ class UdpConnection extends ConnectionInterface
     {
         return $this->_socket;
     }
+    
+    /**
+     * Get the json_encode informattion.
+     *
+     * @return string
+     */
+    public function jsonSerialize()
+    {
+        return [
+            'transport' => $this->transport,
+            'getRemoteIp' => $this->getRemoteIp(),
+            'remotePort' => $this->getRemotePort(),
+            'getRemoteAddress' => $this->getRemoteAddress(),
+            'getLocalIp' => $this->getLocalIp(),
+            'getLocalPort' => $this->getLocalPort(),
+            'getLocalAddress' => $this->getLocalAddress(),
+            'isIpV4' => $this->isIpV4(),
+            'isIpV6' => $this->isIpV6(),
+        ];
+    }
 }