This commit is contained in:
2020-11-04 19:28:40 +08:00
parent dc2216a8b9
commit d533102a77
4 changed files with 124 additions and 69 deletions
+6 -34
View File
@@ -6,11 +6,11 @@ namespace HttpServer\Events;
use Closure;
use HttpServer\Abstracts\Callback;
use Snowflake\Core\JSON;
use Snowflake\Event;
use Snowflake\Snowflake;
use Swoole\Server;
use Exception;
use HttpServer\Events\Utility\DataResolve;
/**
* Class OnPacket
@@ -28,35 +28,6 @@ class OnPacket extends Callback
public ?Closure $pack = null;
/**
* @param $data
* @return mixed
* @throws Exception
*/
public function pack($data)
{
$callback = $this->pack;
if (is_callable($callback, true)) {
return $callback($data);
}
return JSON::encode($data);
}
/**
* @param $data
* @return mixed
*/
public function unpack($data)
{
$callback = $this->unpack;
if (is_callable($callback, true)) {
return $callback($data);
}
return JSON::decode($data);
}
/**
* @param Server $server
* @param $data
@@ -64,17 +35,18 @@ class OnPacket extends Callback
* @return mixed
* @throws Exception
*/
public function onHandler(Server $server,string $data,array $clientInfo)
public function onHandler(Server $server, string $data, array $clientInfo)
{
try {
$client = [$clientInfo['address'], $clientInfo['port']];
if (empty($data = $this->unpack($data))) {
$data = DataResolve::unpack($this->unpack, $clientInfo['address'], $clientInfo['port'], $data);
if (empty($data)) {
throw new Exception('Format error.');
}
$client[] = $this->pack($data);
$client[] = DataResolve::pack($this->pack, $data);
return $server->sendto(...$client);
} catch (\Throwable $exception) {
$client[] = $this->pack(['message' => $exception->getMessage()]);
$client[] = DataResolve::pack($this->pack,['message' => $exception->getMessage()]);
return $server->sendto(...$client);
} finally {
$event = Snowflake::app()->event;