forked from cerc-io/plugeth
04e175b8ec
* rpc: implement websockets with github.com/gorilla/websocket This change makes package rpc use the github.com/gorilla/websocket package for WebSockets instead of golang.org/x/net/websocket. The new library is more robust and supports all WebSocket features including continuation frames. There are new tests for two issues with the previously-used library: - TestWebsocketClientPing checks handling of Ping frames. - TestWebsocketLargeCall checks whether the request size limit is applied correctly. * rpc: raise HTTP/WebSocket request size limit to 5MB * rpc: remove default origin for client connections The client used to put the local hostname into the Origin header because the server wanted an origin to accept the connection, but that's silly: Origin is for browsers/websites. The nobody would whitelist a particular hostname. Now that the server doesn't need Origin anymore, don't bother setting one for clients. Users who need an origin can use DialWebsocket to create a client with arbitrary origin if needed. * vendor: put golang.org/x/net/websocket back * rpc: don't set Origin header for empty (default) origin * rpc: add HTTP status code to handshake error This makes it easier to debug failing connections. * ethstats: use github.com/gorilla/websocket * rpc: fix lint
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
// Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package websocket
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
)
|
|
|
|
// WriteJSON writes the JSON encoding of v as a message.
|
|
//
|
|
// Deprecated: Use c.WriteJSON instead.
|
|
func WriteJSON(c *Conn, v interface{}) error {
|
|
return c.WriteJSON(v)
|
|
}
|
|
|
|
// WriteJSON writes the JSON encoding of v as a message.
|
|
//
|
|
// See the documentation for encoding/json Marshal for details about the
|
|
// conversion of Go values to JSON.
|
|
func (c *Conn) WriteJSON(v interface{}) error {
|
|
w, err := c.NextWriter(TextMessage)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err1 := json.NewEncoder(w).Encode(v)
|
|
err2 := w.Close()
|
|
if err1 != nil {
|
|
return err1
|
|
}
|
|
return err2
|
|
}
|
|
|
|
// ReadJSON reads the next JSON-encoded message from the connection and stores
|
|
// it in the value pointed to by v.
|
|
//
|
|
// Deprecated: Use c.ReadJSON instead.
|
|
func ReadJSON(c *Conn, v interface{}) error {
|
|
return c.ReadJSON(v)
|
|
}
|
|
|
|
// ReadJSON reads the next JSON-encoded message from the connection and stores
|
|
// it in the value pointed to by v.
|
|
//
|
|
// See the documentation for the encoding/json Unmarshal function for details
|
|
// about the conversion of JSON to a Go value.
|
|
func (c *Conn) ReadJSON(v interface{}) error {
|
|
_, r, err := c.NextReader()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = json.NewDecoder(r).Decode(v)
|
|
if err == io.EOF {
|
|
// One value is expected in the message.
|
|
err = io.ErrUnexpectedEOF
|
|
}
|
|
return err
|
|
}
|