2026-02-26 14:39:04 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
2026-04-04 10:29:39 +08:00
|
|
|
use Coroutine\Server\Config;
|
2026-02-26 14:39:04 +08:00
|
|
|
use Coroutine\Server\Websocket;
|
|
|
|
|
use Kiri\Router\Constrict\ConstrictRequest;
|
2026-05-09 22:17:19 +08:00
|
|
|
use Psr\Http\Message\RequestInterface;
|
2026-02-26 14:39:04 +08:00
|
|
|
use function Co\run;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Test extends Websocket
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-04 10:29:39 +08:00
|
|
|
* 对链接进行认证的地方
|
|
|
|
|
* 需要调用$request->withAuthority($authorization);方法将认证后的用户与链接进行关联
|
|
|
|
|
*
|
2026-02-26 14:39:04 +08:00
|
|
|
* @param ConstrictRequest $request
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2026-05-09 22:17:19 +08:00
|
|
|
public function onConnected(RequestInterface $request): bool
|
2026-02-26 14:39:04 +08:00
|
|
|
{
|
|
|
|
|
// TODO: Implement onConnected() method.
|
|
|
|
|
|
2026-05-09 22:17:19 +08:00
|
|
|
// $request->withAuthority(null);
|
2026-02-26 14:39:04 +08:00
|
|
|
|
2026-04-04 10:29:39 +08:00
|
|
|
return false;
|
2026-02-26 14:39:04 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-04 10:29:39 +08:00
|
|
|
|
2026-02-26 14:39:04 +08:00
|
|
|
/**
|
2026-04-04 10:29:39 +08:00
|
|
|
*
|
|
|
|
|
* 客户端发送的消息回调
|
|
|
|
|
*
|
|
|
|
|
* @param ConstrictRequest $psr7Request
|
|
|
|
|
* @param string $frame
|
2026-02-26 14:39:04 +08:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-05-09 22:17:19 +08:00
|
|
|
public function onMessage(RequestInterface $psr7Request, string $frame): void
|
2026-02-26 14:39:04 +08:00
|
|
|
{
|
|
|
|
|
// TODO: Implement onMessage() method.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-04 10:29:39 +08:00
|
|
|
* 消息循环之前调用的函数(此时链接已加入管理中)
|
2026-02-26 14:39:04 +08:00
|
|
|
* @return void
|
2026-04-04 10:29:39 +08:00
|
|
|
*
|
2026-02-26 14:39:04 +08:00
|
|
|
*/
|
2026-04-04 10:29:39 +08:00
|
|
|
public function onBeforeMessageLoop(): void
|
2026-02-26 14:39:04 +08:00
|
|
|
{
|
2026-04-04 10:29:39 +08:00
|
|
|
// TODO: Implement onBeforeMessageLoop() method.
|
2026-02-26 14:39:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-04 10:29:39 +08:00
|
|
|
* @param int $userId
|
2026-02-26 14:39:04 +08:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-04-04 10:29:39 +08:00
|
|
|
public function onDisconnect(int $userId): void
|
2026-02-26 14:39:04 +08:00
|
|
|
{
|
2026-04-04 10:29:39 +08:00
|
|
|
// TODO: Implement onDisconnect() method.
|
2026-02-26 14:39:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-04-04 10:29:39 +08:00
|
|
|
public function onStart(): void
|
2026-02-26 14:39:04 +08:00
|
|
|
{
|
|
|
|
|
}
|
2026-04-04 10:29:39 +08:00
|
|
|
|
2026-02-26 14:39:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
run(function () {
|
2026-04-04 10:29:39 +08:00
|
|
|
$config = new Config;
|
|
|
|
|
$config->port = 9501;
|
|
|
|
|
$config->host = '0.0.0.0';
|
|
|
|
|
$config->authKey = 'access_token';
|
|
|
|
|
|
2026-02-26 14:39:04 +08:00
|
|
|
$websocket = Kiri::getDi()->get(Test::class);
|
2026-04-04 10:29:39 +08:00
|
|
|
$websocket->start($config);
|
2026-02-26 14:39:04 +08:00
|
|
|
});
|