Answer the question
In order to leave comments, you need to log in
How to correctly pass json data through websocket in golang?
There is a string of json data written in sqlite as TEXT.
The data is transmitted via gorilla-websocket. As part of the structure.
type SourseText struct {
id int `json:"id"`
sText string `json:"stext, omitempty"`
}
....
sText := '{"channel":"buu","name":"john", "msg":"doe"}'
....
err = conn.WriteJSON(SourseText)
"{"id":{"1"},{\"channel\":\"buu\",\"name\":\"john\", \"msg\":\"doe\"}}"
Answer the question
In order to leave comments, you need to log in
WriteJSON expects a structure as input, which it then marshals into JSON. Your JSON is already marshaled, so use the WriteMessage method. The first argument there is the type of the message, you need binary, the second is directly JSON in the form of an array of bytes. A string can be converted to an array of bytes like this: []byte(sText)
WriteJSON, as it were, hints, and you already give him ready-made JSON.
It turns out double JSON.
https://github.com/gorilla/websocket/blob/2d1e4548...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question