Ack data is marshalled twice
Describe the bug
Ack data is marshalled twice, producing a string like the following:
"{\"User\":true,\"Name\":\"foo\"}"
which throws an error client side
To Reproduce marshal a struct into bytes. convert to string. Return string.
Expected behavior when sending json in an acknowledgment, it should not be remarshalled
Can you include a code sample that can duplicate this issue?
Describe the bug Ack data is marshalled twice, producing a string like the following:
"{\"User\":true,\"Name\":\"foo\"}"which throws an error client sideTo Reproduce marshal a struct into bytes. convert to string. Return string.
Expected behavior when sending json in an acknowledgment, it should not be remarshalled
Rather than convert your struct to bytes and then string, the socket's Emit function allows you send your struct as-is. The Emit function handles serialization of your struct to json which you can automatically consume as json data on the client.
Instead of
m := Model{User: true, Name: "foo"}
marshalled, _ := json.Marshal(m)
s.Emit("ack", string(marshalled))
You should
m := Model{User: true, Name: "foo"}
s.Emit("ack", m)
and on your client
socket.on('ack', function(msg) {
console.log(msg) // would be {"User":true,"Name":"foo"}
console.log(msg.User) // true
console.log(msg.Name) // foo
}