mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 14:34:17 +00:00
bring back EventSubscriber
This commit is contained in:
@@ -2,6 +2,7 @@ package rpcserver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -433,8 +434,8 @@ type wsConnection struct {
|
||||
// Send pings to server with this period. Must be less than readWait, but greater than zero.
|
||||
pingPeriod time.Duration
|
||||
|
||||
// see DisconnectCallback option.
|
||||
disconnectCallback func(remoteAddr string)
|
||||
// object that is used to subscribe / unsubscribe from events
|
||||
eventSub types.EventSubscriber
|
||||
}
|
||||
|
||||
// NewWSConnection wraps websocket.Conn.
|
||||
@@ -467,11 +468,12 @@ func NewWSConnection(
|
||||
return wsc
|
||||
}
|
||||
|
||||
// DisconnectCallback can be used optionally to set a callback, which will be
|
||||
// called upon disconnect - not Goroutine-safe.
|
||||
func DisconnectCallback(cb func(remoteAddr string)) func(*wsConnection) {
|
||||
// EventSubscriber sets object that is used to subscribe / unsubscribe from
|
||||
// events - not Goroutine-safe. If none given, default node's eventBus will be
|
||||
// used.
|
||||
func EventSubscriber(eventSub types.EventSubscriber) func(*wsConnection) {
|
||||
return func(wsc *wsConnection) {
|
||||
wsc.disconnectCallback = cb
|
||||
wsc.eventSub = eventSub
|
||||
}
|
||||
}
|
||||
|
||||
@@ -524,8 +526,9 @@ func (wsc *wsConnection) OnStart() error {
|
||||
func (wsc *wsConnection) OnStop() {
|
||||
// Both read and write loops close the websocket connection when they exit their loops.
|
||||
// The writeChan is never closed, to allow WriteRPCResponse() to fail.
|
||||
if wsc.disconnectCallback != nil {
|
||||
wsc.disconnectCallback(wsc.remoteAddr)
|
||||
|
||||
if wsc.eventSub != nil {
|
||||
wsc.eventSub.UnsubscribeAll(context.TODO(), wsc.remoteAddr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -535,6 +538,11 @@ func (wsc *wsConnection) GetRemoteAddr() string {
|
||||
return wsc.remoteAddr
|
||||
}
|
||||
|
||||
// GetEventSubscriber implements WSRPCConnection by returning event subscriber.
|
||||
func (wsc *wsConnection) GetEventSubscriber() types.EventSubscriber {
|
||||
return wsc.eventSub
|
||||
}
|
||||
|
||||
// WriteRPCResponse pushes a response to the writeChan, and blocks until it is accepted.
|
||||
// It implements WSRPCConnection. It is Goroutine-safe.
|
||||
func (wsc *wsConnection) WriteRPCResponse(resp types.RPCResponse) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package rpctypes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
@@ -9,6 +10,9 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
|
||||
amino "github.com/tendermint/go-amino"
|
||||
|
||||
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
)
|
||||
|
||||
// a wrapper to emulate a sum type: jsonrpcid = string | int
|
||||
@@ -237,6 +241,7 @@ type WSRPCConnection interface {
|
||||
GetRemoteAddr() string
|
||||
WriteRPCResponse(resp RPCResponse)
|
||||
TryWriteRPCResponse(resp RPCResponse) bool
|
||||
GetEventSubscriber() EventSubscriber
|
||||
Codec() *amino.Codec
|
||||
}
|
||||
|
||||
@@ -246,6 +251,13 @@ type WSRPCContext struct {
|
||||
WSRPCConnection
|
||||
}
|
||||
|
||||
// EventSubscriber mirros tendermint/tendermint/types.EventBusSubscriber
|
||||
type EventSubscriber interface {
|
||||
Subscribe(ctx context.Context, subscriber string, query tmpubsub.Query, outCapacity ...int) (tmtypes.Subscription, error)
|
||||
Unsubscribe(ctx context.Context, subscriber string, query tmpubsub.Query) error
|
||||
UnsubscribeAll(ctx context.Context, subscriber string) error
|
||||
}
|
||||
|
||||
//----------------------------------------
|
||||
// SOCKETS
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user