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 more