Merge pull request #3 from rtyler/default-for-typed-messages

Invoke the default handler when an unknown message type is sent
This commit is contained in:
R Tyler Croy 2020-07-05 20:14:40 -07:00 committed by GitHub
commit 87cfb1d3da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 0 deletions

View File

@ -349,6 +349,7 @@ impl<ServerState: 'static + Send + Sync, ClientState: 'static + Default + Send +
break;
}
let message = message.to_string();
let mut invoke_default = false;
if let Ok(envelope) = serde_json::from_str::<Envelope>(&message) {
debug!("Envelope deserialized: {:?}", envelope);
@ -359,6 +360,7 @@ impl<ServerState: 'static + Send + Sync, ClientState: 'static + Default + Send +
Some(handler.clone())
} else {
debug!("No handler found for message type: {}", envelope.ttype);
invoke_default = true;
None
}
}
@ -378,6 +380,10 @@ impl<ServerState: 'static + Send + Sync, ClientState: 'static + Default + Send +
}
}
} else {
invoke_default = true;
}
if invoke_default {
if let Some(response) = default.call(message, state.clone()).await {
channel_tx.send(response).await;
}