Finished single websocket framework. It works.

完成了全反向websocket框架,全面支持多机器人
This commit is contained in:
jerry
2018-06-15 09:18:53 +08:00
parent ab6408e0eb
commit 2de4337600
17 changed files with 601 additions and 441 deletions

View File

@@ -10,10 +10,12 @@ class Group
{
private $group_id;
private $group_name;
private $self_id;
//private $prefix;
private $members = [];
public function __construct($group_id, $info) {
public function __construct($group_id, $info, $self_id) {
$this->self_id = $self_id;
$this->group_id = $group_id;
$this->group_name = $info["group_name"];
//$this->prefix = $info["prefix"];
@@ -82,9 +84,17 @@ class Group
* @param bool $with_members
*/
public function updateData($with_members = false) {
CQUtil::sendAPI(["action" => "get_group_list"], ["update_group_info", $this->getGroupId()]);
$connection = CQUtil::getApiConnectionByQQ($this->getSelfId());
CQUtil::sendAPI($connection->fd, ["action" => "get_group_list"], ["update_group_info", $this->getGroupId()]);
if ($with_members) {
CQUtil::sendAPI(["action" => "get_group_member_list", "params" => ["group_id" => $this->getGroupId()]], ["update_group_member_list", strval($this->getGroupId())]);
CQUtil::sendAPI($connection->fd, ["action" => "get_group_member_list", "params" => ["group_id" => $this->getGroupId()]], ["update_group_member_list", strval($this->getGroupId())]);
}
}
/**
* @return mixed
*/
public function getSelfId() {
return $this->self_id;
}
}

View File

@@ -65,7 +65,7 @@ class GroupMember extends User
* 返回用户是不是群管理员
* @return bool
*/
public function isAdmin(){
public function isAdmin() {
return in_array($this->getRole(), ["owner", "admin"]);
}
@@ -82,7 +82,7 @@ class GroupMember extends User
"card" => $card
]
];
CQUtil::sendAPI($data, []);
CQUtil::sendAPI(CQUtil::getApiConnectionByQQ($this->getGroup()->getSelfId())->fd, $data, []);
}
/**
@@ -123,9 +123,9 @@ class GroupMember extends User
/**
* 更新群组成员信息
*/
public function updateData(){
public function updateData() {
$user_id = $this->getId();
CQUtil::sendAPI([
CQUtil::sendAPI(CQUtil::getApiConnectionByQQ($this->getGroup()->getSelfId())->fd, [
"action" => "get_group_member_info",
"params" => [
"group_id" => $this->getGroup()->getGroupId(),

View File

@@ -0,0 +1,148 @@
<?php
/**
* Created by PhpStorm.
* User: jerry
* Date: 2018/6/13
* Time: 8:10 PM
*/
class WSConnection
{
public $fd;
/**
* 0 = event连接
* 1 = api连接
* 默认为event连接如果可以收到返回的get_status则标记为1
* @var int
*/
protected $type = 0;
protected $server;
/** @var WSConnection */
protected $pair = null;
protected $qq = "";
public function __construct(swoole_websocket_server $server, $fd) {
$this->server = $server;
$this->fd = $fd;
$this->manageType();
}
/**
* 返回swoole server
* @return swoole_websocket_server
*/
public function getServer() {
return $this->server;
}
/**
* 返回本连接是什么类型的
* @return int
*/
public function getType() {
return $this->type;
}
/**
* 用来确认此连接是API还是event
* 如果此fd连接是event则不会返回任何信息关于QQ的匹配则会在接收入第一条消息后设置
* 如果此连接是api则此操作后HTTP API会返回登录号的号码如果返回了则标记此连接为api并记录这个api连接属于的QQ号
*/
private function manageType() {
$this->server->push($this->fd, '{"action":"get_login_info","echo":{"type":"handshake"}}');
}
/**
* 返回此连接相关联的event连接使用前需初始化完成
* @return $this|null|WSConnection
*/
public function getEventConnection() {
switch ($this->type) {
case 0:
return $this;
case 1:
return $this->pair;
default:
return null;
}
}
/**
* 返回此链接相关联的api连接使用前需初始化完成
* @return $this|null|WSConnection
*/
public function getApiConnection() {
switch ($this->type) {
case 0:
return $this->pair;
case 1:
return $this;
default:
return null;
}
}
/**
* 检查此连接对应的QQ此部分较为复杂先留着
* @param $qq
*/
public function manageQQ($qq) {
//TODO
}
/**
* 返回此连接属于的QQ号
* @return string
*/
public function getQQ() {
return $this->qq;
}
/**
* 返回关联连接experiment
* @return WSConnection
*/
public function getPair() {
return $this->pair;
}
/**
* 设置关联连接
* @param WSConnection $pair
*/
public function setPair(WSConnection $pair) {
$this->pair = $pair;
}
/**
* @param string $qq
*/
public function setQQ($qq) {
$this->qq = $qq;
}
/**
* @param int $type
*/
public function setType(int $type) {
$this->type = $type;
}
/**
*
*/
public function findSub() {
if ($this->qq != "") {
foreach (CQUtil::getConnections() as $fd => $cn) {
if ($cn->getQQ() == $this->qq && $cn->getType() != $this->getType()) {
$this->setPair($cn);
$cn->setPair($this);
}
}
}
}
}