walkor 10 年之前
父节点
当前提交
64479e5247
共有 1 个文件被更改,包括 12 次插入4 次删除
  1. 12 4
      Workerman/Protocols/Websocket.php

+ 12 - 4
Workerman/Protocols/Websocket.php

@@ -135,7 +135,15 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
     public static function encode($buffer, ConnectionInterface $connection)
     public static function encode($buffer, ConnectionInterface $connection)
     {
     {
         $len = strlen($buffer);
         $len = strlen($buffer);
-        $first_byte = $connection->protocolData['binaryType'];
+        // 还没握手不能发数据
+        if(empty($connection->handshake))
+        {
+            $connection->send("HTTP/1.1 400 Bad Request\r\n\r\n<b>400 Bad Request</b><br>Send data before handshake. ", true);
+            $connection->close();
+            return false;
+        }
+       $first_byte = $connection->websocketType;
+        
         if($len<=125)
         if($len<=125)
         {
         {
             return $first_byte.chr($len).$buffer;
             return $first_byte.chr($len).$buffer;
@@ -215,9 +223,8 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
             $connection->handshake = true;
             $connection->handshake = true;
             $connection->consumeRecvBuffer(strlen($buffer));
             $connection->consumeRecvBuffer(strlen($buffer));
             $connection->send($new_message, true);
             $connection->send($new_message, true);
-            $connection->protocolData = array(
-                    'binaryType' => self::BINARY_TYPE_BLOB, // blob or arraybuffer
-            );
+            // blob or arraybuffer
+            $connection->websocketType = self::BINARY_TYPE_BLOB; 
             // 如果有设置onWebSocketConnect回调,尝试执行
             // 如果有设置onWebSocketConnect回调,尝试执行
             if(isset($connection->onWebSocketConnect))
             if(isset($connection->onWebSocketConnect))
             {
             {
@@ -243,6 +250,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
             return 0;
             return 0;
         }
         }
         // 出错
         // 出错
+        $connection->send("HTTP/1.1 400 Bad Request\r\n\r\n<b>400 Bad Request</b><br>Invalid handshake data for websocket. ", true);
         $connection->close();
         $connection->close();
         return 0;
         return 0;
     }
     }