43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
|
package websocket
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"go.fifitido.net/twitch/eventsub/websocket/messages"
|
||
|
)
|
||
|
|
||
|
func (t *Transport) handleMsg(msg *messages.Message) error {
|
||
|
rawPayload := msg.Payload.(map[string]any)
|
||
|
payload, err := messages.DecodePayload(&msg.Metadata, rawPayload)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
switch msg.Metadata.MessageType {
|
||
|
case messages.TypeSessionWelcome:
|
||
|
payload := payload.(messages.SessionWelcomePayload)
|
||
|
t.sessionId = payload.Session.ID
|
||
|
case messages.TypeSessionKeepAlive:
|
||
|
// TODO: add keep alive handler
|
||
|
case messages.TypeRevocation:
|
||
|
// TODO: add revocation handler
|
||
|
case messages.TypeSessionReconnect:
|
||
|
// TODO: add session reconnect handler
|
||
|
case messages.TypeNotification:
|
||
|
payload := payload.(messages.NotificationPayload)
|
||
|
if t.opts.EventsHandler != nil {
|
||
|
if err := t.opts.EventsHandler.Handle(payload.Event); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
default:
|
||
|
return fmt.Errorf("unknown message type: %s", msg.Metadata.MessageType)
|
||
|
}
|
||
|
|
||
|
if err := t.opts.EventsHandler.Handle(msg); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|