Struct trillium_channels::ChannelEvent
source · pub struct ChannelEvent { /* private fields */ }
Expand description
The messages passed between server and connected clients.
ChannelEvents contain a topic, event, payload, and if sent from a client, a unique reference identifier that can be used to respond to this event.
Most interfaces in this crate take an Into<ChannelEvent>
instead of a ChannelEvent directly, so that you can either implement Into
use trillium_channels::ChannelEvent;
use serde_json::{json, Value, to_string};
let event: ChannelEvent = ("topic", "event").into();
assert_eq!(event.topic(), "topic");
assert_eq!(event.event(), "event");
assert_eq!(event.payload(), &json!({}));
let event: ChannelEvent = ("topic", "event", &json!({"some": "payload"})).into();
assert_eq!(event.topic(), "topic");
assert_eq!(event.event(), "event");
assert_eq!(to_string(event.payload()).unwrap(), r#"{"some":"payload"}"#);
#[derive(serde::Serialize)]
struct SomePayload { payload: &'static str };
let event: ChannelEvent = ("topic", "event", &SomePayload { payload: "anything" }).into();
assert_eq!(event.topic(), "topic");
assert_eq!(event.event(), "event");
assert_eq!(to_string(event.payload()).unwrap(), r#"{"payload":"anything"}"#);
Implementations§
source§impl ChannelEvent
impl ChannelEvent
sourcepub fn build_reply(
&self,
event: impl Into<Cow<'static, str>>,
payload: &impl Serialize
) -> ChannelEvent
pub fn build_reply( &self, event: impl Into<Cow<'static, str>>, payload: &impl Serialize ) -> ChannelEvent
Construct a new ChannelEvent with the same reference as this ChannelEvent. Note that this is the only way of setting the reference on an event.
The event
argument can be either a String
or, more commonly, a &'static str
.
The topic will always be the same as the source ChannelEvent’s topic.
sourcepub fn reference(&self) -> Option<&str>
pub fn reference(&self) -> Option<&str>
Returns the reference field (“ref” in json) for this ChannelEvent, if one was provided by the client
sourcepub fn new(
topic: impl Into<Cow<'static, str>>,
event: impl Into<Cow<'static, str>>,
payload: &impl Serialize
) -> Self
pub fn new( topic: impl Into<Cow<'static, str>>, event: impl Into<Cow<'static, str>>, payload: &impl Serialize ) -> Self
Constructs a new ChannelEvent from topic, event, and a serializable payload. Use &() if no payload is needed.
Note that the reference cannot be set this way. To set a
reference, use ChannelEvent::build_reply
Trait Implementations§
source§impl Clone for ChannelEvent
impl Clone for ChannelEvent
source§fn clone(&self) -> ChannelEvent
fn clone(&self) -> ChannelEvent
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for ChannelEvent
impl Debug for ChannelEvent
source§impl<'de> Deserialize<'de> for ChannelEvent
impl<'de> Deserialize<'de> for ChannelEvent
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl<T, E> From<(T, E)> for ChannelEvent
impl<T, E> From<(T, E)> for ChannelEvent
source§impl<T, E, P> From<(T, E, P)> for ChannelEvent
impl<T, E, P> From<(T, E, P)> for ChannelEvent
source§impl PartialEq for ChannelEvent
impl PartialEq for ChannelEvent
source§fn eq(&self, other: &ChannelEvent) -> bool
fn eq(&self, other: &ChannelEvent) -> bool
self
and other
values to be equal, and is used
by ==
.