hyperf-chat/app/Controller/WebSocketController.php

39 lines
948 B
PHP

<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface;
use Swoole\Http\Request;
use Swoole\Websocket\Frame;
use Swoole\Server;
use Swoole\WebSocket\Server as WebSocketServer;
use Hyperf\Amqp\Producer;
use App\Amqp\Producer\DemoProducer;
class WebSocketController implements OnMessageInterface, OnOpenInterface, OnCloseInterface
{
public function onMessage($server, Frame $frame): void
{
$producer = container()->get(Producer::class);
$producer->produce(new DemoProducer('test'. date('Y-m-d H:i:s')));
//$server->push($frame->fd, 'Recv: ' . $frame->data);
}
public function onClose($server, int $fd, int $reactorId): void
{
var_dump('closed');
}
public function onOpen($server, Request $request): void
{
$server->push($request->fd, 'Opened');
}
}