Updated WS API. Fixes #219. Closes #220

This commit is contained in:
obscuren 2015-01-06 11:44:22 +01:00
parent 47e6b2cef8
commit a26aecdfdb
4 changed files with 38 additions and 42 deletions

View File

@ -33,73 +33,73 @@ func (self *WebSocketServer) Serv() {
data := ethutil.NewValue(msg.Args) data := ethutil.NewValue(msg.Args)
bcode, err := ethutil.Compile(data.Get(0).Str(), false) bcode, err := ethutil.Compile(data.Get(0).Str(), false)
if err != nil { if err != nil {
c.Write(args(nil, err.Error()), msg.Seed) c.Write(args(nil, err.Error()), msg.Id)
} }
code := ethutil.Bytes2Hex(bcode) code := ethutil.Bytes2Hex(bcode)
c.Write(args(code, nil), msg.Seed) c.Write(args(code, nil), msg.Id)
case "getBlockByNumber": case "eth_blockByNumber":
args := msg.Arguments() args := msg.Arguments()
block := pipe.BlockByNumber(int32(args.Get(0).Uint())) block := pipe.BlockByNumber(int32(args.Get(0).Uint()))
c.Write(block, msg.Seed) c.Write(block, msg.Id)
case "getKey": case "eth_blockByHash":
c.Write(pipe.Key().PrivateKey, msg.Seed) args := msg.Arguments()
case "transact":
c.Write(pipe.BlockByHash(args.Get(0).Str()), msg.Id)
case "eth_transact":
if mp, ok := msg.Args[0].(map[string]interface{}); ok { if mp, ok := msg.Args[0].(map[string]interface{}); ok {
object := mapToTxParams(mp) object := mapToTxParams(mp)
c.Write( c.Write(
args(pipe.Transact(object["from"], object["to"], object["value"], object["gas"], object["gasPrice"], object["data"])), args(pipe.Transact(pipe.Key().PrivateKey, object["to"], object["value"], object["gas"], object["gasPrice"], object["data"])),
msg.Seed, msg.Id,
) )
} }
case "getCoinBase": case "eth_gasPrice":
c.Write(pipe.CoinBase(), msg.Seed) c.Write("10000000000000", msg.Id)
case "eth_coinbase":
c.Write(pipe.CoinBase(), msg.Id)
case "getIsListening": case "eth_listening":
c.Write(pipe.IsListening(), msg.Seed) c.Write(pipe.IsListening(), msg.Id)
case "getIsMining": case "eth_mining":
c.Write(pipe.IsMining(), msg.Seed) c.Write(pipe.IsMining(), msg.Id)
case "getPeerCoint": case "eth_peerCount":
c.Write(pipe.PeerCount(), msg.Seed) c.Write(pipe.PeerCount(), msg.Id)
case "getCountAt": case "eth_countAt":
args := msg.Arguments() args := msg.Arguments()
c.Write(pipe.TxCountAt(args.Get(0).Str()), msg.Seed) c.Write(pipe.TxCountAt(args.Get(0).Str()), msg.Id)
case "getCodeAt": case "eth_codeAt":
args := msg.Arguments() args := msg.Arguments()
c.Write(len(pipe.CodeAt(args.Get(0).Str())), msg.Seed) c.Write(len(pipe.CodeAt(args.Get(0).Str())), msg.Id)
case "getBlockByHash": case "eth_storageAt":
args := msg.Arguments() args := msg.Arguments()
c.Write(pipe.BlockByHash(args.Get(0).Str()), msg.Seed) c.Write(pipe.StorageAt(args.Get(0).Str(), args.Get(1).Str()), msg.Id)
case "getStorageAt": case "eth_balanceAt":
args := msg.Arguments() args := msg.Arguments()
c.Write(pipe.StorageAt(args.Get(0).Str(), args.Get(1).Str()), msg.Seed) c.Write(pipe.BalanceAt(args.Get(0).Str()), msg.Id)
case "getBalanceAt": case "eth_secretToAddress":
args := msg.Arguments() args := msg.Arguments()
c.Write(pipe.BalanceAt(args.Get(0).Str()), msg.Seed) c.Write(pipe.SecretToAddress(args.Get(0).Str()), msg.Id)
case "getSecretToAddress": case "eth_newFilter":
args := msg.Arguments() case "eth_newFilterString":
case "eth_messages":
c.Write(pipe.SecretToAddress(args.Get(0).Str()), msg.Seed)
case "newFilter":
case "newFilterString":
case "messages":
// TODO // TODO
} }

View File

@ -50,8 +50,8 @@ func (c *Client) Conn() *ws.Conn {
return c.ws return c.ws
} }
func (c *Client) Write(data interface{}, seed int) { func (c *Client) Write(data interface{}, id int) {
msg := &Message{Seed: seed, Data: data} msg := &Message{Id: id, Data: data}
select { select {
case c.ch <- msg: case c.ch <- msg:
default: default:
@ -73,7 +73,6 @@ func (c *Client) Listen() {
// Listen write request via chanel // Listen write request via chanel
func (c *Client) listenWrite() { func (c *Client) listenWrite() {
wslogger.Debugln("Listening write to client")
for { for {
select { select {
@ -93,7 +92,6 @@ func (c *Client) listenWrite() {
// Listen read request via chanel // Listen read request via chanel
func (c *Client) listenRead() { func (c *Client) listenRead() {
wslogger.Debugln("Listening read from client")
for { for {
select { select {

View File

@ -5,7 +5,7 @@ import "github.com/ethereum/go-ethereum/ethutil"
type Message struct { type Message struct {
Call string `json:"call"` Call string `json:"call"`
Args []interface{} `json:"args"` Args []interface{} `json:"args"`
Seed int `json:"seed"` Id int `json:"_id"`
Data interface{} `json:"data"` Data interface{} `json:"data"`
} }

View File

@ -81,8 +81,6 @@ func (s *Server) MessageFunc(f MsgFunc) {
// Listen and serve. // Listen and serve.
// It serves client connection and broadcast request. // It serves client connection and broadcast request.
func (s *Server) Listen() { func (s *Server) Listen() {
wslogger.Debugln("Listening server...")
// ws handler // ws handler
onConnected := func(ws *ws.Conn) { onConnected := func(ws *ws.Conn) {
defer func() { defer func() {