// +---------------------------------------------------------------------- namespace app\webscoket; use app\services\user\UserServices; use qiniu\utils\Arr; use think\facade\Log; /** * socket 事件基础类 * Class BaseHandler * @package app\webscoket */ abstract class BaseHandler { /** * @var Manager */ protected $manager; /** * @var Room */ protected $room; /** * @var int */ protected $fd; /** * 用户聊天端 * @var int|null */ protected $formType; /** * 登陆 * @param array $data * @param Response $response * @return mixed */ abstract public function login(array $data, Response $response); /** * 事件入口 * @param $event * @return null |null */ public function handle($event) { [$method, $result, $manager, $room] = $event; $this->manager = $manager; $this->room = $room; $this->fd = array_shift($result); $this->formType = array_shift($result); if (method_exists($this, $method)) { if ($method == 'login' && is_string($result[0])) { $result[0] = ['token' => $result[0]]; } return $this->{$method}(...$result); } else { Log::error('socket 回调事件' . $method . '不存在,消息内容为:' . json_encode($result)); return null; } } /** * 聊天事件 * @param array $data * @param Response $response */ abstract public function chat(array $data, Response $response); /** * 切换用户聊天 * @param array $data * @param Response $response */ abstract public function to_chat(array $data, Response $response); /** * 测试原样返回 * @param array $data * @param Response $response * @return bool|\think\response\Json|null */ public function test(array $data, Response $response) { return $response->success($data); } /** * 关闭连接触发 * @param array $data * @param Response $response */ public function close(array $data, Response $response) { $uid = $data['uid'] ?? 0; if ($uid) { } } }