<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/23
 * Time: 17:42
 */

define('START_TIME', microtime(true));
require __DIR__ . '/../vendor/autoload.php';
\app\lib\boot::init();

use  app\lib\wclient\WebSocketClient ;


$client = new WebSocketClient();
$timerId = 0 ;

$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"}';

    $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) {
    $fd = $client->getTcpClient()->sock;
    echo "fd: $fd received: {$frame->data}\n";
});


$client->on("close", function ($client)use ($timerId) {
    if (getTimeid()){
        Swoole\Timer::clear(getTimeid());
    }
    $fd = $client->getTcpClient()->sock;
    echo "fd: $fd is closed\n";
});

function  setTimeid($id){
    global  $timerId ;
    $timerId = $id ;
}

function  getTimeid(){
    global  $timerId ;
    return $timerId  ;
}
$config = \app\lib\GlobConfigs::getKey('admin_conf');
$url = "?token=".$config['admin_token'];
$client->connect("192.168.2.200", 9090, $url , 0.5 , true);






