Skip to content

Commit e61d3d8

Browse files
committed
Fix ping pong configuration and update docs
Signed-off-by: Lorenzo <[email protected]>
1 parent 4ef6c14 commit e61d3d8

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,18 +486,27 @@ own logging system.
486486

487487
### Websocket ping-pong
488488

489-
The websocket package currently supports client-initiated pings only.
489+
The websocket package supports configuring ping pong for both endpoints.
490490

491-
If your setup requires the server to be the initiator of a ping-pong (e.g. for web-based charge points),
492-
you may disable ping-pong entirely and just rely on the heartbeat mechanism:
491+
By default, the client sends a ping every 54 seconds and waits for a pong for 60 seconds, before timing out.
492+
The values can be configured as follows:
493+
```go
494+
cfg := ws.NewClientTimeoutConfig()
495+
cfg.PingPeriod = 10 * time.Second
496+
cfg.PongWait = 20 * time.Second
497+
websocketClient.SetTimeoutConfig(cfg)
498+
```
493499

500+
By default, the server does not send out any pings and waits for a ping from the client for 60 seconds, before timing out.
501+
To configure the server to send out pings, the `PingPeriod` and `PongWait` must be set to a value greater than 0:
494502
```go
495503
cfg := ws.NewServerTimeoutConfig()
496-
cfg.PingWait = 0 // this instructs the server to wait forever
504+
cfg.PingPeriod = 10 * time.Second
505+
cfg.PongWait = 20 * time.Second
497506
websocketServer.SetTimeoutConfig(cfg)
498507
```
499508

500-
> A server-initiated ping may be supported in a future release.
509+
To disable sending ping messages, set the `PingPeriod` value to `0`.
501510

502511
## OCPP 2.0.1 Usage
503512

ws/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ func (c *client) Start(urlStr string) error {
341341
ws,
342342
resp.TLS,
343343
NewDefaultWebSocketConfig(
344-
true,
345344
c.timeoutConfig.WriteWait,
346345
0,
347346
c.timeoutConfig.PingPeriod,

ws/server.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,10 @@ out:
418418
conn,
419419
r.TLS,
420420
NewDefaultWebSocketConfig(
421-
false,
422421
s.timeoutConfig.WriteWait,
423422
s.timeoutConfig.PingWait,
424-
0,
425-
0),
423+
s.timeoutConfig.PingPeriod,
424+
s.timeoutConfig.PongWait),
426425
s.handleMessage,
427426
s.handleDisconnect,
428427
func(_ Channel, err error) {

ws/websocket.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ func SetLogger(logger logging.Logger) {
6161
// To set a custom configuration, refer to the server's SetTimeoutConfig method.
6262
// If no configuration is passed, a default configuration is generated via the NewServerTimeoutConfig function.
6363
type ServerTimeoutConfig struct {
64-
WriteWait time.Duration // The timeout for network write operations. After a timeout, the connection is closed.
65-
PingWait time.Duration // The timeout for waiting for a ping from the client. After a timeout, the connection is closed.
66-
PingEnabled bool // Whether the server should send ping messages to each client.
67-
PingPeriod time.Duration // The interval for sending ping messages to a client. This is only relevant if PingEnabled is true.
64+
WriteWait time.Duration // The timeout for network write operations. After a timeout, the connection is closed.
65+
PingWait time.Duration // The timeout for waiting for a ping from the client. After a timeout, the connection is closed.
66+
PingPeriod time.Duration // The interval for sending ping messages to a client. If set to 0, no pings are sent.
67+
PongWait time.Duration // The timeout for waiting for a pong from the server. After a timeout, the connection is closed. Needs to be set, if server is configured to send ping messages.
6868
}
6969

7070
// NewServerTimeoutConfig creates a default timeout configuration for a websocket endpoint.
@@ -73,10 +73,10 @@ type ServerTimeoutConfig struct {
7373
// You may change fields arbitrarily and pass the struct to a SetTimeoutConfig method.
7474
func NewServerTimeoutConfig() ServerTimeoutConfig {
7575
return ServerTimeoutConfig{
76-
WriteWait: defaultWriteWait,
77-
PingWait: defaultPingWait,
78-
PingEnabled: false,
79-
PingPeriod: defaultPingPeriod,
76+
WriteWait: defaultWriteWait,
77+
PingWait: defaultPingWait,
78+
PingPeriod: 0,
79+
PongWait: 0,
8080
}
8181
}
8282

@@ -88,8 +88,8 @@ func NewServerTimeoutConfig() ServerTimeoutConfig {
8888
type ClientTimeoutConfig struct {
8989
WriteWait time.Duration // The timeout for network write operations. After a timeout, the connection is closed.
9090
HandshakeTimeout time.Duration // The timeout for the initial handshake to complete.
91-
PongWait time.Duration // The timeout for waiting for a pong from the server. After a timeout, the connection is closed.
92-
PingPeriod time.Duration
91+
PongWait time.Duration // The timeout for waiting for a pong from the server. After a timeout, the connection is closed. Needs to be set, if client is configured to send ping messages.
92+
PingPeriod time.Duration // The interval for sending ping messages to a server. If set to 0, no pings are sent.
9393
RetryBackOffRepeatTimes int
9494
RetryBackOffRandomRange int
9595
RetryBackOffWaitMinimum time.Duration
@@ -191,13 +191,12 @@ type PingConfig struct {
191191
//
192192
// No custom configuration functions are run. Overrides need to be applied externally.
193193
func NewDefaultWebSocketConfig(
194-
sendPing bool,
195194
writeWait time.Duration,
196195
readWait time.Duration,
197196
pingPeriod time.Duration,
198197
pongWait time.Duration) WebSocketConfig {
199198
var pingCfg *PingConfig
200-
if sendPing {
199+
if pingPeriod > 0 {
201200
pingCfg = &PingConfig{
202201
PingPeriod: pingPeriod,
203202
PongWait: pongWait,

0 commit comments

Comments
 (0)