fix delay
This commit is contained in:
parent
ace2ff22e9
commit
43ef0a50fc
15
main.go
15
main.go
@ -4,6 +4,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.in/gcfg.v1"
|
"gopkg.in/gcfg.v1"
|
||||||
@ -42,11 +43,12 @@ var (
|
|||||||
|
|
||||||
cfg Config
|
cfg Config
|
||||||
|
|
||||||
ownUserID64 = int64(0)
|
ownUserID64 = int64(0)
|
||||||
ownUserID32 = int32(0)
|
ownUserID32 = int32(0)
|
||||||
lastOwnTDMsg time.Time
|
lastOwnTDMsg time.Time
|
||||||
MQCWMsgQueue chan ChatWarsMessage
|
lastOwnTDMsgMux sync.Mutex
|
||||||
MQTGCmdQueue chan TGCommand
|
MQCWMsgQueue chan ChatWarsMessage
|
||||||
|
MQTGCmdQueue chan TGCommand
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -133,7 +135,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
go MQKeepAliveWorker()
|
go MQKeepAliveWorker()
|
||||||
|
|
||||||
|
lastOwnTDMsgMux.Lock()
|
||||||
lastOwnTDMsg = time.Now()
|
lastOwnTDMsg = time.Now()
|
||||||
|
lastOwnTDMsgMux.Unlock()
|
||||||
|
|
||||||
go ListenCW(client)
|
go ListenCW(client)
|
||||||
go ListenMQ(client, MQTGCmdQueue)
|
go ListenMQ(client, MQTGCmdQueue)
|
||||||
go ListenMe(client)
|
go ListenMe(client)
|
||||||
|
18
td.go
18
td.go
@ -30,11 +30,6 @@ func ListenMe(c *tdlib.Client) {
|
|||||||
|
|
||||||
func ListenMQ(c *tdlib.Client, msgs <-chan TGCommand) {
|
func ListenMQ(c *tdlib.Client, msgs <-chan TGCommand) {
|
||||||
for m := range msgs {
|
for m := range msgs {
|
||||||
for now := time.Now(); lastOwnTDMsg.Add(time.Second).After(now); {
|
|
||||||
log.Printf("ListenMQ : channel busy, waiting before acting...\n")
|
|
||||||
time.Sleep(time.Until(lastOwnTDMsg.Add(time.Second)))
|
|
||||||
now = time.Now()
|
|
||||||
}
|
|
||||||
b, _ := json.Marshal(m)
|
b, _ := json.Marshal(m)
|
||||||
log.Printf("****************************** New MQ command ******************************\n%s\n****************************************************************************\n", string(b))
|
log.Printf("****************************** New MQ command ******************************\n%s\n****************************************************************************\n", string(b))
|
||||||
go clientMsg(c, m)
|
go clientMsg(c, m)
|
||||||
@ -224,9 +219,22 @@ func OwnUserID(c *tdlib.Client) int32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func clientMsg(c *tdlib.Client, m TGCommand) {
|
func clientMsg(c *tdlib.Client, m TGCommand) {
|
||||||
|
log.Printf("clientMsg : Delaying message by %d seconds.\n", m.Delay)
|
||||||
if m.Delay != 0 {
|
if m.Delay != 0 {
|
||||||
time.Sleep(time.Duration(m.Delay) * time.Second)
|
time.Sleep(time.Duration(m.Delay) * time.Second)
|
||||||
}
|
}
|
||||||
|
for {
|
||||||
|
lastOwnTDMsgMux.Lock()
|
||||||
|
now := time.Now()
|
||||||
|
if lastOwnTDMsg.Add(time.Second).Before(now) {
|
||||||
|
lastOwnTDMsg = now
|
||||||
|
lastOwnTDMsgMux.Unlock()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
lastOwnTDMsgMux.Unlock()
|
||||||
|
time.Sleep(time.Until(now.Add(time.Second)))
|
||||||
|
}
|
||||||
|
|
||||||
switch m.Type {
|
switch m.Type {
|
||||||
case commandSendMsg:
|
case commandSendMsg:
|
||||||
msgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(m.Text, nil), true, true)
|
msgTxt := tdlib.NewInputMessageText(tdlib.NewFormattedText(m.Text, nil), true, true)
|
||||||
|
Loading…
Reference in New Issue
Block a user