xiaoai/php_server/app/common/service/socket/handlers/WebWorkerHandler.php

60 lines
2.4 KiB
PHP

<?php
namespace app\common\service\socket\handlers;
use app\common\service\socket\BaseMessageHandler;
use app\common\service\socket\WorkerEnum;
use Workerman\Connection\TcpConnection;
class WebWorkerHandler extends BaseMessageHandler
{
protected int $msgType;
public function handle(TcpConnection $connection, string $uid, array $payload): void
{
$content = !is_array($payload['content']) ? json_decode($payload['content'], true) : $payload['content'];
$content['userId'] = $content['userId'] ?? 0;
try {
$this->uid = $uid;
$this->payload = $payload;
$this->connection = $connection;
$this->userId = $content['userId'] ?? 0;
if($this->userId == 0){
$this->payload['reply'] = '用户参数无效';
$this->payload['code'] = WorkerEnum::WEB_USER_INVALID_REQUEST;
$this->sendError($this->connection, $this->payload);
return;
}
$worker = $this->service->getWorker();
if(isset($worker->uidConnections[$uid])){
$worker->uidConnections[$uid]->apptype = WorkerEnum::WS_WEB_TYPE;
$worker->uidConnections[$uid]->userid = $content['userId'] ?? 0;
$worker->uidConnections[$uid]->clientType = 'webUser';
$worker->uidConnections[$uid]->name = 'web_' . $content['userId'];
$this->service->getRedis()->set("xhs:user:{$content['userId']}" , $uid);
}
$message = array(
'messageId' => $uid,
'type' => 'bindSocket',
'reply' => [
'type' => 'bindSocket',
'msg' => '绑定成功',
'userId' => $content['userId'],
'wsId' => $uid
],
'code' => WorkerEnum::SUCCESS_CODE
);
$this->sendResponse($uid, $message, $message['reply']);
$this->setLog($message, 'bind');
} catch (\Exception $e) {
$this->setLog('异常信息'. $e, 'bind');
$this->payload['reply'] = $e->getMessage();
$this->payload['code'] = WorkerEnum::WED_BIND_ERROR_CODE;
$this->payload['type'] = 'error';
$this->sendError($this->connection, $this->payload);
}
}
}