diff --git a/irc.go b/irc.go index 6d7a4da..2dd0cad 100644 --- a/irc.go +++ b/irc.go @@ -26,7 +26,7 @@ func reader(irc *IRCConnection) { irc.Error <- err break } - irc.lastMessage = time.Seconds() + irc.lastMessage = time.Now() msg = msg[0 : len(msg)-2] //Remove \r\n event := &IRCEvent{Raw: msg} if msg[0] == ':' { @@ -75,18 +75,18 @@ func writer(irc *IRCConnection) { //Pings the server if we have not recived any messages for 5 minutes func pinger(i *IRCConnection) { - i.ticker = time.Tick(1000 * 1000 * 1000 * 60 * 1) //Tick every minute. - i.ticker2 = time.Tick(1000 * 1000 * 1000 * 60 * 15) //Tick every 15 minutes. + i.ticker = time.Tick(1 * time.Minute) //Tick every minute. + i.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes. for { select { case <-i.ticker: //Ping if we haven't recived anything from the server within 4 minutes - if time.Seconds()-i.lastMessage >= 60*4 { - i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds())) + if time.Since(i.lastMessage) >= (4 * time.Minute) { + i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano())) } case <-i.ticker2: //Ping every 15 minutes. - i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds())) + i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano())) //Try to recapture nickname if it's not as configured. if i.nick != i.nickcurrent { i.nickcurrent = i.nick diff --git a/irc_callback.go b/irc_callback.go index 018fec5..b6a922b 100644 --- a/irc_callback.go +++ b/irc_callback.go @@ -1,10 +1,10 @@ package irc import ( - "strings" "fmt" - "time" "strconv" + "strings" + "time" ) func (irc *IRCConnection) AddCallback(eventcode string, callback func(*IRCEvent)) { @@ -80,7 +80,7 @@ func (irc *IRCConnection) setupCallbacks() { }) irc.AddCallback("CTCP_TIME", func(e *IRCEvent) { - ltime := time.LocalTime() + ltime := time.Now() irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01TIME %s\x01", e.Nick, ltime.String())) }) @@ -101,8 +101,9 @@ func (irc *IRCConnection) setupCallbacks() { }) irc.AddCallback("PONG", func(e *IRCEvent) { - ns, _ := strconv.Atoi64(e.Message) - fmt.Printf("Lag: %fs\n", float32((time.Nanoseconds()-ns))/1000/1000/1000) + ns, _ := strconv.ParseInt(e.Message, 10, 64) + delta := time.Duration(time.Now().UnixNano() - ns) + fmt.Printf("Lag: %vs\n", delta) }) irc.AddCallback("NICK", func(e *IRCEvent) { diff --git a/irc_struct.go b/irc_struct.go index ba3ec1e..fb1b788 100644 --- a/irc_struct.go +++ b/irc_struct.go @@ -4,7 +4,10 @@ package irc -import "net" +import ( + "net" + "time" +) type IRCConnection struct { socket net.Conn @@ -19,9 +22,9 @@ type IRCConnection struct { Password string events map[string][]func(*IRCEvent) - lastMessage int64 - ticker <-chan int64 - ticker2 <-chan int64 + lastMessage time.Time + ticker <-chan time.Time + ticker2 <-chan time.Time VerboseCallbackHandler bool