[英]golang websocket.JSON.Receive invalid character 'q' looking for beginning of value
ERROR while receiving JSON data via websockets通过 websockets 接收 JSON 数据时出错
invalid character 'q' looking for beginning of value寻找值开头的无效字符“q”
I am using "golang.org/x/net/websocket" package我正在使用“golang.org/x/net/websocket”包
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我传递的 JSON 字符串是
{"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.我无法找出错误,我认为它与 JSON 数据有关。
The issue is related to the data sent by the client.该问题与客户端发送的数据有关。 That data is likely not a json data.该数据可能不是 json 数据。
The client is currently sending document.getElementById("message").value
which is a string and not a json data.客户端当前正在发送document.getElementById("message").value
它是一个字符串而不是一个 json 数据。
The client code should be modified as follows:客户端代码应修改如下:
sock.send(JSON.stringify(obj))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.