Procházet zdrojové kódy

fix onWebSocketConnect

walkor před 10 roky
rodič
revize
95077899f8

+ 2 - 2
Applications/Todpole/start.php

@@ -25,8 +25,8 @@ $gateway->onConnect = function($connection)
     $connection->onWebSocketConnect = function($connection , $http_header)
     {
         // 可以在这里判断连接来源是否合法,不合法就关掉连接
-        // $_SERVER['SERVER_NAME']标识来自哪个站点的页面发起的websocket链接
-        if($_SERVER['SERVER_NAME'] != 'kedou.workerman.net')
+        // $_SERVER['HTTP_ORIGIN']标识来自哪个站点的页面发起的websocket链接
+        if($_SERVER['HTTP_ORIGIN'] != 'http://kedou.workerman.net')
         {
             $connection->close();
         }

+ 8 - 0
Workerman/Protocols/Websocket.php

@@ -199,6 +199,11 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
             {
                 $Sec_WebSocket_Key = $match[1];
             }
+            else
+            {
+                $connection->close("HTTP/1.1 400 Bad Request\r\n\r\n400 Bad Request");
+                return 0;
+            }
             $new_key = base64_encode(sha1($Sec_WebSocket_Key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11",true));
             // 握手返回的数据
             $new_message = "HTTP/1.1 101 Switching Protocols\r\n";
@@ -291,6 +296,9 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                 case 'referer':
                     $_SERVER['HTTP_REFERER'] = $value;
                     break;
+                case 'origin':
+                    $_SERVER['HTTP_ORIGIN'] = $value;
+                    break;
             }
         }