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 Hyperf\Amqp\Producer;
|
2020-11-04 11:57:16 +08:00
|
|
|
|
use App\Amqp\Producer\ChatMessageProducer;
|
2020-11-03 14:07:54 +08:00
|
|
|
|
|
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);
|
|
|
|
|
|
2020-11-03 17:12:57 +08:00
|
|
|
|
$ip = config('ip_address');
|
2020-11-04 11:57:16 +08:00
|
|
|
|
$producer->produce(new ChatMessageProducer("我是来自[{$ip} 服务器的消息],{$frame->data}"));
|
2020-11-02 22:45:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function onClose($server, int $fd, int $reactorId): void
|
|
|
|
|
{
|
2020-11-03 17:12:57 +08:00
|
|
|
|
echo PHP_EOL."FD : 【{$fd}】 已断开...";
|
2020-11-02 22:45:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function onOpen($server, Request $request): void
|
|
|
|
|
{
|
2020-11-03 17:12:57 +08:00
|
|
|
|
$ip = config('ip_address');
|
|
|
|
|
$server->push($request->fd, "成功连接[{$ip}],IM 服务器");
|
|
|
|
|
echo PHP_EOL."FD : 【{$request->fd}】 成功连接...";
|
2020-11-02 22:45:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|