aatxe / irc

the irc crate – usable, async IRC for Rust
Mozilla Public License 2.0
528 stars 97 forks source link

External input to Client #140

Closed HeapUnderfl0w closed 6 years ago

HeapUnderfl0w commented 6 years ago

I currently have the situation where i do not use the crate for a bot, but instead as a back-end for a user facing UI. At the moment i get information OUT of the event system by just stuffing a mpsc::Sender into the function and output over that.

reactor.register_client_with_handler(client, move |client, message| {
    let sender = send.clone();
    client::client_handler_fn(client, message, sender, uu.get_stop())
});

But this does not work if i want to send messages over the client connection, as the client itself is consumed by the register function.

Is there any approach i am missing, which i could use to pass messages to the client (which in turns sends them) ?

aatxe commented 6 years ago

Thanks for the question! Probably the most important part is to know that IrcClient is cheap to clone ([doc pointer]). So, while you do need to give a copy to register_client_with_handler, you can clone a copy for yourself. Once you have a separate copy, the question of how to set up message sending is somewhat dependent on the input source, but as an example, you might have a channel where the receiving half just sends whatever it receives to the client.

HeapUnderfl0w commented 6 years ago

Ah, i just assumed that cloning the client itself would create some problems with the actual underlying connection.

I will try that out and update this issue after that.

HeapUnderfl0w commented 6 years ago

Works as intended, thank you very much.

aatxe commented 6 years ago

Fantastic! Cheers! 🍻