Fix some encoding errors

This commit is contained in:
Hamcha 2016-02-09 10:04:27 +00:00
parent 5ffb054dfa
commit 85284e40be
3 changed files with 19 additions and 7 deletions

View file

@ -41,7 +41,7 @@ func handleClient(c net.Conn) {
var cmd tg.ClientCommand var cmd tg.ClientCommand
err = json.Unmarshal(bytes, &cmd) err = json.Unmarshal(bytes, &cmd)
if err != nil { if err != nil {
log.Printf("Can't parse JSON: %s\r\n", err.Error()) log.Printf("[handleClient] Can't parse JSON: %s\r\n", err.Error())
continue continue
} }
executeClientCommand(cmd) executeClientCommand(cmd)

View file

@ -1,17 +1,29 @@
package main package main
import ( import (
"io/ioutil" "encoding/json"
"log"
"net/http" "net/http"
"../tg"
) )
func webhook(rw http.ResponseWriter, req *http.Request) { func webhook(rw http.ResponseWriter, req *http.Request) {
// Read entire request and broadcast to everyone defer req.Body.Close()
data, err := ioutil.ReadAll(req.Body)
// Re-encode request to ensure conformity
var update tg.APIUpdate
err := json.NewDecoder(req.Body).Decode(&update)
if err != nil { if err != nil {
log.Println("[webhook] Received incorrect request: " + err.Error())
return
}
data, err := json.Marshal(update)
if err != nil {
log.Println("[webhook] Cannot re-encode json (??) : " + err.Error())
return return
} }
defer req.Body.Close()
broadcast(string(data)) broadcast(string(data))
} }

View file

@ -34,9 +34,9 @@ func (b *Broker) SendTextMessage(chat *APIChat, text string) {
}, },
} }
// Encode command and send to broker // Encode command and send to broker
err := json.NewEncoder(b.Socket).Encode(&cmd) data, err := json.Marshal(cmd)
if err != nil { if err != nil {
log.Printf("[SendTextMessage] JSON Encode error: %s\n", err.Error()) log.Printf("[SendTextMessage] JSON Encode error: %s\n", err.Error())
} }
fmt.Fprintf(b.Socket, "\n") fmt.Fprintln(data)
} }