-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dot, dot/rpc: implement basic websocket support for RPC (#791)
* implement basic websocket support for RPC * remove redundant code * update tests, lint * added tests * merge conflicts * lint issues
- Loading branch information
1 parent
2eb3fb4
commit 6f62c61
Showing
15 changed files
with
182 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package rpc | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/gorilla/websocket" | ||
) | ||
|
||
// ServeHTTP implemented to handle WebSocket connections | ||
func (h *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
var upg = websocket.Upgrader{ | ||
CheckOrigin: func(r *http.Request) bool { | ||
return true | ||
}, | ||
} | ||
ws, err := upg.Upgrade(w, r, nil) | ||
if err != nil { | ||
log.Error("[rpc] websocket upgrade failed", "error", err) | ||
return | ||
} | ||
for { | ||
rpcHost := fmt.Sprintf("http://%s:%d/", h.serverConfig.Host, h.serverConfig.RPCPort) | ||
for { | ||
_, mbytes, err := ws.ReadMessage() | ||
if err != nil { | ||
log.Error("[rpc] websocket failed to read message", "error", err) | ||
return | ||
} | ||
log.Trace("[rpc] websocket received", "message", fmt.Sprintf("%s", mbytes)) | ||
client := &http.Client{} | ||
buf := &bytes.Buffer{} | ||
_, err = buf.Write(mbytes) | ||
if err != nil { | ||
log.Error("[rpc] failed to write message to buffer", "error", err) | ||
return | ||
} | ||
|
||
req, err := http.NewRequest("POST", rpcHost, buf) | ||
if err != nil { | ||
log.Error("[rpc] failed request to rpc service", "error", err) | ||
return | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json;") | ||
|
||
res, err := client.Do(req) | ||
if err != nil { | ||
log.Error("[rpc] websocket error calling rpc", "error", err) | ||
return | ||
} | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
log.Error("[rpc] error reading response body", "error", err) | ||
return | ||
} | ||
|
||
err = res.Body.Close() | ||
if err != nil { | ||
log.Error("[rpc] error closing response body", "error", err) | ||
return | ||
} | ||
var wsSend interface{} | ||
err = json.Unmarshal(body, &wsSend) | ||
if err != nil { | ||
log.Error("[rpc] error unmarshal rpc response", "error", err) | ||
return | ||
} | ||
|
||
err = ws.WriteJSON(wsSend) | ||
if err != nil { | ||
log.Error("[rpc] error writing json response", "error", err) | ||
return | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package rpc | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var addr = flag.String("addr", "localhost:8546", "http service address") | ||
var testCalls = []struct { | ||
call []byte | ||
expected []byte | ||
}{ | ||
{[]byte(`{"jsonrpc":"2.0","method":"system_name","params":[],"id":1}`), []byte(`{"id":1,"jsonrpc":"2.0","result":"gossamer v0.0"}` + "\n")}, // working request | ||
{[]byte(`{"jsonrpc":"2.0","method":"unknown","params":[],"id":1}`), []byte(`{"error":{"code":-32000,"data":null,"message":"rpc error method unknown not found"},"id":1,"jsonrpc":"2.0"}` + "\n")}, // unknown method | ||
{[]byte{}, []byte(`{"error":{"code":-32700,"data":{"id":null,"jsonrpc":"","method":"","params":null},"message":"EOF"},"id":null,"jsonrpc":"2.0"}` + "\n")}, // empty request | ||
} | ||
|
||
func TestNewWebSocketServer(t *testing.T) { | ||
|
||
cfg := &HTTPServerConfig{ | ||
Modules: []string{"system"}, | ||
RPCPort: 8545, | ||
WSPort: 8546, | ||
} | ||
s := NewHTTPServer(cfg) | ||
err := s.Start() | ||
require.Nil(t, err) | ||
|
||
time.Sleep(time.Second) // give server a second to start | ||
|
||
u := url.URL{Scheme: "ws", Host: *addr, Path: "/"} | ||
log.Printf("connecting to %s", u.String()) | ||
|
||
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil) | ||
if err != nil { | ||
log.Fatal("dial:", err) | ||
} | ||
defer c.Close() | ||
|
||
for _, item := range testCalls { | ||
err = c.WriteMessage(websocket.TextMessage, item.call) | ||
require.Nil(t, err) | ||
|
||
_, message, err := c.ReadMessage() | ||
require.Nil(t, err) | ||
require.Equal(t, item.expected, message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ enabled = false | |
port = 8545 | ||
host = "localhost" | ||
modules = ["system", "author", "chain"] | ||
ws-port = 8546 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ enabled = false | |
port = 8545 | ||
host = "localhost" | ||
modules = ["system"] | ||
ws-port = 8546 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters