Skip to content

Commit aa8b2bc

Browse files
authored
feat: provide callback for connection alive messages (shurcooL#77)
1 parent bb54675 commit aa8b2bc

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

subscription.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,17 @@ type SubscriptionProtocol interface {
107107
// SubscriptionContext represents a shared context for protocol implementations with the websocket connection inside
108108
type SubscriptionContext struct {
109109
context.Context
110-
websocketConn WebsocketConn
111-
OnConnected func()
112-
onDisconnected func()
113-
cancel context.CancelFunc
114-
subscriptions map[string]Subscription
115-
disabledLogTypes []OperationMessageType
116-
log func(args ...interface{})
117-
acknowledged int32
118-
exitStatusCodes []int
119-
mutex sync.Mutex
110+
websocketConn WebsocketConn
111+
OnConnected func()
112+
onDisconnected func()
113+
onConnectionAlive func()
114+
cancel context.CancelFunc
115+
subscriptions map[string]Subscription
116+
disabledLogTypes []OperationMessageType
117+
log func(args ...interface{})
118+
acknowledged int32
119+
exitStatusCodes []int
120+
mutex sync.Mutex
120121
}
121122

122123
// Log prints condition logging with message type filters
@@ -419,6 +420,12 @@ func (sc *SubscriptionClient) OnDisconnected(fn func()) *SubscriptionClient {
419420
return sc
420421
}
421422

423+
// OnConnectionAlive event is triggered when the websocket receive a connection alive message (differs per protocol)
424+
func (sc *SubscriptionClient) OnConnectionAlive(fn func()) *SubscriptionClient {
425+
sc.context.onConnectionAlive = fn
426+
return sc
427+
}
428+
422429
// get internal client status
423430
func (sc *SubscriptionClient) getClientStatus() int32 {
424431
return atomic.LoadInt32(&sc.clientStatus)

subscription_graphql_ws.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ func (gws *graphqlWS) OnMessage(ctx *SubscriptionContext, subscription Subscript
132132
_ = gws.Unsubscribe(ctx, message.ID)
133133
case GQLPing:
134134
ctx.Log(message, "server", GQLPing)
135+
if ctx.onConnectionAlive != nil {
136+
ctx.onConnectionAlive()
137+
}
135138
// send pong response message back to the server
136139
msg := OperationMessage{
137140
Type: GQLPong,

subscriptions_transport_ws.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ func (stw *subscriptionsTransportWS) OnMessage(ctx *SubscriptionContext, subscri
164164
_ = stw.Unsubscribe(ctx, message.ID)
165165
case GQLConnectionKeepAlive:
166166
ctx.Log(message, "server", GQLConnectionKeepAlive)
167+
if ctx.onConnectionAlive != nil {
168+
ctx.onConnectionAlive()
169+
}
167170
case GQLConnectionAck:
168171
// Expected response to the ConnectionInit message from the client acknowledging a successful connection with the server.
169172
// The client is now ready to request subscription operations.

0 commit comments

Comments
 (0)