mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-09 21:29:06 -05:00
improvement: better handling of out-of-protocol messages
This commit is contained in:
parent
8250e00f05
commit
99e117f620
28
src/main.rs
28
src/main.rs
|
@ -327,16 +327,6 @@ pub enum NostrMessage {
|
||||||
CloseMsg(CloseCmd),
|
CloseMsg(CloseCmd),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Nostr protocol messages from a relay/server
|
|
||||||
#[derive(Deserialize, Serialize, Clone, PartialEq, Debug)]
|
|
||||||
pub enum NostrResponse {
|
|
||||||
/// A `NOTICE` response
|
|
||||||
NoticeRes(String),
|
|
||||||
/// An `EVENT` response, composed of the subscription identifier,
|
|
||||||
/// and serialized event JSON
|
|
||||||
EventRes(String, String),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Convert Message to NostrMessage
|
/// Convert Message to NostrMessage
|
||||||
fn convert_to_msg(msg: String) -> Result<NostrMessage> {
|
fn convert_to_msg(msg: String) -> Result<NostrMessage> {
|
||||||
let config = config::SETTINGS.read().unwrap();
|
let config = config::SETTINGS.read().unwrap();
|
||||||
|
@ -372,14 +362,6 @@ async fn nostr_server(
|
||||||
) {
|
) {
|
||||||
// get a broadcast channel for clients to communicate on
|
// get a broadcast channel for clients to communicate on
|
||||||
let mut bcast_rx = broadcast.subscribe();
|
let mut bcast_rx = broadcast.subscribe();
|
||||||
// upgrade the TCP connection to WebSocket
|
|
||||||
//let conn = tokio_tungstenite::accept_async_with_config(stream, Some(config)).await;
|
|
||||||
//let ws_stream = conn.expect("websocket handshake error");
|
|
||||||
// wrap websocket into a stream & sink of Nostr protocol messages
|
|
||||||
|
|
||||||
// don't wrap in a proto stream, because it broke pings.
|
|
||||||
//let mut nostr_stream = protostream::wrap_ws_in_nostr(ws_stream);
|
|
||||||
|
|
||||||
// Track internal client state
|
// Track internal client state
|
||||||
let mut conn = conn::ClientConn::new();
|
let mut conn = conn::ClientConn::new();
|
||||||
let cid = conn.get_client_prefix();
|
let cid = conn.get_client_prefix();
|
||||||
|
@ -431,13 +413,17 @@ async fn nostr_server(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// check if this client has a subscription
|
|
||||||
ws_next = ws_stream.next() => {
|
ws_next = ws_stream.next() => {
|
||||||
let protomsg = match ws_next {
|
// Consume text messages from the client, parse into Nostr messages.
|
||||||
|
let nostr_msg = match ws_next {
|
||||||
Some(Ok(Message::Text(m))) => {
|
Some(Ok(Message::Text(m))) => {
|
||||||
let msg_parse = convert_to_msg(m);
|
let msg_parse = convert_to_msg(m);
|
||||||
Some(msg_parse)
|
Some(msg_parse)
|
||||||
},
|
},
|
||||||
|
Some(Ok(Message::Binary(_))) => {
|
||||||
|
ws_stream.send(Message::Text(format!("[\"NOTICE\",\"{}\"]", "binary messages are not accepted"))).await.ok();
|
||||||
|
continue;
|
||||||
|
},
|
||||||
None | Some(Ok(Message::Close(_))) | Some(Err(WsError::AlreadyClosed)) | Some(Err(WsError::ConnectionClosed)) => {
|
None | Some(Ok(Message::Close(_))) | Some(Err(WsError::AlreadyClosed)) | Some(Err(WsError::ConnectionClosed)) => {
|
||||||
info!("Closing connection");
|
info!("Closing connection");
|
||||||
None
|
None
|
||||||
|
@ -449,7 +435,7 @@ async fn nostr_server(
|
||||||
};
|
};
|
||||||
|
|
||||||
// convert ws_next into proto_next
|
// convert ws_next into proto_next
|
||||||
match protomsg {
|
match nostr_msg {
|
||||||
Some(Ok(NostrMessage::EventMsg(ec))) => {
|
Some(Ok(NostrMessage::EventMsg(ec))) => {
|
||||||
// An EventCmd needs to be validated to be converted into an Event
|
// An EventCmd needs to be validated to be converted into an Event
|
||||||
// handle each type of message
|
// handle each type of message
|
||||||
|
|
Loading…
Reference in New Issue
Block a user