Skip to content

Commit 98e34fa

Browse files
authored
Add request ID to websocket types (#172)
1 parent e571a5f commit 98e34fa

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

internal/ws/message.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import "encoding/json"
44

55
// WSMessage represents a request coming from the websocket client.
66
type WSMessage struct {
7-
Action string `json:"action"`
8-
Data json.RawMessage `json:"data,omitempty"`
7+
RequestID string `json:"requestId"`
8+
Action string `json:"action"`
9+
Data json.RawMessage `json:"data,omitempty"`
910
}
1011

1112
// WSResponse represents a message sent back to the client.
1213
type WSResponse struct {
13-
Action string `json:"action"`
14-
Status int `json:"status"`
15-
Data interface{} `json:"data,omitempty"`
14+
RequestID string `json:"requestId,omitempty"`
15+
Action string `json:"action"`
16+
Status int `json:"status"`
17+
Data interface{} `json:"data,omitempty"`
1618
}

internal/ws/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ func (s *WSServer) handleMessage(ctx context.Context, conn *connection, msg WSMe
307307
}
308308

309309
resp.Action = msg.Action
310+
resp.RequestID = msg.RequestID
310311

311312
if err := conn.safeWriteJSON(resp); err != nil {
312313
log.Errorf("failed to write JSON to WebSocket: %v", err)

internal/ws/server_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,12 @@ func (s *WSServerTestSuite) TestHandleMessageRoutesResponse() {
186186

187187
s.waitForConnections(1)
188188

189-
payload := WSMessage{Action: "echo", Data: json.RawMessage(`"hello"`)}
189+
payload := WSMessage{RequestID: "1", Action: "echo", Data: json.RawMessage(`"hello"`)}
190190
s.NoError(conn.WriteJSON(payload))
191191

192192
var resp WSResponse
193193
s.NoError(conn.ReadJSON(&resp))
194+
s.Equal("1", resp.RequestID)
194195
s.Equal("echo", resp.Action)
195196
s.Equal("hello", resp.Data)
196197
}

0 commit comments

Comments
 (0)