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:
parent
eb51558009
commit
0200b741dc
2 changed files with 88 additions and 44 deletions
27
client.go
27
client.go
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue