1
0
Fork 0
mirror of https://github.com/fluffle/goirc synced 2025-09-06 00:43:20 +00:00

Fix for issues/6 (2/2): Move to using control channels and select.

This commit is contained in:
Alex Bramley 2011-07-22 01:08:42 +01:00
parent eb51558009
commit 0200b741dc
2 changed files with 88 additions and 44 deletions

View file

@ -15,6 +15,11 @@ func main() {
c.AddHandler("connected",
func(conn *irc.Conn, line *irc.Line) { conn.Join("#go-nuts") })
// Set up a handler to notify of disconnect events.
quit := make(chan bool)
c.AddHandler("disconnected",
func(conn *irc.Conn, line *irc.Line) { quit <- true })
// connect to server
if err := c.Connect("irc.freenode.net"); err != nil {
fmt.Printf("Connection error: %s\n", err)
@ -74,18 +79,18 @@ func main() {
}
}()
// stall here waiting for asplode on error channel
for {
for err := range c.Err {
for !reallyquit {
select {
case err := <-c.Err:
fmt.Printf("goirc error: %s\n", err)
}
if reallyquit {
break
}
fmt.Println("Reconnecting...")
if err := c.Connect("irc.freenode.net"); err != nil {
fmt.Printf("Connection error: %s\n", err)
break
case <-quit:
if !reallyquit {
fmt.Println("Reconnecting...")
if err := c.Connect("irc.freenode.net"); err != nil {
fmt.Printf("Connection error: %s\n", err)
reallyquit = true
}
}
}
}
}