Use custom Send methods

This commit is contained in:
Taylor Gerring 2015-01-28 10:22:37 -06:00
parent e9d017ba08
commit a88f609b8a

View File

@ -14,7 +14,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/ */
package ws package rpcws
import ( import (
"fmt" "fmt"
@ -30,6 +30,7 @@ import (
) )
var wslogger = logger.NewLogger("RPC-WS") var wslogger = logger.NewLogger("RPC-WS")
var JSON rpc.JsonWrapper
type WebSocketServer struct { type WebSocketServer struct {
eth *eth.Ethereum eth *eth.Ethereum
@ -98,25 +99,28 @@ func (s *WebSocketServer) apiHandler(api *rpc.EthereumApi) http.Handler {
func sockHandler(api *rpc.EthereumApi) websocket.Handler { func sockHandler(api *rpc.EthereumApi) websocket.Handler {
fn := func(conn *websocket.Conn) { fn := func(conn *websocket.Conn) {
for { for {
wslogger.Debugln("Handling request") wslogger.Debugln("Handling connection")
var reqParsed rpc.RpcRequest var reqParsed rpc.RpcRequest
// reqParsed, reqerr := JSON.ParseRequestBody(conn.Request())
if err := websocket.JSON.Receive(conn, &reqParsed); err != nil { if err := websocket.JSON.Receive(conn, &reqParsed); err != nil {
wslogger.Debugln(rpc.ErrorParseRequest) // if reqerr != nil {
websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest}) JSON.Send(conn, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest})
// websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest})
continue continue
} }
var response interface{} var response interface{}
reserr := api.GetRequestReply(&reqParsed, &response) reserr := api.GetRequestReply(&reqParsed, &response)
if reserr != nil { if reserr != nil {
wslogger.Errorln(reserr) // websocket.JSON.Send(conn, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()})
websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()}) JSON.Send(conn, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()})
continue continue
} }
wslogger.Debugf("Generated response: %T %s", response, response) wslogger.Debugf("Generated response: %T %s", response, response)
websocket.JSON.Send(conn, rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response}) JSON.Send(conn, &rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response})
// websocket.JSON.Send(conn, rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response})
} }
} }
return websocket.Handler(fn) return websocket.Handler(fn)