Skip to content
Snippets Groups Projects
Commit 09899429 authored by Mateusz Charytoniuk's avatar Mateusz Charytoniuk
Browse files

chore: rename dm-rpc -> json-rpc

parent 9c44a6cf
No related branches found
No related tags found
No related merge requests found
Showing
with 177 additions and 125 deletions
......@@ -55,9 +55,9 @@ You can set up all the asynchronous features using
attributes. No elaborate configuration is needed.
```php
#[RespondsToWebSocketRPC(RPCMethod::Echo)]
#[Singleton(collection: SingletonCollection::WebSocketRPCResponder)]
final readonly class EchoResponder extends WebSocketRPCResponder
#[RespondsToWebSocketJsonRPC(JsonRPCMethod::Echo)]
#[Singleton(collection: SingletonCollection::WebSocketJsonRPCResponder)]
final readonly class EchoResponder extends WebSocketJsonJsonRPCResponder
{
public function getConstraint(): Constraint
{
......@@ -69,7 +69,7 @@ final readonly class EchoResponder extends WebSocketRPCResponder
WebSocketConnection $webSocketConnection,
RPCRequest $rpcRequest,
): void {
$webSocketConnection->push(new RPCResponse(
$webSocketConnection->push(new JsonRPCResponse(
$rpcRequest,
$rpcRequest->payload,
));
......
......@@ -11,14 +11,14 @@ description: >
# Protocols
## RPC
## Json RPC
```graphviz render
digraph {
Message
-> WebSocketServerController
-> RPCProtocolController
-> WebSocketRPCResponder
-> JsonRPCProtocolController
-> WebSocketJsonRPCResponder
;
}
```
......@@ -34,7 +34,7 @@ const serverUrl = new URL('wss://localhost:9501');
serverUrl.searchParams.append('csrf', /* obtain CSRF token */);
const webSocket = new WebSocket(serverUrl, ['dm-rpc']);
const webSocket = new WebSocket(serverUrl, ['jsonrpc']);
```
For example, if you are using {{docs/features/templating/twig/index}}, you can
......@@ -53,31 +53,31 @@ serverUrl.searchParams.append(
);
```
### Writing RPC Responders
### Writing Json RPC Responders
```php
<?php
namespace App\WebSocketRPCResponder;
namespace App\WebSocketJsonRPCResponder;
use App\RPCMethod;
use Distantmagic\Resonance\Attribute\RespondsToWebSocketRPC;
use App\JsonRPCMethod;
use Distantmagic\Resonance\Attribute\RespondsToWebSocketJsonRPC;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\Attribute\WantsFeature;
use Distantmagic\Resonance\Feature;
use Distantmagic\Resonance\Constraint;
use Distantmagic\Resonance\Constraint\StringConstraint;
use Distantmagic\Resonance\RPCRequest;
use Distantmagic\Resonance\RPCResponse;
use Distantmagic\Resonance\Feature;
use Distantmagic\Resonance\JsonRPCRequest;
use Distantmagic\Resonance\JsonRPCResponse;
use Distantmagic\Resonance\SingletonCollection;
use Distantmagic\Resonance\WebSocketAuthResolution;
use Distantmagic\Resonance\WebSocketConnection;
use Distantmagic\Resonance\WebSocketRPCResponder;
use Distantmagic\Resonance\WebSocketJsonRPCResponder;
#[RespondsToWebSocketRPC(RPCMethod::Echo)]
#[Singleton(collection: SingletonCollection::WebSocketRPCResponder)]
#[RespondsToWebSocketJsonRPC(JsonRPCMethod::Echo)]
#[Singleton(collection: SingletonCollection::WebSocketJsonRPCResponder)]
#[WantsFeature(Feature::WebSocket)]
final readonly class EchoResponder extends WebSocketRPCResponder
final readonly class EchoResponder extends WebSocketJsonRPCResponder
{
public function getConstraint(): Constraint
{
......@@ -87,9 +87,9 @@ final readonly class EchoResponder extends WebSocketRPCResponder
public function onRequest(
WebSocketAuthResolution $webSocketAuthResolution,
WebSocketConnection $webSocketConnection,
RPCRequest $rpcRequest,
JsonRPCRequest $rpcRequest,
): void {
$webSocketConnection->push(new RPCResponse(
$webSocketConnection->push(new JsonRPCResponse(
$rpcRequest,
$rpcRequest->payload,
));
......@@ -101,12 +101,12 @@ final readonly class EchoResponder extends WebSocketRPCResponder
In case you want not only respond to RPC messages, but also be able to push
notifications to the client at any moment, you can implement
`Distantmagic\Resonance\WebSocketRPCConnectionControllerInterface` and
`Distantmagic\Resonance\WebSocketJsonRPCConnectionControllerInterface` and
register in in the {{docs/features/dependency-injection/index}} container.
For example:
```php file:app/WebSocketRPCConnectionController.php
```php file:app/WebSocketJsonRPCConnectionController.php
<?php
namespace App;
......@@ -114,14 +114,14 @@ namespace App;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\Attribute\WantsFeature;
use Distantmagic\Resonance\Feature;
use Distantmagic\Resonance\RPCNotification;
use Distantmagic\Resonance\JsonRPCNotification;
use Distantmagic\Resonance\WebSocketAuthResolution;
use Distantmagic\Resonance\WebSocketConnection;
use Distantmagic\Resonance\WebSocketRPCConnectionControllerInterface;
use Distantmagic\Resonance\WebSocketJsonRPCConnectionControllerInterface;
#[Singleton(provides: WebSocketRPCConnectionControllerInterface::class)]
#[Singleton(provides: WebSocketJsonRPCConnectionControllerInterface::class)]
#[WantsFeature(Feature::WebSocket)]
readonly class WebSocketRPCConnectionController implements WebSocketRPCConnectionControllerInterface
readonly class WebSocketJsonRPCConnectionController implements WebSocketJsonRPCConnectionControllerInterface
{
public function onClose(
WebSocketAuthResolution $webSocketAuthResolution,
......@@ -139,8 +139,8 @@ readonly class WebSocketRPCConnectionController implements WebSocketRPCConnectio
// connection is closed
}
$webSocketConnection->push(new RPCNotification(
RPCMethod::YourMethod,
$webSocketConnection->push(new JsonRPCNotification(
JsonRPCMethod::YourMethod,
[
// your payload
]
......
......@@ -24,18 +24,18 @@ However, they cannot share the same port if they are separated.
## Enabling the Server
To enable WebSocket server you need to provide the
`RPCMethodValidatorInterface`. For example:
`JsonRPCMethodValidatorInterface`. For example:
```php file:app\RPCMethod.php
```php file:app\JsonRPCMethod.php
<?php
namespace App;
use Distantmagic\Resonance\EnumValuesTrait;
use Distantmagic\Resonance\JsonRPCMethodInterface;
use Distantmagic\Resonance\NameableEnumTrait;
use Distantmagic\Resonance\RPCMethodInterface;
enum RPCMethod: string implements RPCMethodInterface
enum JsonRPCMethod: string implements JsonRPCMethodInterface
{
use EnumValuesTrait;
use NameableEnumTrait;
......@@ -49,7 +49,7 @@ Do not forget about `#[WantsFeature(Feature::WebSocket)` attribute. If added to
any singleton, it tells Resonance to enable the WebSocket server.
:::
```php file:app\RPCMethodValidator.php
```php file:app\JsonRPCMethodValidator.php
<?php
namespace App;
......@@ -57,21 +57,21 @@ namespace App;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\Attribute\WantsFeature;
use Distantmagic\Resonance\Feature;
use Distantmagic\Resonance\RPCMethodInterface;
use Distantmagic\Resonance\RPCMethodValidatorInterface;
use Distantmagic\Resonance\JsonRPCMethodInterface;
use Distantmagic\Resonance\JsonRPCMethodValidatorInterface;
#[Singleton(provides: RPCMethodValidatorInterface::class)]
#[Singleton(provides: JsonRPCMethodValidatorInterface::class)]
#[WantsFeature(Feature::WebSocket)]
readonly class RPCMethodValidator implements RPCMethodValidatorInterface
readonly class JsonRPCMethodValidator implements JsonRPCMethodValidatorInterface
{
public function cases(): array
{
return RPCMethod::cases();
return JsonRPCMethod::cases();
}
public function castToRPCMethod(string $methodName): RPCMethodInterface
public function castToRPCMethod(string $methodName): JsonRPCMethodInterface
{
return RPCMethod::from($methodName);
return JsonRPCMethod::from($methodName);
}
public function names(): array
......
......@@ -149,9 +149,9 @@ readonly class CatAdopt implements PromptSubjectResponderInterface
class="language-php"
data-controller="hljs"
data-hljs-language-value="php"
>#[RespondsToWebSocketRPC(RPCMethod::Echo)]
#[Singleton(collection: SingletonCollection::WebSocketRPCResponder)]
final readonly class EchoResponder extends WebSocketRPCResponder
>#[RespondsToWebSocketJsonRPC(JsonRPCMethod::Echo)]
#[Singleton(collection: SingletonCollection::WebSocketJsonRPCResponder)]
final readonly class EchoResponder extends WebSocketJsonRPCResponder
{
public function getConstraint(): Constraint
{
......
......@@ -39,7 +39,7 @@ Once you have both Resonance and
First, we must create a security gate and decide which users can use our
chat. Let's say all authenticated users can do that:
```php file:app/SiteActionGate/StartWebSocketRPCConnectionGate.php
```php file:app/SiteActionGate/StartWebSocketJsonRPCConnectionGate.php
<?php
namespace App\SiteActionGate;
......@@ -52,9 +52,9 @@ use Distantmagic\Resonance\SingletonCollection;
use Distantmagic\Resonance\SiteAction;
use Distantmagic\Resonance\SiteActionGate;
#[DecidesSiteAction(SiteAction::StartWebSocketRPCConnection)]
#[DecidesSiteAction(SiteAction::StartWebSocketJsonRPCConnection)]
#[Singleton(collection: SingletonCollection::SiteActionGate)]
final readonly class StartWebSocketRPCConnectionGate extends SiteActionGate
final readonly class StartWebSocketJsonRPCConnectionGate extends SiteActionGate
{
public function can(?AuthenticatedUser $authenticatedUser): bool
{
......@@ -101,7 +101,7 @@ use Distantmagic\Resonance\TwigTemplate;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
#[Can(SiteAction::StartWebSocketRPCConnection)]
#[Can(SiteAction::StartWebSocketJsonRPCConnection)]
#[RespondsToHttp(
method: RequestMethod::GET,
pattern: '/chat',
......@@ -136,9 +136,9 @@ Method, the first field, is validated against `RPCMethodInterface`, through
namespace App;
use Distantmagic\Resonance\EnumValuesTrait;
use Distantmagic\Resonance\RPCMethodInterface;
use Distantmagic\Resonance\JsonRPCMethodInterface;
enum RPCMethod: string implements RPCMethodInterface
enum JsonRPCMethod: string implements JsonRPCMethodInterface
{
use EnumValuesTrait;
......@@ -160,26 +160,26 @@ namespace App;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\Attribute\WantsFeature;
use Distantmagic\Resonance\Feature;
use Distantmagic\Resonance\RPCMethodInterface;
use Distantmagic\Resonance\RPCMethodValidatorInterface;
use Distantmagic\Resonance\JsonRPCMethodInterface;
use Distantmagic\Resonance\JsonRPCMethodValidatorInterface;
#[Singleton(provides: RPCMethodValidatorInterface::class)]
#[Singleton(provides: JsonRPCMethodValidatorInterface::class)]
#[WantsFeature(Feature::WebSocket)]
readonly class RPCMethodValidator implements RPCMethodValidatorInterface
readonly class JsonRPCMethodValidator implements JsonRPCMethodValidatorInterface
{
public function cases(): array
{
return RPCMethod::cases();
return JsonRPCMethod::cases();
}
public function castToRPCMethod(string $methodName): RPCMethodInterface
public function castToRPCMethod(string $methodName): JsonRPCMethodInterface
{
return RPCMethod::from($methodName);
return JsonRPCMethod::from($methodName);
}
public function values(): array
{
return RPCMethod::values();
return JsonRPCMethod::values();
}
}
```
......@@ -196,33 +196,33 @@ It will then use `LlamaCppClient` to fetch tokens from
[llama.cpp](https://github.com/ggerganov/llama.cpp) server and forward them
to the WebSocket connection:
```php file:app/WebSocketRPCResponder/LlmChatPromptResponder.php
```php file:app/WebSocketJsonRPCResponder/LlmChatPromptResponder.php
<?php
namespace App\WebSocketRPCResponder;
namespace App\WebSocketJsonRPCResponder;
use App\RPCMethod;
use Distantmagic\Resonance\Attribute\RespondsToWebSocketRPC;
use App\JsonRPCMethod;
use Distantmagic\Resonance\Attribute\RespondsToWebSocketJsonRPC;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\Attribute\WantsFeature;
use Distantmagic\Resonance\Feature;
use Distantmagic\Resonance\Constraint;
use Distantmagic\Resonance\Constraint\ObjectConstraint;
use Distantmagic\Resonance\Constraint\StringConstraint;
use Distantmagic\Resonance\Feature;
use Distantmagic\Resonance\JsonRPCNotification;
use Distantmagic\Resonance\JsonRPCRequest;
use Distantmagic\Resonance\JsonRPCResponse;
use Distantmagic\Resonance\LlamaCppClient;
use Distantmagic\Resonance\LlamaCppCompletionRequest;
use Distantmagic\Resonance\RPCNotification;
use Distantmagic\Resonance\RPCRequest;
use Distantmagic\Resonance\RPCResponse;
use Distantmagic\Resonance\SingletonCollection;
use Distantmagic\Resonance\WebSocketAuthResolution;
use Distantmagic\Resonance\WebSocketConnection;
use Distantmagic\Resonance\WebSocketRPCResponder;
use Distantmagic\Resonance\WebSocketJsonRPCResponder;
#[RespondsToWebSocketRPC(RPCMethod::LlmChatPrompt)]
#[Singleton(collection: SingletonCollection::WebSocketRPCResponder)]
#[RespondsToWebSocketJsonRPC(JsonRPCMethod::LlmChatPrompt)]
#[Singleton(collection: SingletonCollection::WebSocketJsonRPCResponder)]
#[WantsFeature(Feature::WebSocket)]
final readonly class LlmChatPromptResponder extends WebSocketRPCResponder
final readonly class LlmChatPromptResponder extends WebSocketJsonRPCResponder
{
public function __construct(
private LlamaCppClient $llamaCppClient,
......@@ -240,7 +240,7 @@ final readonly class LlmChatPromptResponder extends WebSocketRPCResponder
public function onNotification(
WebSocketAuthResolution $webSocketAuthResolution,
WebSocketConnection $webSocketConnection,
RPCNotification $rpcNotification,
JsonRPCNotification $rpcNotification,
): void {
$request = new LlamaCppCompletionRequest($rpcNotification->payload->prompt);
......@@ -248,8 +248,8 @@ final readonly class LlmChatPromptResponder extends WebSocketRPCResponder
foreach ($completion as $token) {
if ($webSocketConnection->status->isOpen()) {
$webSocketConnection->push(new RPCNotification(
RPCMethod::LlmToken,
$webSocketConnection->push(new JsonRPCNotification(
JsonRPCMethod::LlmToken,
$token->content,
));
} else {
......@@ -265,7 +265,7 @@ final readonly class LlmChatPromptResponder extends WebSocketRPCResponder
[Stimulus](https://stimulus.hotwired.dev/) controller starts WebSocket
connection and sends user prompts to the server. Not that WebSocket
connection uses both {{docs/features/security/csrf-protection/index}} and
`dm-rpc` {{docs/features/websockets/protocols}}:
`jsonrpc` {{docs/features/websockets/protocols}}:
```typescript file:resources/ts/controller_llmchat.ts
import { Controller } from "@hotwired/stimulus";
......@@ -290,7 +290,7 @@ export class controller_llmchat extends Controller<HTMLElement> {
servertUrl.searchParams.append("csrf", this.csrfTokenValue);
const webSocket = new WebSocket(servertUrl, ["dm-rpc"]);
const webSocket = new WebSocket(servertUrl, ["jsonrpc"]);
webSocket.addEventListener("close", () => {
this.webSocket = null;
......@@ -307,9 +307,7 @@ export class controller_llmchat extends Controller<HTMLElement> {
const parsed: unknown = JSON.parse(evt.data);
if (Array.isArray(parsed) && "string" === typeof parsed[1]) {
this.chatLogTarget.append(parsed[1]);
}
this.chatLogTarget.append(parsed.result);
});
}
......@@ -322,13 +320,13 @@ export class controller_llmchat extends Controller<HTMLElement> {
this.chatLogTarget.innerHTML = '';
this.webSocket?.send(JSON.stringify([
"llm_chat_prompt",
{
prompt: this.userInputFieldTarget.value,
},
null
]));
this.webSocket?.send(JSON.stringify({
jsonrpc: "2.0",
method: "llm_chat_prompt",
params: {
prompt: this.userInputFieldTarget.value,
}
}));
this.userInputFieldTarget.value = '';
}
......
......@@ -6,12 +6,12 @@ namespace Distantmagic\Resonance\Attribute;
use Attribute;
use Distantmagic\Resonance\Attribute as BaseAttribute;
use Distantmagic\Resonance\RPCMethodInterface;
use Distantmagic\Resonance\JsonRPCMethodInterface;
#[Attribute(Attribute::TARGET_CLASS)]
final readonly class RespondsToWebSocketRPC extends BaseAttribute
final readonly class RespondsToWebSocketJsonRPC extends BaseAttribute
{
public function __construct(
public RPCMethodInterface $method,
public JsonRPCMethodInterface $method,
) {}
}
......@@ -5,18 +5,18 @@ declare(strict_types=1);
namespace Distantmagic\Resonance\InputValidatedData;
use Distantmagic\Resonance\InputValidatedData;
use Distantmagic\Resonance\RPCMethodInterface;
use Distantmagic\Resonance\JsonRPCMethodInterface;
/**
* @template TPayload
*/
readonly class RPCMessage extends InputValidatedData
readonly class JsonRPCMessage extends InputValidatedData
{
/**
* @param TPayload $payload
*/
public function __construct(
public RPCMethodInterface $method,
public JsonRPCMethodInterface $method,
public mixed $payload,
public ?string $requestId,
) {}
......
......@@ -12,13 +12,13 @@ use Distantmagic\Resonance\Constraint\EnumConstraint;
use Distantmagic\Resonance\Constraint\StringConstraint;
use Distantmagic\Resonance\Constraint\TupleConstraint;
use Distantmagic\Resonance\Feature;
use Distantmagic\Resonance\InputValidatedData\RPCMessage;
use Distantmagic\Resonance\InputValidatedData\JsonRPCMessage;
use Distantmagic\Resonance\InputValidator;
use Distantmagic\Resonance\RPCMethodValidatorInterface;
use Distantmagic\Resonance\JsonRPCMethodValidatorInterface;
use Distantmagic\Resonance\SingletonCollection;
/**
* @extends InputValidator<RPCMessage, array{
* @extends InputValidator<JsonRPCMessage, array{
* 0: string,
* 1: mixed,
* 2: null|string,
......@@ -26,13 +26,13 @@ use Distantmagic\Resonance\SingletonCollection;
*/
#[GrantsFeature(Feature::WebSocket)]
#[Singleton(collection: SingletonCollection::InputValidator)]
readonly class RPCMessageValidator extends InputValidator
readonly class JsonRPCMessageValidator extends InputValidator
{
public function __construct(private RPCMethodValidatorInterface $rpcMethodValidator) {}
public function __construct(private JsonRPCMethodValidatorInterface $rpcMethodValidator) {}
public function castValidatedData(mixed $data): RPCMessage
public function castValidatedData(mixed $data): JsonRPCMessage
{
return new RPCMessage(
return new JsonRPCMessage(
$this->rpcMethodValidator->castToRPCMethod($data[0]),
$data[1],
$data[2],
......
......@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Distantmagic\Resonance;
interface RPCMethodInterface
interface JsonRPCMethodInterface
{
public function getValue(): string;
}
......@@ -4,14 +4,14 @@ declare(strict_types=1);
namespace Distantmagic\Resonance;
interface RPCMethodValidatorInterface
interface JsonRPCMethodValidatorInterface
{
/**
* @return array<RPCMethodInterface>
* @return array<JsonRPCMethodInterface>
*/
public function cases(): array;
public function castToRPCMethod(string $methodName): RPCMethodInterface;
public function castToRPCMethod(string $methodName): JsonRPCMethodInterface;
/**
* @return array<string>
......
......@@ -11,13 +11,13 @@ use Stringable;
*
* @template TPayload
*/
readonly class RPCNotification implements Stringable
readonly class JsonRPCNotification implements Stringable
{
/**
* @param TPayload $payload
*/
public function __construct(
public RPCMethodInterface $method,
public JsonRPCMethodInterface $method,
public mixed $payload,
) {}
......
......@@ -9,13 +9,13 @@ namespace Distantmagic\Resonance;
*
* @template TPayload
*/
readonly class RPCRequest
readonly class JsonRPCRequest
{
/**
* @param TPayload $payload
*/
public function __construct(
public RPCMethodInterface $method,
public JsonRPCMethodInterface $method,
public mixed $payload,
public string $requestId,
) {}
......
......@@ -6,10 +6,10 @@ namespace Distantmagic\Resonance;
use Stringable;
readonly class RPCResponse implements Stringable
readonly class JsonRPCResponse implements Stringable
{
public function __construct(
private RPCRequest $rpcRequest,
private JsonRPCRequest $rpcRequest,
private mixed $content,
) {}
......
......@@ -37,6 +37,6 @@ enum SingletonCollection implements SingletonCollectionInterface
case TwigFilter;
case TwigFunction;
case TwigLoader;
case WebSocketJsonRPCResponder;
case WebSocketProtocolController;
case WebSocketRPCResponder;
}
......@@ -5,50 +5,50 @@ declare(strict_types=1);
namespace Distantmagic\Resonance\SingletonProvider;
use Distantmagic\Resonance\Attribute\RequiresSingletonCollection;
use Distantmagic\Resonance\Attribute\RespondsToWebSocketRPC;
use Distantmagic\Resonance\Attribute\RespondsToWebSocketJsonRPC;
use Distantmagic\Resonance\Attribute\Singleton;
use Distantmagic\Resonance\PHPProjectFiles;
use Distantmagic\Resonance\SingletonAttribute;
use Distantmagic\Resonance\SingletonCollection;
use Distantmagic\Resonance\SingletonContainer;
use Distantmagic\Resonance\SingletonProvider;
use Distantmagic\Resonance\WebSocketRPCResponderAggregate;
use Distantmagic\Resonance\WebSocketRPCResponderInterface;
use Distantmagic\Resonance\WebSocketJsonRPCResponderAggregate;
use Distantmagic\Resonance\WebSocketJsonRPCResponderInterface;
/**
* @template-extends SingletonProvider<WebSocketRPCResponderAggregate>
* @template-extends SingletonProvider<WebSocketJsonRPCResponderAggregate>
*/
#[RequiresSingletonCollection(SingletonCollection::WebSocketRPCResponder)]
#[Singleton(provides: WebSocketRPCResponderAggregate::class)]
final readonly class WebSocketRPCResponderAggregateProvider extends SingletonProvider
#[RequiresSingletonCollection(SingletonCollection::WebSocketJsonRPCResponder)]
#[Singleton(provides: WebSocketJsonRPCResponderAggregate::class)]
final readonly class WebSocketJsonRPCResponderAggregateProvider extends SingletonProvider
{
public function provide(SingletonContainer $singletons, PHPProjectFiles $phpProjectFiles): WebSocketRPCResponderAggregate
public function provide(SingletonContainer $singletons, PHPProjectFiles $phpProjectFiles): WebSocketJsonRPCResponderAggregate
{
$webSocketRPCResponderAggregate = new WebSocketRPCResponderAggregate();
$webSocketJsonRPCResponderAggregate = new WebSocketJsonRPCResponderAggregate();
foreach ($this->collectWebSocketRPCResponders($singletons) as $rpcResponderAttribute) {
$webSocketRPCResponderAggregate->cachedConstraints->put(
foreach ($this->collectWebSocketJsonRPCResponders($singletons) as $rpcResponderAttribute) {
$webSocketJsonRPCResponderAggregate->cachedConstraints->put(
$rpcResponderAttribute->singleton,
$rpcResponderAttribute->singleton->getConstraint(),
);
$webSocketRPCResponderAggregate->rpcResponders->put(
$webSocketJsonRPCResponderAggregate->rpcResponders->put(
$rpcResponderAttribute->attribute->method,
$rpcResponderAttribute->singleton,
);
}
return $webSocketRPCResponderAggregate;
return $webSocketJsonRPCResponderAggregate;
}
/**
* @return iterable<SingletonAttribute<WebSocketRPCResponderInterface,RespondsToWebSocketRPC>>
* @return iterable<SingletonAttribute<WebSocketJsonRPCResponderInterface,RespondsToWebSocketJsonRPC>>
*/
private function collectWebSocketRPCResponders(SingletonContainer $singletons): iterable
private function collectWebSocketJsonRPCResponders(SingletonContainer $singletons): iterable
{
return $this->collectAttributes(
$singletons,
WebSocketRPCResponderInterface::class,
RespondsToWebSocketRPC::class,
WebSocketJsonRPCResponderInterface::class,
RespondsToWebSocketJsonRPC::class,
);
}
}
......@@ -8,7 +8,7 @@ enum SiteAction implements SiteActionInterface
{
use NameableEnumTrait;
case StartWebSocketRPCConnection;
case StartWebSocketJsonRPCConnection;
case UseGraphQL;
case UseOAuth2;
}
......@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Distantmagic\Resonance;
abstract readonly class WebSocketRPCConnectionController implements WebSocketRPCConnectionControllerInterface
abstract readonly class WebSocketJsonRPCConnectionController implements WebSocketJsonRPCConnectionControllerInterface
{
public function onClose(WebSocketAuthResolution $webSocketAuthResolution, WebSocketConnection $webSocketConnection): void {}
}
......@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Distantmagic\Resonance;
interface WebSocketRPCConnectionControllerInterface
interface WebSocketJsonRPCConnectionControllerInterface
{
public function onClose(
WebSocketAuthResolution $webSocketAuthResolution,
......
......@@ -4,18 +4,18 @@ declare(strict_types=1);
namespace Distantmagic\Resonance;
use Distantmagic\Resonance\InputValidatedData\RPCMessage;
use Distantmagic\Resonance\InputValidatedData\JsonRPCMessage;
use Ds\Set;
readonly class WebSocketRPCConnectionHandle
readonly class WebSocketJsonRPCConnectionHandle
{
/**
* @var Set<WebSocketRPCResponderInterface>
* @var Set<WebSocketJsonRPCResponderInterface>
*/
private Set $activeResponders;
public function __construct(
public WebSocketRPCResponderAggregate $webSocketRPCResponderAggregate,
public WebSocketJsonRPCResponderAggregate $webSocketJsonRPCResponderAggregate,
public WebSocketAuthResolution $webSocketAuthResolution,
public WebSocketConnection $webSocketConnection,
) {
......@@ -36,15 +36,15 @@ readonly class WebSocketRPCConnectionHandle
}
}
public function onRPCMessage(RPCMessage $rpcMessage): ConstraintResult
public function onRPCMessage(JsonRPCMessage $rpcMessage): ConstraintResult
{
$responder = $this
->webSocketRPCResponderAggregate
->webSocketJsonRPCResponderAggregate
->selectResponder($rpcMessage)
;
$constraintResult = $this
->webSocketRPCResponderAggregate
->webSocketJsonRPCResponderAggregate
->cachedConstraints
->get($responder)
->validate($rpcMessage->payload)
......@@ -65,7 +65,7 @@ readonly class WebSocketRPCConnectionHandle
$responder->onRequest(
$this->webSocketAuthResolution,
$this->webSocketConnection,
new RPCRequest(
new JsonRPCRequest(
$rpcMessage->method,
$constraintResult->castedData,
$rpcMessage->requestId,
......@@ -75,7 +75,7 @@ readonly class WebSocketRPCConnectionHandle
$responder->onNotification(
$this->webSocketAuthResolution,
$this->webSocketConnection,
new RPCNotification(
new JsonRPCNotification(
$rpcMessage->method,
$constraintResult->castedData,
)
......
......@@ -10,9 +10,9 @@ use Distantmagic\Resonance\WebSocketProtocolException\UnexpectedRequest;
/**
* @template TPayload
*
* @template-implements WebSocketRPCResponderInterface<TPayload>
* @template-implements WebSocketJsonRPCResponderInterface<TPayload>
*/
abstract readonly class WebSocketRPCResponder implements WebSocketRPCResponderInterface
abstract readonly class WebSocketJsonRPCResponder implements WebSocketJsonRPCResponderInterface
{
public function onBeforeMessage(
WebSocketAuthResolution $webSocketAuthResolution,
......@@ -27,7 +27,7 @@ abstract readonly class WebSocketRPCResponder implements WebSocketRPCResponderIn
public function onNotification(
WebSocketAuthResolution $webSocketAuthResolution,
WebSocketConnection $webSocketConnection,
RPCNotification $rpcNotification,
JsonRPCNotification $rpcNotification,
): void {
throw new UnexpectedNotification($rpcNotification->method);
}
......@@ -35,7 +35,7 @@ abstract readonly class WebSocketRPCResponder implements WebSocketRPCResponderIn
public function onRequest(
WebSocketAuthResolution $webSocketAuthResolution,
WebSocketConnection $webSocketConnection,
RPCRequest $rpcRequest,
JsonRPCRequest $rpcRequest,
): void {
throw new UnexpectedRequest($rpcRequest->method);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment