Websocket work over json rpc (#1030)

* added ws support

* fixed log

* ws over json rpc

* lint

* fixed bad req
This commit is contained in:
Giulio rebuffo 2020-09-02 07:56:48 +02:00 committed by GitHub
parent 050ef8773f
commit cad769ee67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -89,24 +89,34 @@ func OpenDB(cfg Flags) (ethdb.KV, ethdb.Backend, error) {
func StartRpcServer(ctx context.Context, cfg Flags, rpcAPI []rpc.API) error {
// register apis and create handler stack
httpEndpoint := fmt.Sprintf("%s:%d", cfg.HttpListenAddress, cfg.HttpPort)
srv := rpc.NewServer()
if err := node.RegisterApisFromWhitelist(rpcAPI, cfg.API, srv, false); err != nil {
return fmt.Errorf("could not start register RPC apis: %w", err)
}
handler := node.NewHTTPHandlerStack(srv, cfg.HttpCORSDomain, cfg.HttpVirtualHost)
var listener *http.Server
var err error
httpHandler := node.NewHTTPHandlerStack(srv, cfg.HttpCORSDomain, cfg.HttpVirtualHost)
var wsHandler http.Handler
if cfg.WebsocketEnabled {
listener, _, err = node.StartHTTPEndpoint(httpEndpoint, rpc.DefaultHTTPTimeouts, srv.WebsocketHandler([]string{"*"}))
if err != nil {
return fmt.Errorf("could not start Websocket: %w", err)
}
} else {
listener, _, err = node.StartHTTPEndpoint(httpEndpoint, rpc.DefaultHTTPTimeouts, handler)
if err != nil {
return fmt.Errorf("could not start RPC api: %w", err)
}
wsHandler = srv.WebsocketHandler([]string{"*"})
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if cfg.WebsocketEnabled && r.Method == "GET" {
wsHandler.ServeHTTP(w, r)
}
httpHandler.ServeHTTP(w, r)
})
listener, _, err := node.StartHTTPEndpoint(httpEndpoint, rpc.DefaultHTTPTimeouts, handler)
if err != nil {
return fmt.Errorf("could not start RPC api: %w", err)
}
log.Info("HTTP endpoint opened", "url", httpEndpoint, "ws", cfg.WebsocketEnabled)
defer func() {