vali il y a 6 ans
Parent
commit
70dafb60dd

+ 14 - 16
websockServ/app/lib/wclient/PacketHandler.php

@@ -1,5 +1,7 @@
 <?php
-namespace  app\lib\wclient;
+
+namespace app\lib\wclient;
+
 /**
  * Created by PhpStorm.
  * User: marsnowxiao
@@ -8,7 +10,8 @@ namespace  app\lib\wclient;
  */
 use app\lib\wclient\WebSocketFrame as WebSocketFrame;
 
-class PacketHandler {
+class PacketHandler
+{
     const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
     const TOKEN_LENGHT = 16;
     const maxPacketSize = 2000000;
@@ -32,15 +35,15 @@ class PacketHandler {
         return base64_encode($randomString);
     }
 
-    public function buildHandShakeRequest($host, $port)
+    public function buildHandShakeRequest($host, $port, $getParas)
     {
         $this->key = static::generateToken(self::TOKEN_LENGHT);
 
-        return "GET / HTTP/1.1" . "\r\n" .
+        return "GET /{$getParas} HTTP/1.1" . "\r\n" .
             "Origin: null" . "\r\n" .
             "Host: {$host}:{$port}" . "\r\n" .
             "Sec-WebSocket-Key: {$this->key}" . "\r\n" .
-            "User-Agent: SwooleWebsocketClient"."/0.1.4" . "\r\n" .
+            "User-Agent: SwooleWebsocketClient" . "/0.1.4" . "\r\n" .
             "Upgrade: Websocket" . "\r\n" .
             "Connection: Upgrade" . "\r\n" .
             "Sec-WebSocket-Protocol: wamp" . "\r\n" .
@@ -60,7 +63,7 @@ class PacketHandler {
             }
         }
 
-        return (isset($headerInfo['Sec-WebSocket-Accept']) && $headerInfo['Sec-WebSocket-Accept'] == base64_encode(pack('H*', sha1($this->key.self::GUID))));
+        return (isset($headerInfo['Sec-WebSocket-Accept']) && $headerInfo['Sec-WebSocket-Accept'] == base64_encode(pack('H*', sha1($this->key . self::GUID))));
     }
 
     public function processDataFrame(&$packet)
@@ -87,33 +90,28 @@ class PacketHandler {
         $length = $handle & 0x7f;
         $index++;
         //126 short
-        if ($length == 0x7e)
-        {
+        if ($length == 0x7e) {
             if (strlen($packet) < $index + 2)
                 return null;
             //2 byte
             $handle = unpack('nl', substr($packet, $index, 2));
             $index += 2;
             $length = $handle['l'];
-        }
-        //127 int64
-        elseif ($length > 0x7e)
-        {
+        } //127 int64
+        elseif ($length > 0x7e) {
             if (strlen($packet) < $index + 8)
                 return null;
             //8 byte
             $handle = unpack('Nh/Nl', substr($packet, $index, 8));
             $index += 8;
             $length = $handle['l'];
-            if ($length > static::maxPacketSize)
-            {
+            if ($length > static::maxPacketSize) {
                 throw new \Exception("frame length is too big.\n");
             }
         }
 
         //mask-key: int32
-        if ($mask)
-        {
+        if ($mask) {
             if (strlen($packet) < $index + 4)
                 return null;
             $mask = array_map('ord', str_split(substr($packet, $index, 4)));

+ 6 - 4
websockServ/app/lib/wclient/WebSocketClient.php

@@ -11,6 +11,7 @@ class WebSocketClient
     private $state;
     private $host;
     private $port;
+    private $getParas;
     private $handler;
     private $buffer;
     private $openCb;
@@ -41,17 +42,18 @@ class WebSocketClient
         $this->buffer = "";
     }
 
-    public function connect($host, $port)
+    public function connect($host, $port, $getParas = '', $timeout = 0.5, $flag = 0)
     {
         $this->host = $host;
         $this->port = $port;
-        $this->client->connect($host, $port);
+        $this->getParas = $getParas;
+        $this->client->connect($host, $port, $timeout, $flag);
     }
 
     public function sendHandShake()
     {
         $this->state = static::HANDSHAKING;
-        $request = $this->handler->buildHandShakeRequest($this->host, $this->port);
+        $request = $this->handler->buildHandShakeRequest($this->host, $this->port, $this->getParas);
         $this->client->send($request);
     }
 
@@ -150,7 +152,7 @@ class WebSocketClient
                 return;
         }
         $data = \swoole_websocket_server::pack($data, $_type);
-        $this->client->send($data);
+        return $this->client->send($data);
     }
 
     public function getTcpClient()

+ 27 - 4
websockServ/commands/clientws1.php

@@ -14,12 +14,21 @@ use  app\lib\wclient\WebSocketClient ;
 
 
 $client = new WebSocketClient();
+$timerId = 0 ;
 
-$client->on("open",function ($client) {
+$client->on("open",function ($client) use ($timerId)  {
     $fd = $client->getTcpClient()->sock;
     echo "fd: $fd is open\n";
     $msg = '{"cmd":"test","act":"broad","data":"你好,朋友们!","time":1558492565,"token":"aasasfa"}';
-    $client->send($msg);
+
+    $timerId = Swoole\Timer::tick(3000,function()use($msg,$client){
+           $ret = $client->send($msg);
+           if (!$ret){
+               $client->close();
+       }
+    });
+    setTimeid($timerId);
+
 });
 
 $client->on("message", function ($client, $frame) {
@@ -28,12 +37,26 @@ $client->on("message", function ($client, $frame) {
 });
 
 
-$client->on("close", function ($client) {
+$client->on("close", function ($client)use ($timerId) {
+    if (getTimeid()){
+        Swoole\Timer::clear(getTimeid());
+    }
     $fd = $client->getTcpClient()->sock;
     echo "fd: $fd is closed\n";
 });
 
-$client->connect("192.168.2.200", 9090);
+function  setTimeid($id){
+    global  $timerId ;
+    $timerId = $id ;
+}
+
+function  getTimeid(){
+    global  $timerId ;
+    return $timerId  ;
+}
+
+$url = "?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJib3JuIiwiaWF0IjoxNTU5MDI3Mzk1LCJleHAiOjE1NTkwMzQ1OTUsIm5iZiI6MTU1OTAyNzQ1NSwic3ViIjoxMCwianRpIjoiOGVlODI5Y2QtNjM3Mi1iNjUyLTYxYWItMWY3YWViYTNhODM0In0.eLuHFc5uhTAc87DVCAnTKzRloZS0n79jYWTYE9ea7E0";
+$client->connect("192.168.2.200", 9090, $url , 0.5 , true);
 
 
 

+ 2 - 1
websockServ/commands/clientws2.php

@@ -15,7 +15,8 @@ require __DIR__.'/../vendor/autoload.php';
 
 use WebSocket\Client as Client;
 
-$client = new Client("ws://192.168.2.200:9090");
+$url = "?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJib3JuIiwiaWF0IjoxNTU5MDI3Mzk1LCJleHAiOjE1NTkwMzQ1OTUsIm5iZiI6MTU1OTAyNzQ1NSwic3ViIjoxMCwianRpIjoiOGVlODI5Y2QtNjM3Mi1iNjUyLTYxYWItMWY3YWViYTNhODM0In0.eLuHFc5uhTAc87DVCAnTKzRloZS0n79jYWTYE9ea7E0";
+$client = new Client("ws://192.168.2.200:9090/".$url);
 
 
 $str = '{"cmd":"test","act":"broad","data":"你好,朋友们!","time":1558492565,"token":"aasasfa"}';

+ 1 - 2
websockServ/commands/mysql_poole_client_test.php

@@ -24,7 +24,7 @@ if (!$rts){
     die("连接发生错误,操作中止\n");
 }
 
-$sql =("select  * from members where id=57");
+$sql =("select  * from members where id=57111");
 $client->send($sql);
 $resdata = $client->recv();
 print_r(json_decode($resdata,true)) ;
@@ -37,7 +37,6 @@ print_r(json_decode($resdata,true)) ;
 
 $sql ="select  * from \"districts\" where id=1";
 $client->send($sql);
-$client->waitall();
 $resdata = $client->recv();
 print_r(json_decode($resdata,true)) ;