Skip to content

Commit

Permalink
send keepalive on init
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Aug 8, 2019
1 parent bcddd7a commit f93df34
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions client/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
connectionInitMsg = "connection_init" // Client -> Server
startMsg = "start" // Client -> Server
connectionAckMsg = "connection_ack" // Server -> Client
connectionKa = "ka" // Server -> Client
dataMsg = "data" // Server -> Client
errorMsg = "error" // Server -> Client
)
Expand Down Expand Up @@ -72,10 +73,20 @@ func (p *Client) WebsocketWithPayload(query string, initPayload map[string]inter
if err = c.ReadJSON(&ack); err != nil {
return errorSubscription(fmt.Errorf("ack: %s", err.Error()))
}

if ack.Type != connectionAckMsg {
return errorSubscription(fmt.Errorf("expected ack message, got %#v", ack))
}

var ka operationMessage
if err = c.ReadJSON(&ka); err != nil {
return errorSubscription(fmt.Errorf("ka: %s", err.Error()))
}

if ka.Type != connectionKa {
return errorSubscription(fmt.Errorf("expected ka message, got %#v", ack))
}

if err = c.WriteJSON(operationMessage{Type: startMsg, ID: "1", Payload: requestBody}); err != nil {
return errorSubscription(fmt.Errorf("start: %s", err.Error()))
}
Expand Down
1 change: 1 addition & 0 deletions handler/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (c *wsConnection) init() bool {
}

c.write(&operationMessage{Type: connectionAckMsg})
c.write(&operationMessage{Type: connectionKeepAliveMsg})
case connectionTerminateMsg:
c.close(websocket.CloseNormalClosure, "terminated")
return false
Expand Down
7 changes: 7 additions & 0 deletions handler/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestWebsocket(t *testing.T) {
require.NoError(t, c.WriteJSON(&operationMessage{Type: connectionInitMsg}))

require.Equal(t, connectionAckMsg, readOp(c).Type)
require.Equal(t, connectionKeepAliveMsg, readOp(c).Type)
})

t.Run("client can terminate before run", func(t *testing.T) {
Expand All @@ -67,6 +68,7 @@ func TestWebsocket(t *testing.T) {

require.NoError(t, c.WriteJSON(&operationMessage{Type: connectionInitMsg}))
require.Equal(t, connectionAckMsg, readOp(c).Type)
require.Equal(t, connectionKeepAliveMsg, readOp(c).Type)

require.NoError(t, c.WriteJSON(&operationMessage{Type: connectionTerminateMsg}))

Expand All @@ -80,6 +82,7 @@ func TestWebsocket(t *testing.T) {

require.NoError(t, c.WriteJSON(&operationMessage{Type: connectionInitMsg}))
require.Equal(t, connectionAckMsg, readOp(c).Type)
require.Equal(t, connectionKeepAliveMsg, readOp(c).Type)

require.NoError(t, c.WriteJSON(&operationMessage{
Type: startMsg,
Expand All @@ -98,6 +101,7 @@ func TestWebsocket(t *testing.T) {

require.NoError(t, c.WriteJSON(&operationMessage{Type: connectionInitMsg}))
require.Equal(t, connectionAckMsg, readOp(c).Type)
require.Equal(t, connectionKeepAliveMsg, readOp(c).Type)

require.NoError(t, c.WriteJSON(&operationMessage{
Type: startMsg,
Expand Down Expand Up @@ -138,6 +142,7 @@ func TestWebsocketWithKeepAlive(t *testing.T) {

require.NoError(t, c.WriteJSON(&operationMessage{Type: connectionInitMsg}))
require.Equal(t, connectionAckMsg, readOp(c).Type)
require.Equal(t, connectionKeepAliveMsg, readOp(c).Type)

require.NoError(t, c.WriteJSON(&operationMessage{
Type: startMsg,
Expand Down Expand Up @@ -174,6 +179,7 @@ func TestWebsocketInitFunc(t *testing.T) {
require.NoError(t, c.WriteJSON(&operationMessage{Type: connectionInitMsg}))

require.Equal(t, connectionAckMsg, readOp(c).Type)
require.Equal(t, connectionKeepAliveMsg, readOp(c).Type)
})

t.Run("accept connection if WebsocketInitFunc is provided and is accepting connection", func(t *testing.T) {
Expand All @@ -189,6 +195,7 @@ func TestWebsocketInitFunc(t *testing.T) {
require.NoError(t, c.WriteJSON(&operationMessage{Type: connectionInitMsg}))

require.Equal(t, connectionAckMsg, readOp(c).Type)
require.Equal(t, connectionKeepAliveMsg, readOp(c).Type)
})

t.Run("reject connection if WebsocketInitFunc is provided and is accepting connection", func(t *testing.T) {
Expand Down

0 comments on commit f93df34

Please sign in to comment.