pub fn connect<A: AsRef<str>>(
address: A,
reco_settings: ReconnectionSettings,
encoding: EncodingRef,
) -> Result<(Writer, Reader)>
Expand description
Create a connection to the given address.
A Writer
/Reader
pair is returned. If the connection fails,
an error is returned.
If you don’t want to reconnect, use ReconnectionSettings::DoNotReconnect
.
Examples found in repository?
examples/peekaboo.rs (lines 16-18)
11fn main() {
12 let args: Vec<String> = env::args().collect();
13 let channel = args.get(1).expect("Channel must be given as an argument.");
14
15 // Connect to freenode and use no not reconnect.
16 let (writer, reader) = connect("irc.freenode.net:6667",
17 ReconnectionSettings::DoNotReconnect,
18 UTF_8).unwrap();
19 writer.raw(format!("USER {} 8 * :{}\n", "peekaboo", "peekaboo"));
20 writer.raw(format!("NICK {}\n", "peekaboo"));
21
22 // Receive events.
23 for event in reader.iter() {
24 println!("{:?}", event);
25 match event {
26 Event::Message(msg) => {
27 if msg.code == Code::RplWelcome {
28 // join channel, no password
29 writer.raw(format!("JOIN {}\n", channel));
30 }
31 // JOIN is sent when you join a channel.
32 if msg.code == Code::Join {
33 // If there is a prefix...
34 if let Some(prefix) = msg.prefix {
35 match prefix {
36 // And the prefix is a user...
37 Prefix::User(user) => {
38 // And that user's nick is peekaboo, we've joined the channel!
39 if user.nickname == "peekaboo" {
40 writer.raw(format!("PRIVMSG {} :{}\n", channel, "peekaboo"));
41 // Note that if the reconnection settings said to reconnect,
42 // it would. Close would "really" stop it.
43 writer.raw(format!("QUIT :{}\n", "peekaboo"));
44 // writer.close();
45 }
46 }
47 _ => {}
48 }
49 }
50 }
51 }
52 _ => {}
53 }
54 }
55}