Skip to content

Commit

Permalink
Add ping command handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizzadar committed Dec 19, 2023
1 parent b139421 commit 30c20f6
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ func (p *provider) WebsocketLoop() {
break
}

// Intercept and handle register command here
if rawCommand.Command == "register" {
switch rawCommand.Command {
case "register":
// Intercept and handle register command here
var request registerCommandData
if err := json.Unmarshal(rawCommand.Data, &request); err != nil {
p.log.Err(err).Msg("Failed to decode register request")
Expand All @@ -117,15 +118,21 @@ func (p *provider) WebsocketLoop() {

// Set registered flag, enabling commands from bridge to come in
p.registered = true
continue
} else if rawCommand.Command == "response" {
case "ping":
buf, err := json.Marshal(RawCommand[struct{}]{Command: "pong", ReqID: rawCommand.ReqID})
if err != nil {
p.log.Err(err).Msg("Failed to encode ping response")
break
}
p.ws.WriteMessage(websocket.TextMessage, buf)
case "response":
// Otherwise pass to the results channel, we're expecting a listener
select {
case p.resultsCh <- rawCommand.Data:
default:
p.log.Warn().Msg("Failed to send response, no request waiter!")
}
} else {
default:
p.log.Warn().Str("command", rawCommand.Command).Msg("Received unknown command")
}
}
Expand Down

0 comments on commit 30c20f6

Please sign in to comment.