Skip to content

Commit a7a9d35

Browse files
committed
Send pings from ws server
1 parent 28c59fd commit a7a9d35

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

options_server.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package jsonrpc
33
import (
44
"context"
55
"reflect"
6+
"time"
67
)
78

89
type ParamDecoder func(ctx context.Context, json []byte) (reflect.Value, error)
910

1011
type ServerConfig struct {
11-
paramDecoders map[reflect.Type]ParamDecoder
1212
maxRequestSize int64
13-
errors *Errors
13+
pingInterval time.Duration
14+
15+
paramDecoders map[reflect.Type]ParamDecoder
16+
errors *Errors
1417
}
1518

1619
type ServerOption func(c *ServerConfig)
@@ -19,6 +22,8 @@ func defaultServerConfig() ServerConfig {
1922
return ServerConfig{
2023
paramDecoders: map[reflect.Type]ParamDecoder{},
2124
maxRequestSize: DEFAULT_MAX_REQUEST_SIZE,
25+
26+
pingInterval: 5 * time.Second,
2227
}
2328
}
2429

@@ -39,3 +44,9 @@ func WithServerErrors(es Errors) ServerOption {
3944
c.errors = &es
4045
}
4146
}
47+
48+
func WithServerPingInterval(d time.Duration) ServerOption {
49+
return func(c *ServerConfig) {
50+
c.pingInterval = d
51+
}
52+
}

server.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"reflect"
99
"strings"
10+
"time"
1011

1112
"github.com/gorilla/websocket"
1213
)
@@ -28,6 +29,7 @@ type RPCServer struct {
2829

2930
paramDecoders map[reflect.Type]ParamDecoder
3031

32+
pingInterval time.Duration
3133
maxRequestSize int64
3234
}
3335

@@ -44,6 +46,8 @@ func NewServer(opts ...ServerOption) *RPCServer {
4446
paramDecoders: config.paramDecoders,
4547
maxRequestSize: config.maxRequestSize,
4648
errors: config.errors,
49+
50+
pingInterval: config.pingInterval,
4751
}
4852
}
4953

@@ -69,9 +73,10 @@ func (s *RPCServer) handleWS(ctx context.Context, w http.ResponseWriter, r *http
6973
}
7074

7175
(&wsConn{
72-
conn: c,
73-
handler: s,
74-
exiting: make(chan struct{}),
76+
conn: c,
77+
handler: s,
78+
pingInterval: s.pingInterval,
79+
exiting: make(chan struct{}),
7580
}).handleWsConn(ctx)
7681

7782
if err := c.Close(); err != nil {

0 commit comments

Comments
 (0)