func WriteRPCResponse(w http.ResponseWriter, res RPCResponse) { buf, n, err := new(bytes.Buffer), new(int64), new(error) binary.WriteJSON(res, buf, n, err) if *err != nil { log.Warn("Failed to write RPC response", "error", err) } w.Header().Set("Content-Type", "application/json") w.WriteHeader(200) w.Write(buf.Bytes()) }
func binaryWriter(args ...interface{}) ([]interface{}, error) { list := []interface{}{} for _, a := range args { buf, n, err := new(bytes.Buffer), new(int64), new(error) binary.WriteJSON(a, buf, n, err) if *err != nil { return nil, *err } list = append(list, buf.Bytes()) } return list, nil }
func argsToJson(args ...interface{}) ([]string, error) { l := len(args) jsons := make([]string, l) n, err := new(int64), new(error) for i, a := range args { buf := new(bytes.Buffer) binary.WriteJSON(a, buf, n, err) if *err != nil { return nil, *err } jsons[i] = string(buf.Bytes()) } return jsons, nil }
// receives on a write channel and writes out on the socket func (con *WSConnection) write() { defer con.wsConn.Close() n, err := new(int64), new(error) for { select { case msg := <-con.writeChan: buf := new(bytes.Buffer) binary.WriteJSON(msg, buf, n, err) if *err != nil { log.Error("Failed to marshal WSResponse to JSON", "error", err) } else { if err := con.wsConn.WriteMessage(websocket.TextMessage, buf.Bytes()); err != nil { log.Error("Failed to write response on websocket", "error", err) con.Stop() return } } case <-con.quitChan: return } } }