1

I am using the gorilla websocket library for golang: http://www.gorillatoolkit.org/pkg/websocket

This is the code I am using to create the websocket connection:

conn, err := upgrader.Upgrade(w, r, nil)

Having an issue with sending JSON from golang to javascript. I can get it working but have to do what seem to be unnecessary steps. Here is the basics of the golang backend that does not work:

    type clientDB struct{
         ChunksWritten int64
         ChunksRead int64
         BytesWritten   int64
         BytesRead    int64
         DataBytesWritten int64
         DataBytesRead    int64
         ActivePeers   int
         TotalPeers    int
         TorrentHashString string
}

fullClientDB := new(clientDB) //creating a new clientDB struct

b, err := json.Marshal(fullClientDB)
        if err != nil {
            fmt.Println(err)
            return
        }


conn.WriteJSON(b)

When I use JSON.parse in javascript I get the following response:

var clientUpdate = JSON.parse(evt.data);

eyJDaHVua3NXcml0dGVuIjowLCJDaHVua3NSZWFkIjowLCJCeXRlc1dyaXR0ZW4iOjU0NDgxLCJCeXRlc1JlYWQiOjc4NzgyLCJEYXRhQnl0ZXNXcml0dGVuIjowLCJEYXRhQnl0ZXNSZWFkIjowLCJBY3RpdmVQZWVycyI6MCwiVG90YWxQZWVycyI6NDMsIlRvcnJlbnRIYXNoU3RyaW5nIjoiOWY5MTY1ZDlhMjgxYTliOGU3ODJjZDUxNzZiYmNjODI1NmZkMTg3MSJ9

I can get it working making the following changes:

conn.WriteJSON(string(b))

Then in javascript I actually parse the data TWICE.

var clientUpdate = JSON.parse(evt.data);
var clientUpdateJSON = JSON.parse(clientUpdate);

After that I can access the data correctly as a JSON object. Is there something I'm missing about sending JSON objects from golang to javascript over websockets?

8
  • Somewhere you are base64 encoding the data. The string you posted after base64 decode is {"ChunksWritten":0,"ChunksRead":0,"BytesWritten":54481,"BytesRead":78782,"DataBytesWritten":0,"DataBytesRead":0,"ActivePeers":0,"TotalPeers":43,"TorrentHashString":"9f9165d9a281a9b8e782cd5176bbcc8256fd1871"} Commented Sep 26, 2017 at 21:02
  • I don't know what kind of conn has a WriteJSON method, but it looks like you're taking a slice which is already JSON, and encoding it again as JSON, which is probably not what you want to do. Commented Sep 26, 2017 at 21:07
  • Sorry forgot to add the library I am using: gorillatoolkit.org/pkg/websocket This is creating the connection: conn, err := upgrader.Upgrade(w, r, nil) Commented Sep 26, 2017 at 21:08
  • 1
    Then only use WriteJSON or json.Marshal; you don't need to encode it twice. Commented Sep 26, 2017 at 21:10
  • 1
    @RayfenWindspear: base64 is the default json encoding format for a []byte Commented Sep 26, 2017 at 21:17

1 Answer 1

2

The gorilla websocket package automatically encodes to JSON so using the standard library to encode as well was just encoding it twice, causing it to show up as base64.

Thanks guys!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.