update to 1.5 version

This commit is contained in:
whale
2020-06-05 13:36:30 +08:00
parent a8183757be
commit 59fde3d075
17 changed files with 202 additions and 50 deletions

View File

@@ -4,6 +4,7 @@
namespace ZM\Utils;
use Framework\Console;
use Swlib\Saber;
use Swoole\Coroutine\Http\Client;
@@ -11,18 +12,23 @@ class ZMRequest
{
/**
* 使用Swoole协程客户端发起HTTP GET请求
* @version 1.1
* 返回请求后的body
* 如果请求失败或返回状态不是200则返回 false
* @param $url
* @param array $headers
* @param array $set
* @param bool $return_body
* @return bool|string|Client
* @version 1.1
* 返回请求后的body
* 如果请求失败或返回状态不是200则返回 false
*/
public static function get($url, $headers = [], $set = [], $return_body = true) {
$parse = parse_url($url);
$cli = new Client($parse["host"], ($parse["scheme"] == "https" ? 443 : (isset($parse["port"]) ? $parse["port"] : 80)), ($parse["scheme"] == "https" ? true : false));
if (!isset($parse["host"])) {
Console::warning("ZMRequest: url must contains scheme such as \"http(s)\"");
return false;
}
$port = $parse["port"] ?? (($parse["scheme"] ?? "http") == "https" ? 443 : 80);
$cli = new Client($parse["host"], $port, (($parse["scheme"] ?? "http") == "https" ? true : false));
$cli->setHeaders($headers);
$cli->set($set == [] ? ['timeout' => 15.0] : $set);
$cli->get($parse["path"] . (isset($parse["query"]) ? "?" . $parse["query"] : ""));
@@ -50,7 +56,12 @@ class ZMRequest
*/
public static function post($url, array $header, $data, $set = [], $return_body = true) {
$parse = parse_url($url);
$cli = new Client($parse["host"], ($parse["scheme"] == "https" ? 443 : (isset($parse["port"]) ? $parse["port"] : 80)), ($parse["scheme"] == "https" ? true : false));
if (!isset($parse["host"])) {
Console::warning("ZMRequest: url must contains scheme such as \"http(s)://\"");
return false;
}
$port = $parse["port"] ?? (($parse["scheme"] ?? "http") == "https" ? 443 : 80);
$cli = new Client($parse["host"], $port, (($parse["scheme"] ?? "http") == "https" ? true : false));
$cli->set($set == [] ? ['timeout' => 15.0] : $set);
$cli->setHeaders($header);
$cli->post($parse["path"] . (isset($parse["query"]) ? ("?" . $parse["query"]) : ""), $data);
@@ -65,6 +76,17 @@ class ZMRequest
}
}
/**
* @param $url
* @param array $set
* @param array $header
* @return ZMWebSocket
* @since 1.5
*/
public static function websocket($url, $set = ['websocket_mask' => true], $header = []) {
return new ZMWebSocket($url, $set, $header);
}
/**
* @param $option
* @return Saber
@@ -72,4 +94,4 @@ class ZMRequest
public static function session($option) {
return Saber::session($option);
}
}
}

View File

@@ -0,0 +1,105 @@
<?php
namespace ZM\Utils;
use Framework\Console;
use Swoole\Coroutine\Http\Client;
use Swoole\WebSocket\Frame;
/**
* Class ZMWebSocket
* @package ZM\Utils
* @since 1.5
*/
class ZMWebSocket
{
private $parse;
private $client;
public $is_available = false;
private $close_func;
private $message_func;
public function __construct($url, $set = ['websocket_mask' => true], $header = []) {
$this->parse = parse_url($url);
if (!isset($this->parse["host"])) {
Console::warning("ZMRequest: url must contains scheme such as \"ws(s)://\"");
return;
}
$port = $this->parse["port"] ?? (($this->parse["scheme"] ?? "ws") == "wss" ? 443 : 80);
$this->client = new Client($this->parse["host"], $port, (($this->parse["scheme"] ?? "ws") == "wss" ? true : false));
$this->client->set($set);
if ($header != []) $this->client->setHeaders($header);
$this->is_available = true;
}
/**
* @return bool
*/
public function upgrade() {
if (!$this->is_available) return false;
$r = $this->client->upgrade($this->parse["path"] . (isset($this->parse["query"]) ? ("?" . $this->parse["query"]) : ""));
if ($r) {
go(function () {
while (true) {
$result = $this->client->recv(60);
if ($result === false) {
if ($this->client->connected === false) {
go(function () {
call_user_func($this->close_func, $this->client);
});
break;
}
} elseif ($result instanceof Frame) {
go(function () use ($result) {
$this->is_available = false;
call_user_func($this->message_func, $result, $this->client);
});
}
}
});
return true;
}
return false;
}
/**
* @param callable $callable
* @return $this
*/
public function onMessage(callable $callable) {
$this->message_func = $callable;
return $this;
}
/**
* @param callable $callable
* @return $this
*/
public function onClose(callable $callable) {
$this->close_func = $callable;
return $this;
}
}
if(!debug_backtrace()) {
go(function () {
require_once __DIR__ . "/../../Framework/Console.php";
$cli = new ZMWebSocket("ws://127.0.0.1:20001/");
if (!$cli->is_available) die("Error!\n");
$cli->onMessage(function ($frame){
var_dump($frame);
});
$cli->onClose(function($client){
echo "Connection closed.\n";
});
if($cli->upgrade()){
echo "成功连接!\n";
} else {
echo "连接失败!\n";
}
});
}