Skip to content

Commit

Permalink
Use json-iterator/go instead of buger/jsonparser
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
Jianhui Zhao committed Apr 26, 2019
1 parent ffa0103 commit a323847
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
36 changes: 17 additions & 19 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ package main

import (
"fmt"
jsoniter "github.com/json-iterator/go"
"io"
"io/ioutil"
"net/http"
"sync"
"time"

"github.com/buger/jsonparser"
"github.com/gorilla/websocket"
)

Expand All @@ -50,20 +51,23 @@ var cmdErrMsg = map[int]string{

type commandStatus struct {
token string
resp []byte
resp string
t *time.Timer
}

type CommandInfo struct {
Devid string `json:"devid"`
Cmd string `json:"cmd"`
}

var commands sync.Map

func handleCmdResp(data []byte) {
token, _ := jsonparser.GetString(data, "token")
token := jsoniter.Get(data, "token").ToString()

cmd, ok := commands.Load(token)
if ok {
if cmd, ok := commands.Load(token); ok {
cmd := cmd.(*commandStatus)
attrs, _, _, _ := jsonparser.Get(data, "attrs")
cmd.resp = attrs
cmd.resp = jsoniter.Get(data, "attrs").ToString()
}
}

Expand All @@ -81,8 +85,7 @@ func serveCmd(br *Broker, w http.ResponseWriter, r *http.Request) {
cmdErrReply(RTTY_CMD_ERR_PENDING, w)
} else {
commands.Delete(token)

w.Write(cmd.resp)
io.WriteString(w, cmd.resp)
cmd.t.Stop()
}
} else {
Expand All @@ -94,19 +97,14 @@ func serveCmd(br *Broker, w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
r.Body.Close()

devid, err := jsonparser.GetString(body, "devid")
if err != nil {
cmdErrReply(RTTY_CMD_ERR_INVALID, w)
return
}

_, err = jsonparser.GetString(body, "cmd")
if err != nil {
cmdInfo := CommandInfo{}
err := jsoniter.Unmarshal(body, &cmdInfo)
if err != nil || cmdInfo.Cmd == "" || cmdInfo.Devid == "" {
cmdErrReply(RTTY_CMD_ERR_INVALID, w)
return
}

dev, ok := br.devices[devid]
dev, ok := br.devices[cmdInfo.Devid]
if !ok {
cmdErrReply(RTTY_CMD_ERR_OFFLINE, w)
return
Expand All @@ -123,7 +121,7 @@ func serveCmd(br *Broker, w http.ResponseWriter, r *http.Request) {

commands.Store(token, cmd)

msg := fmt.Sprintf(`{"type":"cmd","token":"%s","attrs":%s}`, token, string(body))
msg := fmt.Sprintf(`{"type":"cmd","token":"%s","attrs":%s}`, token, body)
dev.wsWrite(websocket.TextMessage, []byte(msg))

fmt.Fprintf(w, `{"token":"%s"}`, token)
Expand Down
6 changes: 3 additions & 3 deletions http.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"encoding/json"
"fmt"
jsoniter "github.com/json-iterator/go"
"github.com/rakyll/statik/fs"
log "github.com/sirupsen/logrus"
"github.com/zhaojh329/rttys/cache"
Expand Down Expand Up @@ -84,7 +84,7 @@ func httpStart(br *Broker, cfg *RttysConfig) {
var creds Credentials

// Get the JSON body and decode into credentials
err := json.NewDecoder(r.Body).Decode(&creds)
err := jsoniter.NewDecoder(r.Body).Decode(&creds)
if err != nil {
// If the structure of the body is wrong, return an HTTP error
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -121,7 +121,7 @@ func httpStart(br *Broker, cfg *RttysConfig) {

allowOrigin(w)

resp, _ := json.Marshal(devs)
resp, _ := jsoniter.Marshal(devs)

w.Write(resp)
})
Expand Down

0 comments on commit a323847

Please sign in to comment.