walkor 2 yıl önce
ebeveyn
işleme
e36a75e914
2 değiştirilmiş dosya ile 19 ekleme ve 4 silme
  1. 10 2
      src/Protocols/Websocket.php
  2. 9 2
      src/Protocols/Ws.php

+ 10 - 2
src/Protocols/Websocket.php

@@ -14,6 +14,7 @@
 
 namespace Workerman\Protocols;
 
+use Exception;
 use Throwable;
 use Workerman\Connection\ConnectionInterface;
 use Workerman\Connection\TcpConnection;
@@ -22,6 +23,8 @@ use Workerman\Worker;
 use function base64_encode;
 use function chr;
 use function floor;
+use function gettype;
+use function is_scalar;
 use function ord;
 use function pack;
 use function preg_match;
@@ -230,12 +233,17 @@ class Websocket
     /**
      * Websocket encode.
      *
-     * @param string $buffer
+     * @param mixed $buffer
      * @param TcpConnection $connection
      * @return string
+     * @throws Exception
      */
-    public static function encode(string $buffer, TcpConnection $connection): string
+    public static function encode(mixed $buffer, TcpConnection $connection): string
     {
+        if (!is_scalar($buffer)) {
+            throw new Exception("You can't send(" . gettype($buffer) . ") to client, you need to convert it to string. ");
+        }
+
         $len = strlen($buffer);
         if (empty($connection->websocketType)) {
             $connection->websocketType = static::BINARY_TYPE_BLOB;

+ 9 - 2
src/Protocols/Ws.php

@@ -14,6 +14,7 @@
 
 namespace Workerman\Protocols;
 
+use Exception;
 use Throwable;
 use Workerman\Connection\AsyncTcpConnection;
 use Workerman\Connection\ConnectionInterface;
@@ -22,7 +23,9 @@ use Workerman\Worker;
 use function base64_encode;
 use function bin2hex;
 use function floor;
+use function gettype;
 use function is_array;
+use function is_scalar;
 use function ord;
 use function pack;
 use function preg_match;
@@ -225,13 +228,17 @@ class Ws
     /**
      * Websocket encode.
      *
-     * @param string $payload
+     * @param mixed $payload
      * @param AsyncTcpConnection $connection
      * @return string
      * @throws Throwable
      */
-    public static function encode(string $payload, AsyncTcpConnection $connection): string
+    public static function encode(mixed $payload, AsyncTcpConnection $connection): string
     {
+        if (!is_scalar($payload)) {
+            throw new Exception("You can't send(" . gettype($payload) . ") to client, you need to convert it to string. ");
+        }
+
         if (empty($connection->websocketType)) {
             $connection->websocketType = self::BINARY_TYPE_BLOB;
         }