Merge pull request #16 from presbrey/master

Loop: catch reconnection errors; writeLoop: only log.Print if Debug
This commit is contained in:
Thomas Jager 2014-02-02 03:20:45 -08:00
commit 5d143c7b85
3 changed files with 15 additions and 3 deletions

11
irc.go
View File

@ -74,7 +74,9 @@ func (irc *Connection) writeLoop() {
break break
} }
if irc.Debug {
irc.log.Printf("--> %s\n", b) irc.log.Printf("--> %s\n", b)
}
_, err := irc.socket.Write([]byte(b)) _, err := irc.socket.Write([]byte(b))
if err != nil { if err != nil {
irc.Error <- err irc.Error <- err
@ -120,7 +122,14 @@ func (irc *Connection) Loop() {
} }
irc.log.Printf("Error: %s\n", err) irc.log.Printf("Error: %s\n", err)
irc.Disconnect() irc.Disconnect()
irc.Connect(irc.server) for !irc.stopped {
if err = irc.Connect(irc.server); err != nil {
irc.log.Printf("Error: %s\n", err)
time.Sleep(1 * time.Second)
} else {
break
}
}
} }
} }

View File

@ -115,7 +115,9 @@ func (irc *Connection) setupCallbacks() {
irc.AddCallback("PONG", func(e *Event) { irc.AddCallback("PONG", func(e *Event) {
ns, _ := strconv.ParseInt(e.Message, 10, 64) ns, _ := strconv.ParseInt(e.Message, 10, 64)
delta := time.Duration(time.Now().UnixNano() - ns) delta := time.Duration(time.Now().UnixNano() - ns)
if irc.Debug {
irc.log.Printf("Lag: %vs\n", delta) irc.log.Printf("Lag: %vs\n", delta)
}
}) })
irc.AddCallback("NICK", func(e *Event) { irc.AddCallback("NICK", func(e *Event) {

View File

@ -12,6 +12,7 @@ import (
) )
type Connection struct { type Connection struct {
Debug bool
Error chan error Error chan error
Password string Password string
UseTLS bool UseTLS bool