Mercurial > public > pacobot
comparison socket/conn.go @ 10:5c124578fed2
fix timer bug
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Sat, 15 Mar 2025 17:03:53 +0000 |
parents | e9df3bb010f4 |
children | 6d91c612310a |
comparison
equal
deleted
inserted
replaced
9:228ab74e8321 | 10:5c124578fed2 |
---|---|
1 package socket | 1 package socket |
2 | 2 |
3 import ( | 3 import ( |
4 "encoding/json" | 4 "encoding/json" |
5 "fmt" | |
6 "log" | 5 "log" |
7 "net/url" | 6 "net/url" |
8 "os" | 7 "os" |
9 "os/signal" | 8 "os/signal" |
10 "strings" | 9 "strings" |
31 defer conn.Close() | 30 defer conn.Close() |
32 | 31 |
33 log.Println("socket: connected") | 32 log.Println("socket: connected") |
34 | 33 |
35 var timeout time.Ticker | 34 var timeout time.Ticker |
36 done := make(chan bool) | 35 done := make(chan struct{}) |
37 | 36 |
38 go readMsg(done, conn, &timeout, authToken) | 37 go readMsg(done, conn, &timeout, authToken) |
39 | 38 |
40 for { | 39 for { |
41 select { | 40 select { |
56 Connect(authToken) | 55 Connect(authToken) |
57 } | 56 } |
58 } | 57 } |
59 } | 58 } |
60 | 59 |
61 func readMsg(done chan bool, conn *websocket.Conn, timeout *time.Ticker, authToken string) { | 60 func readMsg(done chan struct{}, conn *websocket.Conn, timeout *time.Ticker, authToken string) { |
61 defer close(done) | |
62 var timeout_secs time.Duration | 62 var timeout_secs time.Duration |
63 | 63 |
64 for { | 64 for { |
65 log.Println("socket: waiting for msg...") | 65 log.Println("socket: waiting for msg...") |
66 _, msg, err := conn.ReadMessage() | 66 _, msg, err := conn.ReadMessage() |
112 | 112 |
113 if err := json.Unmarshal(msg, &resNotifChannelChatMsg); err != nil { | 113 if err := json.Unmarshal(msg, &resNotifChannelChatMsg); err != nil { |
114 log.Fatal(err) | 114 log.Fatal(err) |
115 } | 115 } |
116 | 116 |
117 // TODO: Add to a function | |
118 jsonFormatted, err := json.MarshalIndent(resNotifChannelChatMsg, "", " ") | |
119 | |
120 if err != nil { | |
121 log.Fatalf("socket: error al formatear") | |
122 } | |
123 | |
124 // log.Println(resNotifChannelChatMsg.Payload.Event) | |
125 fmt.Print(string(jsonFormatted)) | |
126 | |
127 chatMsg := resNotifChannelChatMsg.Payload.Event.Msg.Text | 117 chatMsg := resNotifChannelChatMsg.Payload.Event.Msg.Text |
128 log.Println(chatMsg) | |
129 | 118 |
130 if strings.HasPrefix(chatMsg, "!") { | 119 if strings.HasPrefix(chatMsg, "!") { |
131 bot.HandleCmd(strings.Split(chatMsg[1:], " ")) | 120 go bot.HandleCmd(strings.Split(chatMsg[1:], " ")) |
132 } | 121 } |
133 } | 122 } |
134 default: | 123 default: |
135 log.Fatalf("%s: message type not implemented", msgType) | 124 log.Fatalf("socket: %s message type not implemented", msgType) |
136 } | 125 } |
137 } | 126 } |
138 | |
139 done <- true | |
140 } | 127 } |
141 | 128 |
142 func closeConn(conn *websocket.Conn) { | 129 func closeConn(conn *websocket.Conn) { |
143 err := conn.WriteMessage( | 130 err := conn.WriteMessage( |
144 websocket.CloseMessage, | 131 websocket.CloseMessage, |