hyperf-chat/app/Controller/WebSocketController.php

104 lines
2.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface;
use Hyperf\Utils\Codec\Json;
use Phper666\JWTAuth\JWT;
use Swoole\Http\Request;
use Swoole\Websocket\Frame;
use Hyperf\Amqp\Producer;
use App\Amqp\Producer\ChatMessageProducer;
use Swoole\Http\Response;
use Swoole\WebSocket\Server;
use App\Traits\WebSocketTrait;
use App\Service\SocketFDService;
/**
* Class WebSocketController
* @package App\Controller
*/
class WebSocketController implements OnMessageInterface, OnOpenInterface, OnCloseInterface
{
use WebSocketTrait;
/**
* @Inject
* @var JWT
*/
private $jwt;
/**
* @inject
* @var SocketFDService
*/
private $socketFDService;
/**
* 连接创建成功回调事件
*
* @param Response|Server $server
* @param Request $request
*/
public function onOpen($server, Request $request): void
{
$token = $request->get['token'] ?? '';
$userInfo = $this->jwt->getParserData($token);
stdout_log()->notice("用户连接信息 : user_id:{$userInfo['user_id']} | fd:{$request->fd} | data:" . Json::encode($userInfo));
stdout_log()->notice('连接时间:' . date('Y-m-d H:i:s'));
// 绑定fd与用户关系
$this->socketFDService->bindRelation($request->fd, $userInfo['user_id']);
$ip = config('ip_address');
$server->push($request->fd, "成功连接[{$ip}],IM 服务器");
}
/**
* 消息接收回调事件
*
* @param Response|Server $server
* @param Frame $frame
*/
public function onMessage($server, Frame $frame): void
{
// 判断是否为心跳检测
if ($frame->data == 'PING') return;
$ip = config('ip_address');
$producer = container()->get(Producer::class);
$producer->produce(new ChatMessageProducer("我是来自[{$ip} 服务器的消息]{$frame->data}"));
}
/**
* 连接创建成功回调事件
*
* @param Response|\Swoole\Server $server
* @param int $fd
* @param int $reactorId
*/
public function onClose($server, int $fd, int $reactorId): void
{
$user_id = $this->socketFDService->findFdUserId($fd);
stdout_log()->notice("客户端FD:{$fd} 已关闭连接,用户ID为【{$user_id}");
stdout_log()->notice('关闭时间:' . date('Y-m-d H:i:s'));
// 解除fd关系
$this->socketFDService->removeRelation($fd);
// 判断是否存在异地登录
$isOnline = $this->socketFDService->isOnlineAll(intval($user_id));
if (!$isOnline) {
// ... 不存在异地登录,推送下线通知消息
// ... 包装推送消息至队列
} else {
stdout_log()->notice("用户:{$user_id} 存在异地登录...");
}
}
}