Skip to content

Commit

Permalink
Merge pull request #318 from twitchdev/fix-315
Browse files Browse the repository at this point in the history
Forwarding events carried over WebSockets reconnect doesn't work
  • Loading branch information
Xemdo authored Mar 29, 2024
2 parents bf64ffa + 4af033b commit e7d0943
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
15 changes: 6 additions & 9 deletions internal/events/websocket/mock_server/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,23 @@ func printWelcomeMsg() {

log.Printf(lightBlue("Started WebSocket server on %v:%v"), serverManager.ip, serverManager.port)
if serverManager.strictMode {
log.Printf(lightBlue("--require-subscription enabled. Clients will have 10 seconds to subscribe before being disconnected."))
log.Println(lightBlue("--require-subscription enabled. Clients will have 10 seconds to subscribe before being disconnected."))
}

fmt.Println()

log.Printf(yellow("Simulate subscribing to events at: %v://%v:%v/eventsub/subscriptions"), serverManager.protocolHttp, serverManager.ip, serverManager.port)
log.Printf(yellow("POST, GET, and DELETE are supported"))
log.Printf(yellow("For more info: https://dev.twitch.tv/docs/cli/websocket-event-command/#simulate-subscribing-to-mock-eventsub"))
log.Println(yellow("POST, GET, and DELETE are supported"))
log.Println(yellow("For more info: https://dev.twitch.tv/docs/cli/websocket-event-command/#simulate-subscribing-to-mock-eventsub"))

fmt.Println()

log.Printf(lightYellow("Events can be forwarded to this server from another terminal with --transport=websocket\nExample: \"twitch event trigger channel.ban --transport=websocket\""))
log.Println(lightYellow("Events can be forwarded to this server from another terminal with --transport=websocket\nExample: \"twitch event trigger channel.ban --transport=websocket\""))
fmt.Println()
log.Printf(lightYellow("You can send to a specific client after its connected with --session\nExample: \"twitch event trigger channel.ban --transport=websocket --session=e411cc1e_a2613d4e\""))
log.Println(lightYellow("You can send to a specific client after its connected with --session\nExample: \"twitch event trigger channel.ban --transport=websocket --session=e411cc1e_a2613d4e\""))

fmt.Println()
log.Printf(lightGreen("For further usage information, please see our official documentation:\nhttps://dev.twitch.tv/docs/cli/websocket-event-command/"))
log.Println(lightGreen("For further usage information, please see our official documentation:\nhttps://dev.twitch.tv/docs/cli/websocket-event-command/"))
fmt.Println()

log.Printf(lightBlue("Connect to the WebSocket server at: ")+"%v://%v:%v/ws", serverManager.protocolWs, serverManager.ip, serverManager.port)
Expand Down Expand Up @@ -392,7 +392,6 @@ func subscriptionPageHandlerPost(w http.ResponseWriter, r *http.Request) {
Version: body.Version,
CreatedAt: time.Now().UTC().Format(time.RFC3339Nano),
Status: STATUS_ENABLED, // https://dev.twitch.tv/docs/api/reference/#get-eventsub-subscriptions
SessionClientName: clientName,
Conditions: body.Condition,
ClientConnectedAt: client.ConnectedAtTimestamp,
}
Expand Down Expand Up @@ -444,8 +443,6 @@ func subscriptionPageHandlerPost(w http.ResponseWriter, r *http.Request) {
subscription.SubscriptionID,
)
}

return
}

func subscriptionPageHandlerDelete(w http.ResponseWriter, r *http.Request) {
Expand Down
20 changes: 8 additions & 12 deletions internal/events/websocket/mock_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,12 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
client.mustSubscribeTimer = time.NewTimer(10 * time.Second)
if ws.StrictMode {
go func() {
select {
case <-client.mustSubscribeTimer.C:
if len(ws.Subscriptions[client.clientName]) == 0 {
client.CloseWithReason(closeConnectionUnused)
ws.handleClientConnectionClose(client, closeConnectionUnused)
<-client.mustSubscribeTimer.C
if len(ws.Subscriptions[client.clientName]) == 0 {
client.CloseWithReason(closeConnectionUnused)
ws.handleClientConnectionClose(client, closeConnectionUnused)

return
}
return
}
}()
}
Expand Down Expand Up @@ -270,8 +268,6 @@ func (ws *WebSocketServer) WsPageHandler(w http.ResponseWriter, r *http.Request)
ws.handleClientConnectionClose(client, closeClientSentInboundTraffic)
ws.muClients.Unlock()
}

break
}
}

Expand Down Expand Up @@ -457,13 +453,13 @@ func (ws *WebSocketServer) HandleRPCEventSubForwarding(eventsubBody string, clie
subscriptionCreatedAtTimestamp := "" // Used below if in strict mode
if ws.StrictMode {
found := false
for _, clientSubscriptions := range ws.Subscriptions {
for subscriptionClientName, clientSubscriptions := range ws.Subscriptions {
if found {
break
}

for _, sub := range clientSubscriptions {
if sub.SessionClientName == client.clientName && sub.Type == eventObj.Subscription.Type && sub.Version == eventObj.Subscription.Version {
if subscriptionClientName == client.clientName && sub.Type == eventObj.Subscription.Type && sub.Version == eventObj.Subscription.Version {
found = true
subscriptionCreatedAtTimestamp = sub.CreatedAt
}
Expand Down Expand Up @@ -514,7 +510,7 @@ func (ws *WebSocketServer) HandleRPCEventSubForwarding(eventsubBody string, clie
}

if !didSend {
msg := fmt.Sprintf("Error executing remote triggered EventSub: No clients with the subscribed to [%v / %v]", eventObj.Subscription.Type, eventObj.Subscription.Version)
msg := fmt.Sprintf("Error executing remote triggered EventSub: No clients are subscribed to [%v / %v]", eventObj.Subscription.Type, eventObj.Subscription.Version)
log.Println(msg)
return false, msg
}
Expand Down
15 changes: 7 additions & 8 deletions internal/events/websocket/mock_server/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
)

type Subscription struct {
SubscriptionID string // Random GUID for the subscription
ClientID string // Client ID included in headers
Type string // EventSub topic
Version string // EventSub topic version
CreatedAt string // Timestamp of when the subscription was created
DisabledAt *time.Time // Not public; Timestamp of when the subscription was disabled
Status string // Status of the subscription
SessionClientName string // Client name of the session this is associated with.
SubscriptionID string // Random GUID for the subscription
ClientID string // Client ID included in headers
Type string // EventSub topic
Version string // EventSub topic version
CreatedAt string // Timestamp of when the subscription was created
DisabledAt *time.Time // Not public; Timestamp of when the subscription was disabled
Status string // Status of the subscription

ClientConnectedAt string // Time client connected
ClientDisconnectedAt string // Time client disconnected
Expand Down

0 comments on commit e7d0943

Please sign in to comment.