Control message

The following message type is necessary to subscribe or unsubscribe from RepeaterUpdate messages. Add the following enumeration:

pub enum RepeaterControl {
Subscribe(Recipient<RepeaterUpdate>),
Unsubscribe(Recipient<RepeaterUpdate>),
}

It has two variants with the same Recipient<RepeaterUpdate> type inside. Actors will send their own Recipient addresses to start listening for updates or to stop any notifications about new comments.

Implement the Message trait for the RepeaterControl struct to turn it into the message type and use an empty Result associated type:

impl Message for RepeaterControl {
type Result = ();
}

Now, we can implement a Handler trait for the RepeaterControl message:

impl Handler<RepeaterControl> for RepeaterActor {
type Result = ();

fn handle(&mut self, msg: RepeaterControl, _: &mut Self::Context) -> Self::Result {
match msg {
RepeaterControl::Subscribe(listener) => {
self.listeners.insert(listener);
}
RepeaterControl::Unsubscribe(listener) => {
self.listeners.remove(&listener);
}
}
}
}

The implementation of the preceding handler is also pretty simple: it adds a new Recipient to listeners set on the Subscribe message variant, and removes the Recipient upon Unsubscribe messages.

The actor that resends NewComment values to other actors is ready, and now we can start to implement an actor for handling WebSocket connections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.176.88