Skip to content

Commit 128f0ca

Browse files
authored
Merge pull request #228 from twitchdev/fix-226
Fix 226 and 227
2 parents d10cf02 + 403aa18 commit 128f0ca

File tree

4 files changed

+62
-39
lines changed

4 files changed

+62
-39
lines changed

internal/events/websocket/mock_server/close_messages.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ type CloseMessage struct {
66
}
77

88
var (
9+
closeClientDisconnected = &CloseMessage{
10+
code: 1000,
11+
message: "client disconnected",
12+
}
13+
914
closeInternalServerError = &CloseMessage{
1015
code: 4000,
1116
message: "internal server error",

internal/events/websocket/mock_server/manager.go

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ import (
2424

2525
type ServerManager struct {
2626
serverList *util.List[WebSocketServer]
27-
reconnectTesting bool
28-
primaryServer string
29-
ip string
30-
port int
31-
debugEnabled bool
32-
strictMode bool
33-
sslEnabled bool
34-
protocolHttp string
35-
protocolWs string
27+
reconnectTesting bool // Indicates if the server is in the process of running a simulation server reconnect/restart
28+
primaryServer string // The current primary server by its ID. This should be in serverList
29+
ip string // IP the server will bind to
30+
port int // Port the server will bind to
31+
debugEnabled bool // Indicates if the server was started with --debug
32+
strictMode bool // Indicates if the server was started with --require-subscriptions
33+
sslEnabled bool // Indicates if the server was started with --ssl
34+
protocolHttp string // String for the HTTP protocol URIs (http or https)
35+
protocolWs string // String for the WS protocol URIs (ws or wss)
3636
}
3737

3838
var serverManager *ServerManager
@@ -269,11 +269,13 @@ func subscriptionPageHandlerGet(w http.ResponseWriter, r *http.Request) {
269269
Status: subscription.Status,
270270
Type: subscription.Type,
271271
Version: subscription.Version,
272-
Condition: EmptyStruct{},
272+
Condition: subscription.Conditions,
273273
CreatedAt: subscription.CreatedAt,
274274
Transport: SubscriptionTransport{
275-
Method: "websocket",
276-
SessionID: fmt.Sprintf("%v_%v", server.ServerId, clientName),
275+
Method: "websocket",
276+
SessionID: fmt.Sprintf("%v_%v", server.ServerId, clientName),
277+
ConnectedAt: subscription.ClientConnectedAt,
278+
DisconnectedAt: subscription.ClientDisconnectedAt,
277279
},
278280
Cost: 0,
279281
})
@@ -378,6 +380,8 @@ func subscriptionPageHandlerPost(w http.ResponseWriter, r *http.Request) {
378380
CreatedAt: time.Now().UTC().Format(time.RFC3339Nano),
379381
Status: STATUS_ENABLED, // https://dev.twitch.tv/docs/api/reference/#get-eventsub-subscriptions
380382
SessionClientName: clientName,
383+
Conditions: body.Condition,
384+
ClientConnectedAt: client.ConnectedAtTimestamp,
381385
}
382386

383387
var subs []Subscription
@@ -397,19 +401,21 @@ func subscriptionPageHandlerPost(w http.ResponseWriter, r *http.Request) {
397401
w.WriteHeader(http.StatusAccepted)
398402

399403
json.NewEncoder(w).Encode(&SubscriptionPostSuccessResponse{
400-
Body: SubscriptionPostSuccessResponseBody{
401-
ID: subscription.SubscriptionID,
402-
Status: subscription.Status,
403-
Type: subscription.Type,
404-
Version: subscription.Version,
405-
Condition: EmptyStruct{},
406-
CreatedAt: subscription.CreatedAt,
407-
Transport: SubscriptionTransport{
408-
Method: "websocket",
409-
SessionID: fmt.Sprintf("%v_%v", server.ServerId, clientName),
410-
ConnectedAt: client.ConnectedAtTimestamp,
404+
Data: []SubscriptionPostSuccessResponseBody{
405+
{
406+
ID: subscription.SubscriptionID,
407+
Status: subscription.Status,
408+
Type: subscription.Type,
409+
Version: subscription.Version,
410+
Condition: subscription.Conditions,
411+
CreatedAt: subscription.CreatedAt,
412+
Transport: SubscriptionTransport{
413+
Method: "websocket",
414+
SessionID: fmt.Sprintf("%v_%v", server.ServerId, clientName),
415+
ConnectedAt: client.ConnectedAtTimestamp,
416+
},
417+
Cost: 0,
411418
},
412-
Cost: 0,
413419
},
414420
Total: 0,
415421
MaxTotalCost: 10,
@@ -519,7 +525,7 @@ func handlerResponseErrorUnauthorized(w http.ResponseWriter, message string) {
519525
func handlerResponseErrorConflict(w http.ResponseWriter, message string) {
520526
w.WriteHeader(http.StatusConflict)
521527
bytes, _ := json.Marshal(&SubscriptionPostErrorResponse{
522-
Error: "Unauthorized",
528+
Error: "Conflict",
523529
Message: message,
524530
Status: 409,
525531
})

internal/events/websocket/mock_server/server.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
223223
log.Printf("read err [%v]: %v", client.clientName, err)
224224

225225
ws.muClients.Lock()
226-
client.CloseWithReason(closeNetworkError)
227-
ws.handleClientConnectionClose(client, closeNetworkError)
226+
client.CloseWithReason(closeClientDisconnected)
227+
ws.handleClientConnectionClose(client, closeClientDisconnected)
228228
ws.muClients.Unlock()
229229
break
230230
}
@@ -488,9 +488,11 @@ func (ws *WebSocketServer) handleClientConnectionClose(client *Client, closeReas
488488
if ws.Status == 2 {
489489
ws.muSubscriptions.Lock()
490490
subscriptions := ws.Subscriptions[client.clientName]
491-
for _, subscription := range subscriptions {
492-
if subscription.Status == STATUS_ENABLED {
493-
subscription.Status = getStatusFromCloseMessage(closeReason)
491+
for i := range subscriptions {
492+
if subscriptions[i].Status == STATUS_ENABLED {
493+
subscriptions[i].Status = getStatusFromCloseMessage(closeReason)
494+
subscriptions[i].ClientConnectedAt = ""
495+
subscriptions[i].ClientDisconnectedAt = time.Now().UTC().Format(time.RFC3339Nano)
494496
}
495497
}
496498
ws.Subscriptions[client.clientName] = subscriptions

internal/events/websocket/mock_server/subscription.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package mock_server
22

3+
import "github.com/twitchdev/twitch-cli/internal/models"
4+
35
type Subscription struct {
46
SubscriptionID string // Random GUID for the subscription
57
ClientID string // Client ID included in headers
@@ -8,14 +10,19 @@ type Subscription struct {
810
CreatedAt string // Timestamp of when the subscription was created
911
Status string // Status of the subscription
1012
SessionClientName string // Client name of the session this is associated with.
13+
14+
ClientConnectedAt string // Time client connected
15+
ClientDisconnectedAt string // Time client disconnected
16+
17+
Conditions models.EventsubCondition // Values of the subscription's condition object
1118
}
1219

1320
// Request - POST /eventsub/subscriptions
1421
type SubscriptionPostRequest struct {
15-
Type string `json:"type"`
16-
Version string `json:"version"`
17-
Condition interface{} `json:"condition"`
22+
Type string `json:"type"`
23+
Version string `json:"version"`
1824

25+
Condition models.EventsubCondition `json:"condition"`
1926
Transport SubscriptionPostRequestTransport `json:"transport"`
2027
}
2128

@@ -27,7 +34,7 @@ type SubscriptionPostRequestTransport struct {
2734

2835
// Response (Success) - POST /eventsub/subscriptions
2936
type SubscriptionPostSuccessResponse struct {
30-
Body SubscriptionPostSuccessResponseBody `json:"body"`
37+
Data []SubscriptionPostSuccessResponseBody `json:"data"`
3138

3239
Total int `json:"total"`
3340
MaxTotalCost int `json:"max_total_cost"`
@@ -44,8 +51,8 @@ type SubscriptionPostSuccessResponseBody struct {
4451
CreatedAt string `json:"created_at"`
4552
Cost int `json:"cost"`
4653

47-
Condition EmptyStruct `json:"condition"`
48-
Transport SubscriptionTransport `json:"transport"`
54+
Condition models.EventsubCondition `json:"condition"`
55+
Transport SubscriptionTransport `json:"transport"`
4956
}
5057

5158
// Response (Error) - POST /eventsub/subscriptions
@@ -67,9 +74,10 @@ type SubscriptionGetSuccessResponse struct {
6774

6875
// Cross-usage
6976
type SubscriptionTransport struct {
70-
Method string `json:"method"`
71-
SessionID string `json:"session_id"`
72-
ConnectedAt string `json:"connected_at"`
77+
Method string `json:"method"`
78+
SessionID string `json:"session_id"`
79+
ConnectedAt string `json:"connected_at,omitempty"`
80+
DisconnectedAt string `json:"disconnected_at,omitempty"`
7381
}
7482

7583
// Cross-usage
@@ -112,6 +120,8 @@ func getStatusFromCloseMessage(reason *CloseMessage) string {
112120
code := reason.code
113121

114122
switch code {
123+
case 1000:
124+
return STATUS_WEBSOCKET_DISCONNECTED
115125
case 4000:
116126
return STATUS_INTERNAL_ERROR
117127
case 4001:

0 commit comments

Comments
 (0)