hyperf-chat/app/Controller/WebSocketController.php

39 lines
948 B
PHP
Raw Normal View History

2020-11-02 22:45:37 +08:00
<?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;
2020-11-03 14:07:54 +08:00
use Swoole\Server;
2020-11-02 22:45:37 +08:00
use Swoole\WebSocket\Server as WebSocketServer;
2020-11-03 14:07:54 +08:00
use Hyperf\Amqp\Producer;
use App\Amqp\Producer\DemoProducer;
2020-11-02 22:45:37 +08:00
class WebSocketController implements OnMessageInterface, OnOpenInterface, OnCloseInterface
{
public function onMessage($server, Frame $frame): void
{
2020-11-03 14:07:54 +08:00
$producer = container()->get(Producer::class);
$producer->produce(new DemoProducer('test'. date('Y-m-d H:i:s')));
//$server->push($frame->fd, 'Recv: ' . $frame->data);
2020-11-02 22:45:37 +08:00
}
public function onClose($server, int $fd, int $reactorId): void
{
var_dump('closed');
}
public function onOpen($server, Request $request): void
{
$server->push($request->fd, 'Opened');
}
}