mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 23:55:35 +08:00
initial commit
This commit is contained in:
23
src/ZM/Connection/CQConnection.php
Normal file
23
src/ZM/Connection/CQConnection.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Connection;
|
||||
|
||||
|
||||
class CQConnection extends WSConnection
|
||||
{
|
||||
public $self_id = null;
|
||||
|
||||
public function __construct($server, $fd, $self_id) {
|
||||
parent::__construct($server, $fd);
|
||||
$this->self_id = $self_id;
|
||||
}
|
||||
|
||||
public function getQQ(){
|
||||
return $this->self_id;
|
||||
}
|
||||
|
||||
public function getType() {
|
||||
return "qq";
|
||||
}
|
||||
}
|
||||
73
src/ZM/Connection/ConnectionManager.php
Normal file
73
src/ZM/Connection/ConnectionManager.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Connection;
|
||||
|
||||
|
||||
use Framework\ZMBuf;
|
||||
|
||||
class ConnectionManager
|
||||
{
|
||||
/**
|
||||
* 通过server的fd获取WSConnection实例化对象
|
||||
* @param int $fd
|
||||
* @return WSConnection
|
||||
*/
|
||||
public static function get(int $fd) {
|
||||
foreach (ZMBuf::$connect as $v) {
|
||||
if ($v->fd == $fd) return $v;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @param array $option
|
||||
* @return WSConnection[]
|
||||
*/
|
||||
public static function getByType(string $type, $option = []) {
|
||||
$conn = [];
|
||||
foreach (ZMBuf::$connect as $v) {
|
||||
foreach ($option as $ks => $vs) {
|
||||
if (($v->$ks ?? "") == $vs) continue;
|
||||
else continue 2;
|
||||
}
|
||||
if ($v->getType() == $type) $conn[] = $v;
|
||||
}
|
||||
return $conn;
|
||||
}
|
||||
|
||||
public static function getTypeClassName(string $type) {
|
||||
switch (strtolower($type)) {
|
||||
case "qq":
|
||||
case "universal":
|
||||
return CQConnection::class;
|
||||
case "webconsole":
|
||||
return WCConnection::class;
|
||||
case "proxy":
|
||||
return ProxyConnection::class;
|
||||
default:
|
||||
foreach (ZMBuf::$custom_connection_class as $v) {
|
||||
/** @var WSConnection $r */
|
||||
$r = new $v(ZMBuf::$server, -1);
|
||||
if ($r->getType() == strtolower($type)) return $v;
|
||||
}
|
||||
return UnknownConnection::class;
|
||||
}
|
||||
}
|
||||
|
||||
public static function close($fd) {
|
||||
foreach (ZMBuf::$connect as $k => $v) {
|
||||
if ($v->fd == $fd) {
|
||||
ZMBuf::$server->close($fd);
|
||||
unset(ZMBuf::$connect[$k]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function registerCustomClass() {
|
||||
$classes = getAllClasses(WORKING_DIR . "/src/Custom/Connection/", "Custom\\Connection");
|
||||
ZMBuf::$custom_connection_class = $classes;
|
||||
}
|
||||
}
|
||||
13
src/ZM/Connection/ProxyConnection.php
Normal file
13
src/ZM/Connection/ProxyConnection.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Connection;
|
||||
|
||||
|
||||
class ProxyConnection extends WSConnection
|
||||
{
|
||||
|
||||
public function getType() {
|
||||
return "proxy";
|
||||
}
|
||||
}
|
||||
13
src/ZM/Connection/UnknownConnection.php
Normal file
13
src/ZM/Connection/UnknownConnection.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Connection;
|
||||
|
||||
|
||||
class UnknownConnection extends WSConnection
|
||||
{
|
||||
|
||||
public function getType() {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
10
src/ZM/Connection/WCConnection.php
Normal file
10
src/ZM/Connection/WCConnection.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Connection;
|
||||
|
||||
|
||||
class WCConnection extends WSConnection
|
||||
{
|
||||
public function getType() { return "wc"; }
|
||||
}
|
||||
52
src/ZM/Connection/WSConnection.php
Normal file
52
src/ZM/Connection/WSConnection.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Connection;
|
||||
|
||||
|
||||
use Framework\Console;
|
||||
use Framework\Logger;
|
||||
use swoole_websocket_server;
|
||||
|
||||
abstract class WSConnection
|
||||
{
|
||||
public $fd;
|
||||
|
||||
/** @var swoole_websocket_server */
|
||||
protected $server;
|
||||
|
||||
public $available = false;
|
||||
|
||||
public function __construct($server, $fd) {
|
||||
$this->server = $server;
|
||||
$this->fd = $fd;
|
||||
}
|
||||
|
||||
public abstract function getType();
|
||||
|
||||
public function exists() {
|
||||
return $this->available = $this->server->exist($this->fd);
|
||||
}
|
||||
|
||||
public function close() {
|
||||
ConnectionManager::close($this->fd);
|
||||
}
|
||||
|
||||
public function push($data, $push_error_record = true) {
|
||||
if ($data === null || $data == "") {
|
||||
Console::warning("推送了空消息");
|
||||
return false;
|
||||
}
|
||||
if (!$this->server->exist($this->fd)) {
|
||||
Console::warning("Swoole 原生 websocket连接池中无此连接");
|
||||
return false;
|
||||
}
|
||||
if ($this->server->push($this->fd, $data) === false) {
|
||||
$data = unicode_decode($data);
|
||||
if ($push_error_record) Logger::writeSwooleLog("API push failed. Data: " . $data);
|
||||
Console::error("websocket数据未成功推送,长度:" . strlen($data));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user