简体   繁体   中英

golang websocket.JSON.Receive invalid character 'q' looking for beginning of value

ERROR while receiving JSON data via websockets

invalid character 'q' looking for beginning of value

I am using "golang.org/x/net/websocket" package

I have a Message struct:

type Message struct {
    Sender     *Client `json:"sender",omitempty`
    Sendername string  `json:"senderName"`
    Message    string  `json:"message"`
}

And the code that i am using for receiving on server is:

var err error

    for {
        reply := Message{}
        if err = websocket.JSON.Receive(ws, &reply); err != nil {
            fmt.Println("Cannot receive " + err.Error())
            break
        }

        fmt.Println(reply)
}

And the JSON string I am passing is

{"senderName":"CJ","message":"DATA"}

Client side:

let msg = document.getElementById("message").value             
let obj = { senderName : "CJ", message : msg}             
console.log(JSON.stringify(obj))             
sock.send(msg);

I am not able to figure out the error, I think it is regarding the JSON data.

The issue is related to the data sent by the client. That data is likely not a json data.

The client is currently sending document.getElementById("message").value which is a string and not a json data.

The client code should be modified as follows:

sock.send(JSON.stringify(obj))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM