mirror of
https://github.com/weaveworks/scope.git
synced 2026-05-03 07:49:10 +00:00
* New encoding format:
* Msgpack reports between probe<->app (smaller representation, faster to
encode/decode).
* Still use JSON between app<->UI (try to avoid making javascript deal with
mspack).
The app still suports publishing reports in both gob and JSON, not braking
backwards compatibility.
* Use compile-time generated marshallers/unmarshallers for higher performance. In
order to be able to skip code-generation for certain types, I included
https://github.com/2opremio/go-1/tree/master/codec/codecgen instead of
upstream until https://github.com/ugorji/go/pull/139 is merged.
* Encode/decode intermediate types using github.com/ugorji/go/codec.Selfer
for higher performance and reducing garbage collection (no temporary buffers).
38 lines
843 B
Go
38 lines
843 B
Go
package xfer
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/gorilla/websocket"
|
|
"github.com/ugorji/go/codec"
|
|
)
|
|
|
|
// WriteJSONtoWS writes the JSON encoding of v to the connection.
|
|
func WriteJSONtoWS(c *websocket.Conn, v interface{}) error {
|
|
w, err := c.NextWriter(websocket.TextMessage)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err1 := codec.NewEncoder(w, &codec.JsonHandle{}).Encode(v)
|
|
err2 := w.Close()
|
|
if err1 != nil {
|
|
return err1
|
|
}
|
|
return err2
|
|
}
|
|
|
|
// ReadJSONfromWS reads the next JSON-encoded message from the connection and stores
|
|
// it in the value pointed to by v.
|
|
func ReadJSONfromWS(c *websocket.Conn, v interface{}) error {
|
|
_, r, err := c.NextReader()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = codec.NewDecoder(r, &codec.JsonHandle{}).Decode(v)
|
|
if err == io.EOF {
|
|
// One value is expected in the message.
|
|
err = io.ErrUnexpectedEOF
|
|
}
|
|
return err
|
|
}
|