Protocol
This commit is contained in:
@@ -4,12 +4,41 @@ use super::*;
|
||||
|
||||
impl super::Client {
|
||||
pub async fn initialize(mut socket: WebSocket) -> anyhow::Result<Self> {
|
||||
if let Some(ServerMessage::Initialize()) = Client::read_socket(&mut socket).await? {}
|
||||
let Some(ServerMethod::Initialize {
|
||||
public_key,
|
||||
timestamp,
|
||||
signature,
|
||||
}) = Client::read_socket(&mut socket).await?
|
||||
else {
|
||||
Client::send_socket(
|
||||
&mut socket,
|
||||
ClientMethod::Error {
|
||||
error: Cow::Borrowed(
|
||||
"Failed to initialize, unexpected method, expected: initialize",
|
||||
),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
return Err(anyhow::anyhow!(
|
||||
"Failed to initialize: Client sent the wrong method"
|
||||
));
|
||||
};
|
||||
|
||||
Client::send_socket(
|
||||
&mut socket,
|
||||
ClientMethod::Initialized {
|
||||
public_key,
|
||||
timestamp,
|
||||
signature,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Self {
|
||||
socket,
|
||||
meta: super::ClientMeta {},
|
||||
pub_key: String::new(),
|
||||
public_key: String::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+31
-13
@@ -1,3 +1,5 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use axum::extract::ws::{Message, Utf8Bytes, WebSocket};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -6,24 +8,41 @@ pub mod initialize;
|
||||
pub struct Client {
|
||||
pub socket: WebSocket,
|
||||
pub meta: ClientMeta,
|
||||
pub pub_key: String,
|
||||
pub public_key: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ClientMeta {}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum ClientMessage<'a> {
|
||||
Error(&'a str),
|
||||
#[serde(tag = "method")]
|
||||
pub enum ClientMethod {
|
||||
Initialized {
|
||||
public_key: String,
|
||||
timestamp: u64,
|
||||
signature: String,
|
||||
},
|
||||
|
||||
#[serde(rename = "error")]
|
||||
Error { error: Cow<'static, str> },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum ServerMessage {
|
||||
Initialize(),
|
||||
#[serde(tag = "method")]
|
||||
pub enum ServerMethod {
|
||||
Initialize {
|
||||
public_key: String,
|
||||
timestamp: u64,
|
||||
signature: String,
|
||||
},
|
||||
|
||||
Error {
|
||||
error: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub async fn read_socket(socket: &mut WebSocket) -> anyhow::Result<Option<ServerMessage>> {
|
||||
pub async fn read_socket(socket: &mut WebSocket) -> anyhow::Result<Option<ServerMethod>> {
|
||||
match socket.recv().await.transpose()? {
|
||||
Some(Message::Text(text)) => {
|
||||
if let Ok(msg) = serde_json::from_str(&text.to_string()) {
|
||||
@@ -31,7 +50,9 @@ impl Client {
|
||||
} else {
|
||||
Client::send_socket(
|
||||
socket,
|
||||
ClientMessage::Error("Unable to parse message: {text}"),
|
||||
ClientMethod::Error {
|
||||
error: Cow::Borrowed("Unable to parse message: {text}"),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -51,10 +72,7 @@ impl Client {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_socket<'a>(
|
||||
socket: &mut WebSocket,
|
||||
message: ClientMessage<'a>,
|
||||
) -> anyhow::Result<()> {
|
||||
pub async fn send_socket(socket: &mut WebSocket, message: ClientMethod) -> anyhow::Result<()> {
|
||||
socket
|
||||
.send(Message::Text(Utf8Bytes::from(serde_json::to_string(
|
||||
&message,
|
||||
@@ -64,11 +82,11 @@ impl Client {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn read(&mut self) -> anyhow::Result<Option<ServerMessage>> {
|
||||
pub async fn read(&mut self) -> anyhow::Result<Option<ServerMethod>> {
|
||||
Self::read_socket(&mut self.socket).await
|
||||
}
|
||||
|
||||
pub async fn send<'a>(&mut self, message: ClientMessage<'a>) -> anyhow::Result<()> {
|
||||
pub async fn send(&mut self, message: ClientMethod) -> anyhow::Result<()> {
|
||||
Self::send_socket(&mut self.socket, message).await
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user